mysql多实例部署
软件下载
[root@z1 ~]# cd /usr/src/
[root@z1 src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
配置用户和组并解压二进制程序至/usr/local下
//创建用户和组
[root@z1 src]# useradd -M -r -s /sbin/nologin mysql
[root@z1 src]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
//解压软件至/usr/local/
[root@z1 src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@z1 src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@z1 src]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.38-linux-glibc2.12-x86_64 share
[root@z1 local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
[root@z1 local]# ll
total 0
drwxr-xr-x. 2 root root 6 May 19 2020 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jul 31 21:23 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 root root 36 Aug 1 00:14 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Aug 1 00:12 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 5 root root 49 Jul 31 21:23 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
//修改目录/usr/local/mysql的属主属组
[root@z1 local]# chown -R mysql.mysql /usr/local/mysql*
[root@z1 local]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 May 19 2020 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jul 31 21:23 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 mysql mysql 36 Aug 1 00:14 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Aug 1 00:12 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 5 root root 49 Jul 31 21:23 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
//配置环境变量
[root@z1 local]# cd /usr/local/mysql
[root@z1 mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@z1 mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@z1 mysql]# source /etc/profile.d/mysql.sh
[root@z1 mysql]# which mysql
/usr/local/mysql/bin/mysql
[root@z1 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@z1 mysql]# vi /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@z1 mysql]# ldconfig
[root@z1 mysql]# vi /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man //添加这行
创建各实例数据存放的目录
[root@z1 ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@z1 ~]# chown -R mysql.mysql /opt/data/
[root@z1 ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 1 00:26 3306
drwxr-xr-x. 2 mysql mysql 6 Aug 1 00:26 3307
drwxr-xr-x. 2 mysql mysql 6 Aug 1 00:26 3308
初始化各实例
//初始化3306实例
[root@z1 ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-31T16:28:02.306319Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T16:28:02.452018Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T16:28:02.506813Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T16:28:02.572065Z 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: bf92ddf2-10ed-11ed-9147-000c29b27139.
2022-07-31T16:28:02.572730Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T16:28:02.734975Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:28:02.734989Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:28:02.735326Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T16:28:02.886179Z 1 [Note] A temporary password is generated for root@localhost: .j&?rO>v2VtF
[root@z1 ~]# echo '.j&?rO>v2VtF' > 3306_pass
//初始化3307实例
[root@z1 ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-31T16:29:39.103661Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T16:29:39.232130Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T16:29:39.254861Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T16:29:39.309807Z 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: f93be026-10ed-11ed-94ca-000c29b27139.
2022-07-31T16:29:39.311601Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T16:29:39.750820Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:29:39.750837Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:29:39.751175Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T16:29:39.802821Z 1 [Note] A temporary password is generated for root@localhost: G+f3(2Tlgacy
[root@z1 ~]# echo 'G+f3(2Tlgacy' > 3307_pass
//初始化3308实例
[root@z1 ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-31T16:31:23.732406Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T16:31:23.846637Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T16:31:23.868316Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T16:31:23.926400Z 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: 37971a57-10ee-11ed-98a4-000c29b27139.
2022-07-31T16:31:23.927317Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T16:31:24.148033Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:31:24.148047Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T16:31:24.148364Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T16:31:24.206643Z 1 [Note] A temporary password is generated for root@localhost: Ui5qO(j=nVFs
[root@z1 ~]# echo 'Ui5qO(j=nVFs' > 3308_pass
配置配置文件/etc/my.cnf
[root@z1 ~]# vi /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
安装perl
[root@z1 ~]# yum -y install perl
python-rpm-macros-3-41.el8.noarch
python-srpm-macros-3-41.el8.noarch
python3-pyparsing-2.1.10-7.el8.noarch
python3-rpm-macros-3-41.el8.noarch
qt5-srpm-macros-5.15.2-1.el8.noarch
redhat-rpm-config-125-1.el8.noarch
rust-srpm-macros-5-2.el8.noarch
systemtap-sdt-devel-4.5-3.el8.x86_64
unzip-6.0-45.el8_4.x86_64
zip-3.0-23.el8.x86_64
启动各实例
[root@z1 ~]# mysqld_multi start 3306
[root@z1 ~]# mysqld_multi start 330
[root@z1 ~]# mysqld_multi start 3307
[root@z1 ~]# mysqld_multi start 3308
[root@z1 ~]# 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@z1 ~]# ps -ef | grep 3306 | grep -v grep |awk '{print$2}' |xargs kill -9
[root@z1 ~]# 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 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
修改开启停止命令
[root@z1 ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service
[root@z1 ~]# vi /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@z1 ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service
cp: overwrite '/usr/lib/systemd/system/3308.service'? y
[root@z1 ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service
cp: overwrite '/usr/lib/systemd/system/3307.service'? y
[root@z1 ~]# vi /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@z1 ~]# vi /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@z1 ~]# systemctl start 3306
Job for 3306.service failed because the control process exited with error code.
See "systemctl status 3306.service" and "journalctl -xe" for details.
[root@z1 ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
[root@z1 ~]# systemctl start 3306
[root@z1 ~]# 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@z1 ~]# systemctl stop 3307
[root@z1 ~]# 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 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@z1 ~]# systemctl stop 3308
[root@z1 ~]# 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 128 [::]:22 [::]:*
初始化密码
[root@z1 ~]# cat 3306_pass
.j&?rO>v2VtF
[root@z1 ~]# mysql -uroot -p'.j&?rO>v2VtF' -S /tmp/mysql3306.sock
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@z1 ~]# yum -y install ncurses-compat-libs
Upgraded:
ncurses-6.1-9.20180224.el8.x86_64
ncurses-base-6.1-9.20180224.el8.noarch
ncurses-libs-6.1-9.20180224.el8.x86_64
Installed:
ncurses-compat-libs-6.1-9.20180224.el8.x86_64
Complete!
[root@z1 ~]# mysql -uroot -p'.j&?rO>v2VtF' -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('3306');
Query OK, 0 rows affected, 1 warning (0.01 sec)
[root@z1 ~]# mysql -uroot -p'G+f3(2Tlgacy' -S /tmp/mysql3307.sock -e 'set password = password("3307");' --connect-expired-password
[root@z1 ~]# mysql -uroot -p'Ui5qO(j=nVFs' -S /tmp/mysql3308.sock -e 'set password = password("3308");' --connect-expired-password
浙公网安备 33010602011771号