MySQL多实例部署
什么是MySQL多实例
简单来说,MySQL多实例就是在一台服务器上同时开启多个不同服务端口,同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务器端口来提供服务。这些MySQL多实例共用一套MySQL安装程序,使用不同的my.cnf(也可以相同)配置文件,启动程序和数据文件。在提供服务时,多实例MySQL在逻辑上看起来是各自独立的,它根据配置文件的对应设定值,获得服务器相应数量的硬件资源。
多实例的好处与弊端
它能有效利用服务器的资源,节约服务器资源,弊端会存在资源的相互抢占的问题。当某个数据库实例并发很高或有SQL慢查询时,整个实例会消耗大量的系统CPU、磁盘I/O等资源,导致服务器上的其他数据库实例提供服务的质量一起下降。不同实例获取的资源是相对独立的,无法像虚拟化一样完全隔离。
二进制安装mysql
下载安装
[root@happy ~]# dnf install -y wget vim
[root@happy ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
创建用户(会自动生成组)
[root@happy ~]# useradd -r -M -s /sbin/nologin mysql
解压软件至/usr/local/
[root@happy ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local
修改目录名
[root@happy ~]# cd /usr/local
[root@happy local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@happy local]# ls
bin etc games include lib lib64 libexec mysql sbin share src
修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月 18 2020 bin
drwxr-xr-x. 2 root root 6 5月 18 2020 etc
drwxr-xr-x. 2 root root 6 5月 18 2020 games
drwxr-xr-x. 2 root root 6 5月 18 2020 include
drwxr-xr-x. 2 root root 6 5月 18 2020 lib
drwxr-xr-x. 3 root root 17 3月 29 13:05 lib64
drwxr-xr-x. 2 root root 6 5月 18 2020 libexec
drwxr-xr-x. 9 mysql mysql 129 5月 6 00:47 mysql
drwxr-xr-x. 2 root root 6 5月 18 2020 sbin
drwxr-xr-x. 5 root root 49 3月 29 13:05 share
drwxr-xr-x. 2 root root 6 5月 18 2020 src
配置环境变量
[root@happy ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@happy ~]# . /etc/profile.d/mysql.sh
[root@happy ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
创建存放各个实例的目录
[root@happy ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@happy ~]# chown -R mysql.mysql /opt/data
[root@happy ~]# ll /opt/data/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 5月 10 02:38 3306
drwxr-xr-x. 2 mysql mysql 6 5月 10 02:38 3307
drwxr-xr-x. 2 mysql mysql 6 5月 10 02:38 3308
初始化各个实例并保存实例的临时密码
[root@happy ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2021-05-10T07:41:45.811898Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T07:41:46.028220Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T07:41:46.073801Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T07:41:46.135009Z 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: 2be6b26e-b163-11eb-88d9-000c296364bb.
2021-05-10T07:41:46.135824Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T07:41:46.698230Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T07:41:46.770359Z 1 [Note] A temporary password is generated for root@localhost: y17:gBVJ28mh
[root@happy ~]# echo 'y17:gBVJ28mh' > 3306_pass
[root@happy ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2021-05-10T07:42:21.701637Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T07:42:21.858134Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T07:42:21.885342Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T07:42:21.890303Z 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: 413685b4-b163-11eb-b9fe-000c296364bb.
2021-05-10T07:42:21.890963Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T07:42:22.597633Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T07:42:22.684269Z 1 [Note] A temporary password is generated for root@localhost: 1SfffpO/PeaM
[root@happy ~]# echo '1SfffpO/PeaM' > 3307_pass
[root@happy ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2021-05-10T07:42:46.923618Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-10T07:42:47.104160Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-10T07:42:47.129015Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-10T07:42:47.183785Z 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: 504a0058-b163-11eb-9d92-000c296364bb.
2021-05-10T07:42:47.184644Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-10T07:42:47.510426Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-10T07:42:47.648299Z 1 [Note] A temporary password is generated for root@localhost: BROe)dDPi0f=
[root@happy ~]# echo 'BROe)dDPi0f=' > 3308_pass
安装perl工具
[root@happy ~]# dnf install -y perl
配置配置文件/etc/my.cnf
[root@happy ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[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@happy ~]# mysqld_multi start 3306
[root@happy ~]# mysqld_multi start 3307
[root@happy ~]# mysqld_multi start 3308
[root@happy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
使用临时密码进入实例并修改密码登录
[root@happy ~]# mysql -uroot -p'y17:gBVJ28mh' -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 2
Server version: 5.7.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('yzy123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
[root@happy ~]# mysql -uroot -p'yzy123!' -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.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> quit
[root@happy ~]# mysql -uroot -p'1SfffpO/PeaM' -S /tmp/mysql3307.sock -e 'set password = password("wangqing123!");' --connect-expired-password
[root@happy ~]# mysql -uroot -p'wangqing123!' -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 3
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> quit
[root@happy ~]# mysql -uroot -p'BROe)dDPi0f=' -S /tmp/mysql3308.sock -e 'set password = password("yyy123!");' --connect-expired-password
[root@happy ~]# mysql -uroot -p'yyy123!' -S /tmp/mysql3308.sockmysql: [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.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> quit
开机自启多实例服务
查看进程 确保多实例是关着的
[root@happy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
修改配置文件
[root@happy ~]# ls /usr/local/mysql/support-files/
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@happy ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysql_multi
[root@happy ~]# vim /etc/init.d/mysql_multi
......
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
export PATH=$bindir:$PATH //这一条是添加的
if test -x $bindir/mysqld_multi
then
mysqld_multi="$bindir/mysqld_multi";
else
echo "Can't execute $bindir/mysqld_multi from dir $basedir";
exit;
fi
case "$1" in
'start' )
"$mysqld_multi" start $2
;;
'stop' )
"$mysqld_multi" stop $2
;;
'report' )
......
现在就可以使用这个命令启动多实例了
[root@happy ~]# service mysql_multi start 3306
[root@happy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@happy ~]# service mysql_multi start 3307
[root@happy ~]# service mysql_multi start 3308
[root@happy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
设置开机自启并检验
[root@happy ~]# chkconfig mysql_multi on
[root@happy ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysql_multi 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@happy ~]# reboot
连接断开
连接成功
Last login: Mon May 10 06:48:02 2021 from 192.168.10.88
[root@happy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*