mysql多实例部署

1.软件下载

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2018-08-13 23:56:27--  https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’
    
100%[=====================================>] 643,790,848 2.46MB/s   in 4m 20s
    
2018-08-14 00:00:50 (2.36 MB/s) - ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
[root@localhost src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

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

//创建用户和组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql

//解压软件至/usr/local/
[root@localhost src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 sbin share src
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
lrwxrwxrwx. 1 root root 36 12月 13 16:49 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 12月 13 16:49 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 10月 11 02:20 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 12月 13 16:49 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

//配置环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

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

[root@localhost local]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost local]# chown -R mysql.mysql /opt/data/
[root@localhost local]# ll /opt/data/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 12月 13 16:53 3306
drwxr-xr-x. 2 mysql mysql 6 12月 13 16:53 3307
drwxr-xr-x. 2 mysql mysql 6 12月 13 16:53 3308

4.初始化各实例

//初始化3306实例
[root@localhost local]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2019-12-13T08:56:16.436200Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-13T08:56:17.095360Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-13T08:56:17.192295Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-13T08:56:17.795436Z 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: 6ce46bd6-1d86-11ea-9f7b-000c2900526e.
2019-12-13T08:56:17.797865Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-13T08:56:17.801198Z 1 [Note] A temporary password is generated for root@localhost: jrXQa,%pu0f>
[root@localhost local]# echo 'jrXQa,%pu0f>' > 3306.pass

//初始化3307实例
[root@localhost local]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2019-12-13T08:57:36.419776Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-13T08:57:36.834793Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-13T08:57:36.883177Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-13T08:57:36.940986Z 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: 9c1113cb-1d86-11ea-a1a7-000c2900526e.
2019-12-13T08:57:36.943607Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-13T08:57:36.947931Z 1 [Note] A temporary password is generated for root@localhost: yQL!5z=8rdk*
[root@localhost local]# echo 'yQL!5z=8rdk*' > 3307.pass

//初始化3308实例
[root@localhost local]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2019-12-13T08:58:46.157236Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-13T08:58:46.527441Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-13T08:58:46.576831Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-13T08:58:46.638767Z 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: c59c1c5d-1d86-11ea-a4d1-000c2900526e.
2019-12-13T08:58:46.642996Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-13T08:58:46.645851Z 1 [Note] A temporary password is generated for root@localhost: iloNPuvM)9p+
[root@localhost local]# echo 'iloNPuvM)9p+' > 3308.pass

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

//安装perl
[root@localhost local]# yum -y install perl
[root@localhost local]# vim /etc/my.cnf
[root@localhost local]# cat /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

6.启动各实例

[root@localhost local]# mysqld_multi start 3306
[root@localhost local]# mysqld_multi start 3307
[root@localhost local]# mysqld_multi start 3308
[root@localhost local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port              
LISTEN 0 128 *:22 *:*                  
LISTEN 0 100 127.0.0.1:25 *:*                  
LISTEN 0 25 *:514 *:*                  
LISTEN 0 80 :::3307 :::*                  
LISTEN 0 80 :::3308 :::*                  
LISTEN 0 128 :::22 :::*                  
LISTEN 0 100 ::1:25 :::*                  
LISTEN 0 25 :::514 :::*                  
LISTEN 0 80 :::3306 :::*                  

7.初始化密码

[root@localhost local]# ls
3306.pass 3308.pass etc include lib64 mysql sbin src
3307.pass bin games lib libexec mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@localhost local]# cat 3306.pass 
jrXQa,%pu0f>
[root@localhost local]# mysql -uroot -p'jrXQa,%pu0f>' -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.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> quit
Bye
[root@localhost local]# cat 3307.pass 
yQL!5z=8rdk*
[root@localhost local]# mysql -uroot -p'yQL!5z=8rdk*' -S /tmp/mysql3307.sock -e 'set password = password("123456");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost local]# cat 3308.pass 
iloNPuvM)9p+
[root@localhost local]# mysql -uroot -p'iloNPuvM)9p+' -S /tmp/mysql3308.sock -e 'set password = password("123456");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
原文地址:https://www.cnblogs.com/liping0826/p/12047514.html