Installation of MySQL version 5.7.24 in centos7 and knowledge of adding, deleting, modifying and checking

MySQL Install 5.7.24

www.mysql.com www.oracle.com

http://dev.mysql.com/doc/refman/5.7/en/linux-installation.html

Oracle MySQL、MariaDB、Percona server

Select Platform:
Binary rpm Yum Repository mysql57-community-release-el7-9.noarch.rpm
Binary precompiled Generic mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
source code package installation Source Code mysql-5.7. 19.tar.gz
Insert image description here

Installation environment
[root@mysql1 ~]# rpm -q mariadb-server
package mariadb-server is not installed

[root@mysql1 ~]# sed -ri ‘/^SELINUX=/c\SELINUX=disabled’ /etc/selinux/config
[root@mysql1 ~]# setenforce 0

Method 1: Binary rpm

     http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

        wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

        rpm -ivh  mysql57-community-release-el7-9.noarch.rpm
        yum -y install mysql-server
        mysql -uroot -p'123'
        show databases;

        [root@tianyun Downloads]# md5sum mysql57-community-release-el7-9.noarch.rpm
        [root@tianyun Downloads]# yum -y install mysql57-community-release-el7-9.noarch.rpm
        [root@git ~]# yum repolist
          Loaded plugins: fastestmirror
          Loading mirror speeds from cached hostfile
          repo id                                                                     repo name                               status
          base/7/x86_64                                                       CentOS-7 - Base                         10,019
          extras/7/x86_64                                                      CentOS-7 - Extras                          321
          mysql-connectors-community/x86_64              MySQL Connectors Community                  74
          mysql-tools-community/x86_64                           MySQL Tools Community                       74
          mysql57-community/x86_64                             MySQL 5.7 Community Server                 307
          updates/7/x86_64                                                  CentOS-7 - Updates                         628
          repolist: 11,423

        [root@git ~]# yum repolist all | grep mysql
        mysql-connectors-community/x86_64 MySQL Connectors Community     enabled:     74
        mysql-connectors-community-source MySQL Connectors Community - S disabled
        mysql-tools-community/x86_64      MySQL Tools Community               enabled:             74
        mysql-tools-community-source      MySQL Tools Community - Source disabled
        mysql-tools-preview/x86_64        MySQL Tools Preview                    disabled
        mysql-tools-preview-source           MySQL Tools Preview - Source   disabled
        mysql55-community/x86_64          MySQL 5.5 Community Server     disabled
        mysql55-community-source          MySQL 5.5 Community Server - S disabled
        mysql56-community/x86_64          MySQL 5.6 Community Server     disabled
        mysql56-community-source          MySQL 5.6 Community Server - S disabled
        mysql57-community/x86_64          MySQL 5.7 Community Server     enabled:          307
        mysql57-community-source          MySQL 5.7 Community Server - S disabled
        mysql80-community/x86_64          MySQL 8.0 Community Server     disabled
        mysql80-community-source          MySQL 8.0 Community Server - S disabled

       [root@git ~]# yum-config-manager --enable mysql56-community
       将此56的MySQL设置成开机自启动
       报错解决方式:
                   -bash: yum-config-manager: command not found
                   这个是因为系统默认没有安装这个命令,这个命令在yum-utils 包里。
                    yum -y install yum-utils
       
       [root@git ~]# yum repolist enabled | grep mysql
                mysql-connectors-community/x86_64       MySQL Connectors Community            74
                mysql-tools-community/x86_64            MySQL Tools Community                 74
                mysql56-community/x86_64                MySQL 5.6 Community Server           429
                mysql57-community/x86_64                MySQL 5.7 Community Server           307

        [root@mysql1 ~]# yum -y install mysql-community-server
        [root@mysql1 ~]# systemctl start mysqld                                 //第一次启动先初始数据库
        [root@mysql1 ~]# systemctl enable mysqld
        [root@mysql1 ~]# ls /var/lib/mysql
            auto.cnf            client-key.pem      ib_logfile1         mysql.sock.lock                  server-cert.pem
            ca-key.pem       ib_buffer_pool       ibtmp1             performance_schema          server-key.pem
            ca.pem             ibdata1                  mysql               private_key.pem                 sys
            client-cert.pem  ib_logfile0            mysql.sock          public_key.pem

 
        [root@mysql ~]# grep "password" /var/log/mysqld.log 
                2019-01-15T06:45:28.734665Z 1 [Note] A temporary password is generated for root@localhost: VFISQo_dt7Rt
                2019-01-15T06:47:41.765992Z 3 [Note] Access denied for user 'root'@'localhost' (using password: YES)
        [root@mysql ~]# mysql -uroot -p'VFISQo_dt7Rt'
                mysql: [Warning] Using a password on the command line interface can be insecure.
                Welcome to the MySQL monitor.  Commands end with ; or \g.
                Your MySQL connection id is 5
                Server version: 5.7.24 MySQL Community Server (GPL)

                Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

                Oracle is a registered trademark of Oracle Corporation and/or its
                affiliates. Other names may be trademarks of their respective
                owners.

                Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

                mysql> 
              
                 mysql>   alter  user  'root'@'localhost' identified by "1qaz2WSX#";      修改密码
                 Query OK, 1 row affected (0.00 sec)
                 mysql> \q
                 Bye
                 
                 
                 
                 mysql> show databases;
                            +--------------------+
                            | Database           |
                            +--------------------+
                            | information_schema |
                            | mysql              |
                            | performance_schema |
                            | sys                |
                            +--------------------+
                            4 rows in set (0.01 sec)

                            mysql>

