Mysql多实例部署

下载源码包

[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

配置用户和组并解压二进制程序至/usr/local下

[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
lrwxrwxrwx. 1 mysql mysql 35 Dec 30 00:15 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jun 2 2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Nov 24 15:23 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src
[root@localhost local]# cd
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

创建各实例数据存放的目录

[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Dec 30 00:18 3306
drwxr-xr-x. 2 mysql mysql 6 Dec 30 00:18 3307
drwxr-xr-x. 2 mysql mysql 6 Dec 30 00:18 3308

初始化各实例

[root@localhost ~]# yum -y install ncurses-compat-libs

//初始化3306实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306
2020-12-29T16:22:15.588751Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-29T16:22:16.839052Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-29T16:22:17.030004Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-29T16:22:17.129770Z 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: 047f4f41-49f2-11eb-abb9-000c29c83ec8.
2020-12-29T16:22:17.130762Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-29T16:22:17.785473Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-29T16:22:17.978888Z 1 [Note] A temporary password is generated for root@localhost: LedF2pPoq!?W
[root@localhost ~]# echo 'LedF2pPoq!?W' > 3306

//初始化3307实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307
2020-12-29T16:23:25.830064Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-29T16:23:27.250395Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-29T16:23:27.549030Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-29T16:23:27.580248Z 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: 2e7d3284-49f2-11eb-ad63-000c29c83ec8.
2020-12-29T16:23:27.581981Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-29T16:23:28.343134Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-29T16:23:28.609117Z 1 [Note] A temporary password is generated for root@localhost: jwqUfujih4,d
[root@localhost ~]# echo 'jwqUfujih4,d' > 3307

//初始化3308实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308
2020-12-29T16:23:50.021554Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-29T16:23:51.295725Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-29T16:23:51.659822Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-29T16:23:51.689869Z 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: 3cdc08fa-49f2-11eb-b0a4-000c29c83ec8.
2020-12-29T16:23:51.691009Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-29T16:23:52.238891Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-29T16:23:52.375543Z 1 [Note] A temporary password is generated for root@localhost: FkHgIUfM/1Ih
[root@localhost ~]# echo 'FkHgIUfM/1Ih' > 3308
[root@localhost ~]# ls
3306 3307 3308 anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

安装perl

[root@localhost ~]# yum -y install perl

配置配置文件/etc/my.cnf

[root@localhost ~]# 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@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*

初始化密码

[root@localhost ~]# ls
3306 3307 3308 anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

//更改3306密码
[root@localhost ~]# cat 3306
LedF2pPoq!?W
[root@localhost ~]# mysql -uroot -p'LedF2pPoq!?W' -S /tmp/mysql3306.sock

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

[root@localhost ~]# mysql -uroot -p'123456' -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> quit
Bye

//更改3307密码
[root@localhost ~]# cat 3307
jwqUfujih4,d
[root@localhost ~]# mysql -uroot -p'jwqUfujih4,d' -S /tmp/mysql3307.sock

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

[root@localhost ~]# mysql -uroot -p'123456' -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> quit
Bye

//更改3308密码
[root@localhost ~]# cat 3308
FkHgIUfM/1Ih
[root@localhost ~]# mysql -uroot -p'FkHgIUfM/1Ih' -S /tmp/mysql3308.sock

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

[root@localhost ~]# mysql -uroot -p'123456' -S /tmp/mysql3308.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> quit
Bye