mysql多实例部署
软件下载
//下载二进制格式的mysql软件包
[root@cl ~]# cd /usr/src/
[root@cl src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@cl src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
配置用户和组并解压二进制程序至/usr/local下
//创建用户和组
[root@cl src]# groupadd -r mysql
[root@cl src]# useradd -M -s /sbin/nologin -g mysql mysql
//解压软件至/usr/local/
[root@cl src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@cl src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@cl src]# ls /usr/local/
apache2.4 bin include libexec share
apr etc lib mysql-5.7.38-linux-glibc2.12-x86_64 src
apr-util games lib64 sbin
[root@cl src]# cd /usr/local/
[root@cl local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
[root@cl local]# ll
total 0
drwxr-xr-x. 14 root root 164 Jul 12 23:36 apache2.4
drwxr-xr-x. 6 root root 58 Jul 12 23:01 apr
drwxr-xr-x. 5 root root 43 Jul 12 23:34 apr-util
drwxr-xr-x. 2 root root 18 Jul 7 10:03 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Jun 27 21:06 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 36 Jul 31 17:21 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 31 17:20 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 6 root root 64 Jul 7 10:03 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
//修改目录/usr/local/mysql的属主属组
[root@cl local]# chown -R mysql:mysql /usr/local/mysql
[root@cl local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Jul 31 17:21 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
//配置环境变量
[root@cl local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh
[root@cl local]# source /etc/profile.d/myslq.sh
[root@cl local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/httpd/bin:/root/bin
//头文件
[root@cl local]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@cl local]# ldconfig
//库文件
[root@cl local]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@cl local]# ldconfig
//man文档
[root@cl local]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
创建各实例数据存放的目录
[root@cl ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@cl ~]# chown -R mysql:mysql /opt/data
[root@cl ~]# ll /opt/data
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 31 17:32 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 31 17:32 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 31 17:32 3308
[root@cl ~]# tree /opt/data/
/opt/data/
├── 3306
├── 3307
└── 3308
3 directories, 0 files
初始化各实例
//初始化3306实例
[root@cl ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-31T09:34:27.026920Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T09:34:27.162221Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T09:34:27.190508Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T09:34:27.263944Z 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: f87f587b-10b3-11ed-8de6-000c29791a87.
2022-07-31T09:34:27.266928Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T09:34:27.710492Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:34:27.710516Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:34:27.710879Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T09:34:27.741306Z 1 [Note] A temporary password is generated for root@localhost: +Iefik6lWpvc
[root@cl ~]# echo '+Iefik6lWpvc' > 3306_pass
[root@cl ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-31T09:37:07.608962Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T09:37:07.742972Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T09:37:07.764573Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T09:37:07.768312Z 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: 582a5f64-10b4-11ed-926a-000c29791a87.
2022-07-31T09:37:07.768830Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T09:37:08.076184Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:37:08.076210Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:37:08.076591Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T09:37:08.126051Z 1 [Note] A temporary password is generated for root@localhost: (hNouIrep6>C
[root@cl ~]# echo '(hNouIrep6>C' > 3307_pass
[root@cl ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-31T09:37:54.471329Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T09:37:54.616231Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T09:37:54.638579Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T09:37:54.643016Z 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: 741ae28d-10b4-11ed-9546-000c29791a87.
2022-07-31T09:37:54.643444Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T09:37:55.217290Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:37:55.217316Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T09:37:55.217653Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T09:37:55.257434Z 1 [Note] A temporary password is generated for root@localhost: +P:W&d>Pb61s
[root@cl ~]# echo '+P:W&d>Pb61s' > 3308_pass
安装perl
[root@cl ~]# yum -y install perl
配置配置文件/etc/my.cnf
[root@cl ~]# 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@cl ~]# mysqld_multi start 3306
[root@cl ~]# mysqld_multi start 3307
[root@cl ~]# mysqld_multi start 3308
[root@cl ~]# 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@cl ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service
[root@cl ~]# 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@cl ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service
[root@cl ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service
[root@cl ~]# 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@cl ~]# 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@cl ~]# systemctl daemon-reload
[root@cl ~]# systemctl stop firewalld
[root@cl ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cl ~]# vim /etc/selinux/config
SELINUX=disabled
[root@cl ~]# setenforce 0
初始化密码
[root@cl ~]# mysql -uroot -p'+Iefik6lWpvc' -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> exit
Bye
[root@cl ~]# cat 3307_pass
(hNouIrep6>C
[root@cl ~]# mysql -uroot -p'(hNouIrep6>C' -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> exit
Bye
[root@cl ~]# cat 3308_pass
+P:W&d>Pb61s
[root@cl ~]# mysql -uroot -p'+P:W&d>Pb61s' -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> exit
Bye