Method 2: Source code compilation and installation

Insert image description here

URL: https://dev.mysql.com/downloads/mysql/5.7.html#downloads ------->community---->Mysql Community Server ----->MySQL Community Server 5.7
mysql 5.7 .24 tar package: https://dev.mysql.com/downloads/file/?id=481075
Total package https://dev.mysql.com/downloads/file/?id=481150
Insert image description here

1. 编译安装
[root@mysql1 ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make

cmake:
[root@mysql1 ~]# yum -y install cmake     跨平台安装工具

boost:
[root@mysql1 ~]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz    c++库

wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

mysql:
[root@mysql1 ~]# groupadd mysql
[root@mysql1 ~]# useradd -r -g mysql -s /bin/false mysql
[root@mysql1 ~]# tar xvf mysql-5.7.19.tar.gz
[root@mysql1 ~]# cd mysql-5.7.19
[root@mysql1 mysql-5.7.19]# pwd
/root/mysql-5.7.19
解压boost文件
[root@mysql3 ~]# tar xf /root/boost_1_59_0.tar.gz
[[email protected] ~]# cmake . \
-DWITH_BOOST=boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

下面这个是一定会遇到的,只需加上下面的命令就可以了
提示:boost也可以使用如下指令自动下载
-DDOWNLOAD_BOOST=1

[root@mysql1 ~]# make
[root@mysql1 ~]# make install

2. 初始化
[root@mysql1 local]# cd mysql
[root@mysql1 mysql]# mkdir mysql-files
[root@mysql1 mysql]# chown -R mysql.mysql .
[root@mysql1 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@mysql1 mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

建立MySQL配置文件my.cnf
[root@mysql1 mysql]# vim /etc/my.cnf
[mysqld]sys
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

启动MySQL

 ***centos6***
 
使用centos6 mysql.server脚本(system V)
[root@mysql1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql1 mysql]# chkconfig --add mysqld
[root@mysql1 mysql]# chkconfig mysqld on
[root@mysql1 mysql]# service mysqld start
[root@mysql1 mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/mysql2.err'.
 SUCCESS! 
[root@mysql1 mysql]# ps aux |grep mysqld
mysql    76 pts/0  Sl   14:38   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql2.err --pid-file=/usr/local/mysql/data/mysql2.pid

报错1:
启动的时候会遇到default-character-set=utf8mb4报错,将下面这个写入到/etc/my.cnf
default-character-set=utf8mb4

报错2:
root@MyServer:~# service mysql start
Starting MySQL
..The server quit without updating PID file (/usr/local/mysql/data/MyServer.pid). ... failed!
错误解决排查思路:
1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限
2.可能进程里已经存在mysql进程
3.实在不行就重新初始化


登陆数据库:
[root@MySQL ~]#  /usr/local/mysql/bin/mysql -uroot -p'zijz,Bsqe2<A'
        mysql: [Warning] Using a password on the command line interface can be insecure.
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 6
        Server version: 5.7.24

        Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

        Oracle is a registered trademark of Oracle Corporation and/or its
        affiliates. Other names may be trademarks of their respective
        owners.

        Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

        mysql> show databases;
        ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
        mysql> alter user 'root'@'localhost' identified by '123' ;
        Query OK, 0 rows affected (0.00 sec)

        mysql> \q
        Bye

PATH[可选]
[root@mysql2 mysql]# mysql
-bash: mysql: command not found
[root@mysql2 mysql]# /usr/local/mysql/bin/mysql
[root@mysql1 mysql]# echo “export PATH=$PATH:/usr/local/mysql/bin” >> /etc/profile
[root@mysql1 mysql]# source /etc/profile
[root@mysql1 mysql]# mysqladmin -uroot -p’>>e4KKcol6i1’ password ‘(TianYunYang123)’

We may encounter this problem when installing mysql: When
Insert image description here
encountering this, it means that we have already initialized the database and we need to re-initialize it.

If reinitialization is required...[optional]
[root@mysql1 ~]# killall mysqld
[root@mysql1 ~]# rm -rf /usr/local/mysql/data
[root@mysql1 mysql]# bin/mysqld --initialize - -user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@mysql1 mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

RPM or Yum:
datadir: /var/lib/mysql
source package and precompilation:
basedir: datadir:
/usr/local/mysql /usr/local/mysql/data

Compilation error

The following error occurs when cmake compiles mysql:

Insert image description here
Solution:

Install ncurses-devel: yum -y install ncurses-devel (Debian/Ubuntu needs to install libncurses5-dev).
Delete CMakeCache.txt (the file is in the mysql decompression directory)
and recompile.
Original text: https://blog.csdn.net /zsl10/article/details/51824091

Forgot MySQL password

MySQL 5.7.5 and earlier:


            [root@mysql1 ~]# vim /etc/my.cnf					 
            [mysqld]
            skip-grant-tables                           跳过密码验证

            [root@mysql1 ~]# service mysqld restart
            [root@mysql1 ~]# mysql
            mysql> update mysql.user set password=password("123") where user="root" and host="localhost";   
            mysql> flush privileges;
            mysql> \q

            [root@mysql1 ~]# vim /etc/my.cnf						 
            [mysqld]
            #skip-grant-table
            [root@mysql1 ~]# service mysqld restart

MySQL 5.7.6 and later:


           [root@slave1 ~]# vim /etc/my.cnf
            [mysqld]
            skip-grant-tables
            [root@slave1 ~]# systemctl restart mysqld

'
Start entering the database to perform related operations.
After adding a new password, exit the database
Insert image description here
[root@slave1 ~]# vim /etc/my.cnf
delete the added line
[root@slave1 ~]# systemctl restart mysqld

Modify mysql root password

update mysql.user set authentication_string=password('123') where user='root';     键值(跳过密码验证后,重置数据库密码,建议大小写数字符号),有可能不成功是密码规则不符
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
错误1819 (HY000):您的密码不满足当前的策略要求

alter user user() identified by "1qaz2WSX#";                        修改数据库密码(符合密码规则)

flush privileges;           重新加载特权



修改完 密码后记得进入/etc/my.cnf 下去添加注释# 然后重启服务后,重新进入数据库。

mysqladmin -u root -p')vl*hfrZg3-t' password 'Qianfeng123!'   通过mysqladmin修改数据库密码

Database addition, deletion, modification and query:

Syntax: CREATE DATABASE database name;

Database naming rules:
case sensitive
, unique,
keywords such as create select cannot be used,
numbers alone cannot be used

Use the database and insert data:

mysql> create table test1( test1 是数据库里的表
-> tinyint_test tinyint, tinyint数据类型
-> int_test int
-> );
mysql> desc test1;
±-------------±-----------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±-------------±-----------±-----±----±--------±------+
| tinyint_test | tinyint(4) | YES | | NULL | |
| int_test | int(11) | YES | | NULL | |
±-------------±-----------±-----±----±--------±------+
2 rows in set (0.01 sec)
mysql> insert into test1 values (111,111); 输入数值
Query OK, 1 row affected (0.09 sec)
mysql> insert into test1(tinyint_test) values(128);
ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1 exceeds this type of value
mysql> insert into test1(int_test) values(2147483647);
Query OK, 1 row affected (0.05 sec)

删除表和库:
Table:

Create table create table
View table structure desc table, show create table
Table integrity constraints
Modify table alter table
Copy table create table ...
Delete table drop table

Library

View database: SHOW DATABASES;
Select database:
SELECT database();
USE database name
use zhaohan
Delete database: DROP DATABASE database name;
mysql> drop database zhaohan;

查询:

Simple query
SELECT * FROM employee5;
SELECT name, salary, dep_id FROM employee5;

Avoid duplication of DISTINCT
SELECT post FROM employee5;
SELECT DISTINCT post FROM employee5; Deduplication Note
: DISTINCT cannot be used partially and is usually only used for a certain field.

Query through four arithmetic operations
SELECT name, salary, salary 14 FROM employee5;
SELECT name, salary, salary
14 AS Annual_salary FROM employee5;
SELECT name, salary, salary*14 Annual_salary FROM employee5;

Define the display format
CONCAT() function is used to connect the string
SELECT CONCAT(name, ' annual salary: ', salary*14) AS Annual_salary FROM employee5;

二、单条件查询

Single condition query
SELECT name,post
FROM employee5
WHERE post='hr';

select name,post from employee5 where post=‘hr’;

Multi-condition query
SELECT name,salary
FROM employee5
WHERE post='hr' AND salary>10000;

select name,salary post from employee5 where post=‘hr’ and salary >1000;

关键字BETWEEN AND
SELECT name,salary FROM employee5
WHERE salary BETWEEN 5000 AND 15000;

SELECT name,salary FROM employee5
WHERE salary NOT BETWEEN 5000 AND 15000;

select name,salary from employee5 where salary not between 5000 and 15000;

关键字IS NULL
SELECT name,job_description FROM employee5
WHERE job_description IS NULL;

SELECT name,job_description FROM employee5
WHERE job_description IS NOT NULL;

SELECT name,job_description FROM employee5
WHERE job_description=" ";

Keyword IN set query
SELECT name, salary FROM employee5
WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000;

SELECT name, salary FROM employee5
WHERE salary IN (4000,5000,6000,9000) ;

SELECT name, salary FROM employee
WHERE salary NOT IN (4000,5000,6000,9000) ;

Keyword LIKE fuzzy query
Wildcard character '%'
SELECT * FROM employee5
WHERE name LIKE 'al%';
Wildcard character ' '
SELECT * FROM employee5
WHERE name LIKE 'al
__';

3. Query sorting Sort
by a single column
SELECT * FROM employee5 ORDER BY salary;
SELECT name, salary employee FROM5 ORDER BY salary ASC; Sort once from low to high
SELECT name, salary FROM employee5 ORDER BY salary DESC; Sort once from high to low

Sort by multiple columns
SELECT * FROM employee5
ORDER BY hire_date DESC,
salary ASC;

MySQL commonly used storage engine storage

To put it bluntly, the engine is the implementation of technologies such as how to store data, how to index the stored data, and how to update and query data. Because data is stored in the form of a table in a relational database, the storage engine can also be called a table type (that is, the type of storage and operation of this table). There is only one storage engine in databases such as Oracle and SQL Server, and
all The data storage management mechanism is the same. The MySql database provides a variety of storage engines.
Users can choose different storage engines for data tables according to different needs, and users can also write their own storage engines according to their own needs.

MyISAM storage engine

Since this storage engine does not support transactions or foreign keys, access speeds are faster. Therefore, this storage engine is suitable for applications that do not require transaction integrity and are mainly access-based.

InnoDB storage engine

Since this storage engine has advantages in transactions, that is, it supports transaction features such as commit, rollback, and crash recovery capabilities, it takes up more disk space than the MyISAM storage engine.
Therefore, when frequent update and delete operations are required, and high transaction integrity requirements are required, and concurrency control needs to be implemented, it is recommended to choose it. Tables and libraries can be backed up.

MEMORY

The MEMORY storage engine stores data in memory, so access speed is the fastest, but security is not guaranteed. Suitable for temporary tables or temporary tables that require fast access. It cannot be backed up and is not used in the enterprise.

BLACKHOLE

The black hole storage engine can be applied to the distribution master library in primary and secondary replication.

精讲:

1.InnoDB
Starting from mysql version 5.5, innodb is the default storage engine of mysql. InnoDB has the following advantages
: • Good disaster recovery
• Supports transactions. The default transaction isolation level is repeatable read, and its transaction control is achieved through multi-version concurrency control.
• Use row-level locks
• For tables in the InnoDB engine, the physical organization form of the data is a cluster table (Cluster Table), and the data is organized by the primary key. That is to say, the primary key index and the data are together, and the data is in the order of the primary key. physical distribution. Another common form of data table is a non-clustered table, where the indexes are ordered and the data is unordered.
• Implemented buffer management, which not only buffers indexes but also buffers data, and automatically creates hash indexes to speed up data acquisition. In contrast, MyISAM only caches the index.
• Support foreign keys.
• Support hot backup

2.MyISAM引擎
• Can be used with locks to realize copy, backup and migration under the operating system.
• Use table-level locks, poor concurrency.
• Support full-text search (MySQL InonoDB also supports full-text search after 5.6).
• After the host goes down, the MyISAM table is easily damaged and the disaster recovery is poor.
• No transaction support.
• Only indexes are cached, data caching is implemented using operating system buffers. May cause too many system calls and be inefficient.
• Data is stored compactly, resulting in smaller indexes and faster full table scan performance.

3.MEMORY引擎
• The MEMORY storage engine provides "in-memory" tables and does not support transactions or foreign keys.
• Using memory tables (memory engines) can significantly increase the speed of data access, and can be used to cache frequently accessed data, calculation results, statistical values, and intermediate results that can be reconstructed. However, it also has the following shortcomings.
• Table-level locks are used. Although memory access is fast, table-level locks may become a bottleneck if frequent reads and writes occur.
• Only fixed-size rows are supported. VARCHAR type fields will be stored as fixed-length CHAR types, which wastes space.
• TEXT and BLOB fields are not supported. When some queries require the use of temporary tables (which also use the MEMORY storage engine), if there are TEXT and BLOB fields in the table, they will be converted into disk-based MyISAM tables, seriously reducing performance.
• Due to the expensive cost of memory resources, it is generally not recommended to set up an overly large memory table. If the memory table is full, errors like "Thetable'table_name'is full" will be found in the MySQL error log. You can clear the data or adjust Memory table parameters to avoid errors.
• Data will be lost when the server restarts

4.ARCHIVE引擎
• ARCHIVE storage engine is a storage engine designed to store large amounts of running data in enterprises. The ARCHIVE engine uses zlib lossless data compression so that data is stored in compressed archive tables. When data is inserted, they are compressed.
• It only supports INSERT and SELECT, supports auto-increment keys and indexes on them, and does not support other indexes. It is suitable for logging and user behavior analysis, and does not require UPDATE, DELETE and indexed data.

Authorization:

远程连接MySQL
Example:
mysql -h192.168.5.240 -P 3306 -u root -p123 mysql -e 'select user,host from user'
-h specifies the host name [default is localhost]
-P MySQL server port [default 3306]
-u specifies User name [default root]
-p specifies the login password [default is empty password]
here mysql is the specified login database
-e connects to the SQL statement

grant syntax format:

grant permission list on library name.table name to 'username'@'client host' [identified by 'password' with option parameter];

==Permission list all all permissions (excluding authorization permissions)
select,update

==Database.Table name.All tables under all librariesGlobal level
web.* All tables under the web libraryDatabase level
web.stu_info stu_info table under the web libraryTable level
SELECT (col1), INSERT (col1,col2) ON mydb .mytbl Column level

==Client host% All hosts
192.168.2.% All hosts in the 192.168.2.0 network segment
192.168.2.168 Specified host
localhost Specified host

==with_option parameter
GRANT OPTION: Authorization option
MAX_QUERIES_PER_HOUR: Define the number of queries allowed to be executed per hour
MAX_UPDATES_PER_HOUR: Define the number of updates allowed to be executed per hour MAX_CONNECTIONS_PER_HOUR: Define the
number of connections that can be established per hour MAX_USER_CONNECTIONS
: Define the number of connections that a single user can establish at the same time

View authorized permissions
SHOW GRANTS\G
SHOW GRANTS FOR admin1@'%'\G

REVOKE
syntax for recycling permissions:
REVOKE permission list ON database name FROM user name@'client host'

Example:
REVOKE DELETE ON . FROM admin1@'%'; //Recycle some permissions
REVOKE ALL PRIVILEGES ON . FROM admin2@'%'; //Recycle all permissions
REVOKE ALL PRIVILEGES,GRANT OPTION ON . FROM 'admin2'@'% ';

Guess you like

Origin blog.csdn.net/weixin_42602433/article/details/86505424