mysqld_multi多实例部署

Posted Windy Tracel Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysqld_multi多实例部署相关的知识,希望对你有一定的参考价值。

mysql多实例部署

下载软件

[root@localhost ~]# ls
anaconda-ks.cfg  mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
#安装perl
[root@localhost ~]# yum -y install perl

配置用户和组并解压二进制程序至/usr/local下

#创建用户和组
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g mysql mysql

#解压mysql到/usr/local/
[root@localhost ~]# tar xf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.29-linux-glibc2.12-x86_64  share
[root@localhost local]# ln -s mysql-5.7.29-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
lrwxrwxrwx. 1 root root  36 Jun 18 22:20 mysql -> mysql-5.7.29-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jun 18 22:20 mysql-5.7.29-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Apr  7 04:48 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

#修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll -d /usr/local/mysql 
lrwxrwxrwx. 1 mysql mysql 36 Jun 18 22:20 /usr/local/mysql -> mysql-5.7.29-linux-glibc2.12-x86_64

#配置环境变量
[root@localhost ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

创建实例数据存放的目录

[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/

初始化各实例

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/opt/data/3306/ --user=mysql
2020-06-19T02:26:51.119557Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-19T02:26:51.289336Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-19T02:26:51.317903Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-19T02:26:51.373831Z 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: 557e31d8-b1d4-11ea-8616-000c29d7d941.
2020-06-19T02:26:51.374438Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2020-06-19T02:26:51.639794Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-19T02:26:51.679383Z 1 [Note] A temporary password is generated for root@localhost: rs3+GHrW/qo)
[root@localhost ~]# echo ‘rs3+GHrW/qo)‘ > 3306_pass


[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/opt/data/3307/ --user=mysql
2020-06-19T02:28:04.740186Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-19T02:28:04.905282Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-19T02:28:04.934119Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-19T02:28:04.988177Z 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: 815eda45-b1d4-11ea-883b-000c29d7d941.
2020-06-19T02:28:04.988744Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2020-06-19T02:28:05.572008Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-19T02:28:05.629436Z 1 [Note] A temporary password is generated for root@localhost: Zf&stNghV6Kl
[root@localhost ~]# echo ‘Zf&stNghV6Kl‘ > 3307_pass


[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/opt/data/3308/ --user=mysql
2020-06-19T02:28:50.562599Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-19T02:28:50.724362Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-19T02:28:50.751944Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-19T02:28:50.806578Z 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: 9cae2f9d-b1d4-11ea-8b25-000c29d7d941.
2020-06-19T02:28:50.807144Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2020-06-19T02:28:51.476355Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-19T02:28:51.808583Z 1 [Note] A temporary password is generated for root@localhost: 5fh/s=!924IX
[root@localhost ~]# echo ‘5fh/s=!924IX‘ > 3308_pass

配置配置文件/etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user       = multi_admin
password   = my_password

[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log

[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log

[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

启动各实例

[root@localhost ~]# /usr/local/mysql/bin/mysqld_multi start 3306
[root@localhost ~]# /usr/local/mysql/bin/mysqld_multi start 3307
[root@localhost ~]# /usr/local/mysql/bin/mysqld_multi start 3308
[root@localhost ~]# /usr/local/mysql/bin/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld3306 is running
MySQL server from group: mysqld3307 is running
MySQL server from group: mysqld3308 is running
[root@localhost ~]# ss -tanl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      100               [::1]:25                             [::]:*                  
LISTEN     0      80                 [::]:3306                           [::]:*                  
LISTEN     0      80                 [::]:3307                           [::]:*                  
LISTEN     0      80                 [::]:3308                           [::]:*                  
LISTEN     0      128                [::]:22                             [::]:*  

初始化密码

[root@localhost ~]# ls
3306_pass  3307_pass  3308_pass  anaconda-ks.cfg  mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# cat 3306_pass 
rs3+GHrW/qo)
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p‘rs3+GHrW/qo)‘ -S /tmp/mysql3306.sock
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 3
Server version: 5.7.29

Copyright (c) 2000, 2020, 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> quit
Bye



[root@localhost ~]# cat 3307_pass 
Zf&stNghV6Kl
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p‘Zf&stNghV6Kl‘ -S /tmp/mysql3307.sock --connect-expired-password -e ‘set password=password("qwer!@#$")‘
mysql: [Warning] Using a password on the command line interface can be insecure.



[root@localhost ~]# cat 3308_pass 
5fh/s=!924IX
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p‘5fh/s=!924IX‘ -S /tmp/mysql3308.sock --connect-expired-password -e ‘set password=password("qwer!@#$")‘
mysql: [Warning] Using a password on the command line interface can be insecure.

关闭实例

#关闭3307实例
//需要在数据库实例上创建一个账户用来关闭数据库,该账户的用户名和密码和配置文件中[mysqld_multi] 中配置的一样
[root@localhost ~]# mysql -uroot -p‘qwer!@#$‘ -S /tmp/mysql3307.sock
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 11
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> GRANT SHUTDOWN ON *.* TO ‘multi_admin‘@‘localhost‘ IDENTIFIED BY ‘my_password‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#复制222行注释,在原本的defaults后加上-s
[root@localhost ~]# vim /usr/local/mysql/bin/mysqld_multi
# my $com= join ‘ ‘, ‘my_print_defaults‘, @defaults_options, $group;
  my $com= join ‘ ‘, ‘my_print_defaults -s‘, @defaults_options, $group;

#关闭3307实例
[root@localhost ~]# mysqld_multi stop 3307
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld3306 is running
MySQL server from group: mysqld3307 is not running
MySQL server from group: mysqld3308 is running
[root@localhost ~]# ss -tanl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      100               [::1]:25                             [::]:*                  
LISTEN     0      80                 [::]:3306                           [::]:*                  
LISTEN     0      80                 [::]:3308                           [::]:*                  
LISTEN     0      128                [::]:22                             [::]:*                  

#再启用3307实例
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld3306 is running
MySQL server from group: mysqld3307 is running
MySQL server from group: mysqld3308 is running
[root@localhost ~]# ss -tanl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      100               [::1]:25                             [::]:*                  
LISTEN     0      80                 [::]:3306                           [::]:*                  
LISTEN     0      80                 [::]:3307                           [::]:*                  
LISTEN     0      80                 [::]:3308                           [::]:*                  
LISTEN     0      128                [::]:22                             [::]:* 

以上是关于mysqld_multi多实例部署的主要内容,如果未能解决你的问题,请参考以下文章

mysqld_multi 多实例启动工具

MySQL5.5多实例编译安装——mysqld_multi

二进制包中mysqld_multi实现单主机多实例及slave

MySQL多实例配置

msyql多实例部署

基于mysqld_multi实现MySQL 5.7.24多实例多进程配置