mysql多实例部署
多实例就是在一台服务器上同时开启多个不同的数据库服务端口(例如3306、3307),同时运行多个MYSQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。
软件下载
优点:
MySQL多实例,可以通过多个端口向用户提供服务,充分利用一台服务器的闲置资源
缺点:
无论是多少个端口提供服务,但始终使用的是一台服务器的资源,所以当访问量过大时,依然存在高并发的问题
//下载二进制格式的mysql软件包
[root@yxt ~]# cd /usr/src/
[root@yxt src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@yxt src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
//配置用户和组并解压二进制程序至/usr/local下
[root@yxt src]# groupadd -r mysql
[root@yxt src]# useradd -M -s /sbin/nologin -g mysql mysql
//解压软件至/usr/local/
[root@yxt src]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@yxt src]# ls /usr/local/
apache bin include libexec sbin
apr etc lib mysql-5.7.38-linux-glibc2.12-x86_64 share
apr-util games lib64 pcre
[root@yxt src]# cd /usr/local/
[root@yxt local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'[root@yxt local]# ls
apache etc lib64 pcre
apr games libexec sbin
apr-util include mysql share
bin lib mysql-5.7.38-linux-glibc2.12-x86_64 src
//修改目录/usr/local/mysql的属主属组
[root@yxt local]# chown -R mysql:mysql /usr/local/mysql
[root@yxt local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Jul 30 18:09 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
//添加环境变量
[root@yxt local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh
[root@yxt local]# source /etc/profile.d/myslq.sh
[root@yxt local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/apache/bin/:/root/bin
//头文件
[root@yxt local]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
//库文件
[root@yxt local]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@yxt local]# ldconfig
//man文档
[root@yxt local]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
创建各实例数据存放的目录
(这里可能会报错,如果报错,就删除/opt/data/的所有东西,在执行初始化数据库,一次不行,就多试几遍)
[root@yxt ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@yxt ~]# chown -R mysql:mysql /opt/data
[root@yxt ~]# ll /opt/data
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 30 18:20 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 30 18:20 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 30 18:20 3308
[root@yxt ~]# yum -y install tree
[root@yxt ~]# tree /opt/data
/opt/data
??€ 3306
??€ 3307
??€ 3308
初始化各实例
[root@yxt ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-30T11:47:34.228678Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-30T11:47:34.700465Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-30T11:47:34.745245Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-30T11:47:34.802834Z 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: 67074e9a-0ffd-11ed-ab81-000c2922f929.
2022-07-30T11:47:34.804285Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-30T11:47:35.395589Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:47:35.395614Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:47:35.396430Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-30T11:47:35.469260Z 1 [Note] A temporary password is generated for root@localhost: r4,O4b</p6ed
[root@yxt ~]# echo 'r4,O4b</p6ed' > 3306_pass
[root@yxt ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-30T11:48:18.627377Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-30T11:48:18.897686Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-30T11:48:18.940571Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-30T11:48:18.999510Z 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: 815f2fdc-0ffd-11ed-af2d-000c2922f929.
2022-07-30T11:48:19.001040Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-30T11:48:19.328351Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:48:19.328367Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:48:19.328766Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-30T11:48:19.453303Z 1 [Note] A temporary password is generated for root@localhost: vZ?d<QMJq1jt
[root@yxt ~]# echo 'vZ?d<QMJq1jt' > 3307_pass
[root@yxt ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-30T11:48:44.493873Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-30T11:48:44.795568Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-30T11:48:44.887294Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-30T11:48:44.945178Z 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: 90d62e1d-0ffd-11ed-af36-000c2922f929.
2022-07-30T11:48:44.946691Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-30T11:48:45.147796Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:48:45.147814Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-30T11:48:45.148426Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-30T11:48:45.170413Z 1 [Note] A temporary password is generated for root@localhost: g;WJ=fE=+1aq
[root@yxt ~]# echo 'g;WJ=fE=+1aq' > 3308_pass
安装perl
[root@yxt data]# yum -y install perl
配置配置文件/etc/my.cnf
[root@yxt data]# 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@yxt ~]# mysqld_multi start 3306
[root@yxt ~]# mysqld_multi start 3307
[root@yxt ~]# mysqld_multi start 3308
[root@yxt ~]# 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 [::]:*
使用systemctl服务控制mysql多实例
[root@yxt ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3308.service
[root@yxt ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3307.service
[root@yxt ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service
[root@yxt ~]# vim /usr/lib/systemd/system/3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
ExecStop=ps -ef | grep 3306 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@yxt ~]# vim /usr/lib/systemd/system/3307.service
[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307
ExecStop=ps -ef | grep 3307 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@yxt ~]# vim /usr/lib/systemd/system/3308.service
[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308
ExecStop=ps -ef | grep 3308 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@yxt ~]# systemctl daemon-reload
[root@yxt ~]# systemctl stop firewalld
[root@yxt ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@yxt ~]# vim /etc/selinux/config //修改selinux
SELINUX=disabled
[root@yxt ~]# setenforce 0
//特别注意:先前使用mysqld_multi启动的,只有先杀死3306、3307和3308的MySQL服务进程之后,使用systemctl服务开启或者再停止
[root@yxt ~]# systemctl start 3306
[root@yxt ~]# systemctl start 3307
[root@yxt ~]# systemctl start 3308
[root@yxt ~]# ss -anlt
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@yxt ~]# systemctl stop 3306
[root@yxt ~]# systemctl stop 3307
[root@yxt ~]# systemctl stop 3308
[root@yxt ~]# ss -anlt
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@yxt 3306]# yum -y install ncurses-compat-libs
[root@yxt ~]# mysql -uroot -p'r4,O4b</p6ed' -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.38
Copyright (c) 2000, 2022, 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 ('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit;
Bye
[root@yxt ~]# mysql -uroot -p'vZ?d<QMJq1jt' -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 2
Server version: 5.7.38
Copyright (c) 2000, 2022, 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 ('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit;
Bye
[root@yxt ~]# cat 3308_pass
g;WJ=fE=+1aq
[root@yxt ~]# mysql -uroot -p'g;WJ=fE=+1aq' -S /tmp/mysql3308.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.38
Copyright (c) 2000, 2022, 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 ('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit;
Bye