MySQL不同版本多实例(5.6.X,5.7.X,8.0.X)
1.1 准备工作
1.1.1 创建数据目录
mkdir -p /data/335{6..7}/data
chown -R mysql.mysql /data/
ls -ld /data/335*
结果:
[root@localhost ~]# mkdir -p /data/335{6..7}/data
[root@localhost ~]# chown -R mysql.mysql /data/
[root@localhost ~]# ls -ld /data/335*
drwxr-xr-x 3 mysql mysql 18 Nov 27 11:17 /data/3356
drwxr-xr-x 3 mysql mysql 18 Nov 27 11:17 /data/3357
[root@localhost ~]#
1.1.2 配置文件
实例5.6:
cat > /etc/my3356.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql56
datadir=/data/3356/data
server_id=56
port=3356
socket=/tmp/mysql56.sock
EOF
实例5.7:
cat > /etc/my3357.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql57
datadir=/data/3357/data
server_id=57
port=3357
socket=/tmp/mysql57.sock
EOF
结果:
[root@localhost ~]# cat /etc/my3356.cnf # 5.6
[mysqld]
user=mysql
basedir=/usr/local/mysql56
datadir=/data/3356/data
server_id=56
port=3356
socket=/tmp/mysql56.sock
[root@localhost ~]#
[root@localhost ~]# cat /etc/my3357.cnf # 5.7
[mysqld]
user=mysql
basedir=/usr/local/mysql57
datadir=/data/3357/data
server_id=57
port=3357
socket=/tmp/mysql57.sock
[root@localhost ~]#
1.1.3 解压文件
tar xf mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql57
ln -s /usr/local/mysql-5.6.50-linux-glibc2.12-x86_64 mysql56
结果:
[root@localhost local]# tar xf mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]# tar xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]# ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql57
[root@localhost local]# ln -s /usr/local/mysql-5.6.50-linux-glibc2.12-x86_64 mysql56
1.2 初始化
5.6版本初始化:
/usr/local/mysql56/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56 --datadir=/data/3356/data
5.7版本初始化:
/usr/local/mysql57/bin/mysqld --defaults-file=/etc/my3357.cnf --initialize-insecure
结果:
[root@localhost ~]# /usr/local/mysql56/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56 --datadir=/data/3356/data # 5.6版本初始化
Installing MySQL system tables...2021-11-27 14:10:36 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
……
32021-11-27 14:10:38 11415 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2021-11-27 14:10:38 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
……
2021-11-27 14:10:40 11437 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK # 出现两个OK,没有报错表示初始化成功
……
[root@localhost ~]#
5.7
[root@localhost ~]# /usr/local/mysql57/bin/mysqld --defaults-file=/etc/my3357.cnf --initialize-insecure # 5.7版本初始化
2021-11-27T06:15:12.408238Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-11-27T06:15:12.558959Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-11-27T06:15:12.586227Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-11-27T06:15:12.653529Z 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: 61600db3-4f49-11ec-8f13-000c2943a92f.
2021-11-27T06:15:12.658569Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-11-27T06:15:13.557983Z 0 [Warning] CA certificate ca.pem is self signed.
2021-11-27T06:15:13.850982Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost ~]#
1.3 启动
命令:
/usr/local/mysql57/bin/mysqld_safe --defaults-file=/etc/my3357.cnf &
/usr/local/mysql56/bin/mysqld_safe --defaults-file=/etc/my3356.cnf &
检查:
[root@localhost ~]# netstat -lntup|grep 335
tcp6 0 0 :::3356 :::* LISTEN 11836/mysqld
tcp6 0 0 :::3357 :::* LISTEN 11670/mysqld
[root@localhost ~]#
1.4 登录
mysql -S /tmp/mysql56.sock
mysql -S /tmp/mysql57.sock
mysql -S /tmp/mysql.sock
结果:
[root@localhost ~]# mysql -S /tmp/mysql56.sock # 登录5.6
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> quit
Bye
[root@localhost ~]#
[root@localhost ~]# mysql -S /tmp/mysql57.sock # 登录5.7
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> quit
Bye
[root@localhost ~]# mysql -S /tmp/mysql.sock # 登录8.0
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.24 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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>
1.5 停止
/usr/local/mysql56/bin/mysqladmin -S /tmp/mysql56.sock shutdown
/usr/local/mysql57/bin/mysqladmin -S /tmp/mysql56.sock shutdown
/usr/local/mysql/bin/mysqladmin -S /tmp/mysql.sock shutdown
1.6 小结
0、8.0可以参照8.0.X多实例
1、5.6的初始化命令和5.7+的命令有些区别,注意一下就好。
2、应用建议部署在/usr/local/下。
3、多实例时,连接服务器的客户端程序版本可以比服务器版本高,当前客户端的版本为8.0.24,如:
[root@localhost ~]# mysql --version
mysql Ver 8.0.24 for Linux on x86_64 (MySQL Community Server - GPL)
[root@localhost ~]#