I have done a lot of practical installations before, the multi-instance installation and shutdown of mysql, but the version at that time was 5.6. Since the installation methods of 5.7 and 5.6 mysql are still different, today I will test the installation of mysql5.7 multi-instances .
Environment Description:
os: rhel6.3 64bit
mysql: 5.7.21
An instance already exists:
root 2564 2538 0 23:31 pts/1 00:00:00 grep mysql [root@qht131 ~]# ps -ef | grep mysql root 1533 1 0 23:28 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/u01/mysql --pid-file=/u01/mysql/mysqld.pid mysql 1880 1533 0 23:28 ? 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/u01/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/u01/log/mysql/mysql_3306.err --open-files-limit=8192 --pid-file=/u01/mysql/mysqld.pid --socket=/u01/mysql/mysql.sock root 2566 2538 0 23:31 pts/1 00:00:00 grep mysql
1. Modify my.cnf
[root@qht131 mysql]# cat /etc/my.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld mysqladmin = /usr/local/mysql/bin/mysqladmin log = /u01/log/mysql_multi.log [mysqld1] character_set_server= utf8 init_connect= 'SET NAMES utf8' basedir= /usr/local/mysql datadir= /u01/mysql socket = /u01/mysql/mysql.sock user=mysql #You must start mysql with the mysql account, avoid using root to start port=3306 performance_schema=off innodb_buffer_pool_size=32M bind_address=0.0.0.0 skip-name-resolve=0 [mysqld2] character_set_server= utf8 init_connect= 'SET NAMES utf8' basedir= /usr/local/mysql datadir= /u01/mysql2 socket = /u01/mysql2/mysql.sock user=mysql port=3307 performance_schema=off innodb_buffer_pool_size=32M bind_address=0.0.0.0 skip-name-resolve=0 character_set_server= utf8 init_connect= 'SET NAMES utf8' basedir= /usr/local/mysql datadir= /u01/mysql3 socket = /u01/mysql3/mysql.sock user=mysql port=3308 performance_schema=off innodb_buffer_pool_size=32M bind_address=0.0.0.0 skip-name-resolve=0
2. Create the required directory:
[root@qht131 mysql]# mkdir -p /u01/mysql2 [root@qht131 mysql]# mkdir -p /u01/log/mysql2 [root@qht131 mysql]# mkdir -p /u01/mysql3 [root@qht131 mysql]# mkdir -p /u01/log/mysql3 [root@qht131 mysql]# chown -R mysql.mysql /u01
3. Initialize each instance and open the SSL connection:
[root@qht131 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/u01/mysql2
2018-05-04T22:32:19.567383Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-05-04T22:32:20.937886Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-05-04T22:32:21.154932Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-05-04T22:32:21.244128Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 027cc829-4feb-11e8-9103-000c29aa0615. 2018-05-04T22:32:21.246035Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-05-04T22:32:21.246980Z 1 [Note] A temporary password is generated for root@localhost: vd539qdjd#,B
[root@qht131 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/u01/mysql3
2018-05-04T22:33:34.514887Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-05-04T22:33:35.948344Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-05-04T22:33:36.150828Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-05-04T22:33:36.217487Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2f2ccf35-4feb-11e8-9402-000c29aa0615. 2018-05-04T22:33:36.221292Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-05-04T22:33:36.227321Z 1 [Note] A temporary password is generated for root@localhost: RcwAsh&A%9mr
[root@qht131 mysql]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/u01/mysql2 Generating a 2048 bit RSA private key .............................................................+++ .....+++ writing new private key to 'ca-key.pem' ----- Generating a 2048 bit RSA private key .......................+++ ................+++ writing new private key to 'server-key.pem' ----- Generating a 2048 bit RSA private key ....+++ ..........................+++ writing new private key to 'client-key.pem' ----- [root@qht131 mysql]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/u01/mysql3 Generating a 2048 bit RSA private key ..............................................+++ ...........................+++ writing new private key to 'ca-key.pem' ----- Generating a 2048 bit RSA private key ...................................................................................................................+++ ..+++ writing new private key to 'server-key.pem' ----- Generating a 2048 bit RSA private key ..+++ ..+++ writing new private key to 'client-key.pem' -----
4. Configure automatic startup
[root@qht131 mysql]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi [root@qht131 mysql]# chmod +x /etc/init.d/mysqld_multi [root@qht131 mysql]# chkconfig --add mysqld_multi
5. Start all instances
[root@qht131 mysql]# /etc/init.d/mysqld_multi start [root@qht131 mysql]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running
[root@qht131 mysql]# netstat -lntp | grep mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6800/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 6803/mysqld tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 6806/mysqld
Started successfully!
6. Remember to modify the root password of the two newly added instances first:
[root@qht131 mysql2]# mysql -S /u01/mysql2/mysql.sock -uroot -pvd539qdjd#,B mysql> set password=password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
[root@qht131 mysql2]# mysql -S /u01/mysql2/mysql.sock -uroot -pRcwAsh&A%9mr mysql> set password=password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
7. Then try to see if it can be closed normally
[root@qht131 mysql]# /etc/init.d/mysqld_multi stop
[root@qht131 mysql]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running
If the shutdown is not successful, check the Log of the multi-instance:
[root@qht131 log]# tail -f mysql_multi.log 2018-05-04T21:52:32.951509Z 10 [Note] Access denied for user 'root'@'localhost' (using password: NO) mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' 2018-05-04T21:52:32.958002Z 10 [Note] Access denied for user 'root'@'localhost' (using password: NO) mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' 2018-05-04T21:52:32.965020Z 10 [Note] Access denied for user 'root'@'localhost' (using password: NO) mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'
It is found that mysqladmin cannot connect to the database, add user and password to /etc/my.cnf and try:
[mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld mysqladmin = /usr/local/mysql/bin/mysqladmin log = /u01/log/mysql_multi.log user=root #Added user and password password=123456
After adding the user name and password, it still does not work. The reason is that mysqld_multi reads the parameters of the instance by calling my_print_defaults. The reading results are as follows:
[root@qht131 mysql2]# my_print_defaults mysqld_multi mysql1 --mysqld=/usr/local/mysql/bin/mysqld --mysqladmin=/usr/local/mysql/bin/mysqladmin --log=/u01/log//mysql_multi.log [root@qht131 mysql2]# vi /etc/my.cnf [root@qht131 mysql2]# /etc/init.d/mysqld_multi stop [root@qht131 mysql2]# my_print_defaults mysqld_multi mysql1 --mysqld=/usr/local/mysql/bin/mysqld --mysqladmin=/usr/local/mysql/bin/mysqladmin --log=/u01/log/mysql_multi.log --user=root --password=*****
Since the read password is encrypted, it is naturally impossible to log in. The solution is to modify the mysqld_multi script:
Then close the database again and succeed:
[root@qht131 bin]# /etc/init.d/mysqld_multi stop [root@qht131 bin]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running MySQL server from group: mysqld3 is not running
Reference: https://blog.csdn.net/zhengwei125/article/details/52413835
8. Turn a single instance on and off:
If you start the first instance with Port 3306:
[root@qht131 u01]# mysqld_multi start 1 [root@qht131 u01]# mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is not running MySQL server from group: mysqld3 is not running
[root@qht131 u01]# ps -ef | grep mysqld mysql 12945 1 4 17:49 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --character_set_server=utf8 --init_connect=SET NAMES utf8 --basedir=/usr/local/mysql --datadir=/u01/mysql --socket=/u01/mysql/mysql.sock --user=mysql --port=3306 --performance_schema=off --innodb_buffer_pool_size=32M --bind_address=0.0.0.0 --skip-name-resolve=0 --log-error=/u01/log/mysql/mysql_3306.err root 12975 2781 0 17:49 pts/2 00:00:00 grep mysqld
In my case GNR is 1,2,3. So it is enough to specify 1, 2, and 3 when a single startup is started. The official documentation states the following:
Each value represents an option group number or range of group numbers. The value should be the number at the end of the group name in the option file. For example, the for a group named is . GNR
GNR
[mysqld17]
17
The same is true for closing:
[root@qht131 u01]# mysqld_multi stop 1 [root@qht131 u01]# mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running MySQL server from group: mysqld3 is not running
It can also be closed with mysqladmin
[root@qht131 u01]# mysqladmin -uroot -p -S /u01/mysql/mysql.sock shutdown
9. To connect a single instance, connect the instance by specifying the sock file of the instance:
[root@qht131 u01]# mysql -uroot -p -S /u01/mysql/mysql.sock
[root@qht131 u01]# mysql -uroot -p -S /u01/mysql2/mysql.sock
[root@qht131 u01]# mysql -uroot -p -S /u01/mysql3/mysql.sock