49.Mysql多实例部署
Mysql多实例部署
配置网络yum源
[root@xieyuhui yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@xieyuhui yum.repos.d]# yum install epel-release -y
将软件包解压至/usr/local
[root@xieyuhui ~]# tar -xzvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
创建mysql的用户和组
[root@xieyuhui ~]# useradd -M -s /sbin/nologin -g mysql mysql
创建软连接
[root@xieyuhui ~]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
修改/usr/local/mysql的所有者和所属组
[root@xieyuhui local]# chown -R mysql.mysql /usr/local/
给mysql配置环境变量
[root@xieyuhui ~]# cd /etc/profile.d/
[root@xieyuhui profile.d]# vim mysql.shexport PATH=/usr/local/mysql/bin:$PATH
[root@xieyuhui profile.d]# . /etc/profile.d/mysql.sh
创建mysql存放数据的目录
[root@xieyuhui profile.d]# mkdir -p /opt/data/{3306..3308}
把所有者和所属组换成mysql
[root@xieyuhui ~]# chown -R mysql.mysql /opt/data/
初始化各实例
初始化3306
[root@xieyuhui ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2025-09-19T03:14:36.425495Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-09-19T03:14:36.563218Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-09-19T03:14:36.586074Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-09-19T03:14:36.641190Z 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: c59fcbf4-9506-11f0-a261-000c299a729a.
2025-09-19T03:14:36.641778Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-09-19T03:14:37.267042Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:14:37.267057Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:14:37.268002Z 0 [Warning] CA certificate ca.pem is self signed.
2025-09-19T03:14:37.646367Z 1 [Note] A temporary password is generated for root@localhost: W==<fgaes8_4 #密码为 W==<fgaes8_4
初始化3307
[root@xieyuhui ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2025-09-19T03:14:36.425495Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-09-19T03:14:36.563218Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-09-19T03:14:36.586074Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-09-19T03:14:36.641190Z 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: c59fcbf4-9506-11f0-a261-000c299a729a.
2025-09-19T03:14:36.641778Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-09-19T03:14:37.267042Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:14:37.267057Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:14:37.268002Z 0 [Warning] CA certificate ca.pem is self signed.
2025-09-19T03:14:37.646367Z 1 [Note] A temporary password is generated for root@localhost: W==<fgaes8_4
[root@xieyuhui ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2025-09-19T03:16:03.598504Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-09-19T03:16:03.755562Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-09-19T03:16:03.779952Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-09-19T03:16:03.835328Z 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: f9989142-9506-11f0-a6b2-000c299a729a.
2025-09-19T03:16:03.836259Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-09-19T03:16:04.050270Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:16:04.050286Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:16:04.050859Z 0 [Warning] CA certificate ca.pem is self signed.
2025-09-19T03:16:04.234285Z 1 [Note] A temporary password is generated for root@localhost: kziwiirws5%R #密码为 kziwiirws5%R
初始化3308
[root@xieyuhui ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2025-09-19T03:16:44.060313Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-09-19T03:16:44.241776Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-09-19T03:16:44.276190Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-09-19T03:16:44.331489Z 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: 11bbca1b-9507-11f0-a891-000c299a729a.
2025-09-19T03:16:44.332395Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-09-19T03:16:44.802039Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:16:44.802055Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-09-19T03:16:44.802510Z 0 [Warning] CA certificate ca.pem is self signed.
2025-09-19T03:16:44.962209Z 1 [Note] A temporary password is generated for root@localhost: N#aw<G?si9rZ #密码为 N#aw<G?si9rZ
安装perl
[root@xieyuhui ~]# yum -y install perl
编辑多实例配置文件
[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@xieyuhui ~]# mysqld_multi start 3306
[root@xieyuhui ~]# mysqld_multi start 3307
[root@xieyuhui ~]# mysqld_multi start 3308
初始化密码
[root@xieyuhui ~]# mysql -uroot -p'W==<fgaes8_4' -S /tmp/mysql3306.sock -e 'set password = password("redhat");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xieyuhui ~]# mysql -uroot -p'kziwiirws5%R' -S /tmp/mysql3307.sock -e 'set password = password("redhat");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xieyuhui ~]# mysql -uroot -p'N#aw<G?si9rZ' -S /tmp/mysql3308.sock -e 'set password = password("redhat");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
登录mysql查看
[root@xieyuhui ~]# mysql -uroot -predhat -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.37 MySQL Community Server (GPL)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>
[root@xieyuhui ~]# mysql -uroot -predhat -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.37 MySQL Community Server (GPL)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.
[root@xieyuhui ~]# mysql -uroot -predhat -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 3
Server version: 5.7.37 MySQL Community Server (GPL)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>