多实例介绍
简单的说,就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口提供各自的服务
这些mysql多实例公用一套MySQL安装程序,使用不同的my.cnf配置文件,启动程序,数据文件,在提供服务时,多实例在逻辑上看上去是各自独立的,多实例根据配置文件的设定值来取得相关的文件资源。
多实例配置
这里采用的二进制包安装mysql
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# useradd -r -M -s /sbin/nolog mysql [root@localhost ~]# ls mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz [root@localhost ~]# tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost ~]# mv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/ /usr/local/mysql [root@localhost local]# chown -R mysql.mysql mysql/ [root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH'>/etc/profile.d/mysql.sh [root@localhost local]# source /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 [root@localhost ~]# yum -y install perl
创建各实例数据存放的目录
[root@localhost local]# mkdir -p /opt/data/{3306,3307,3308} [root@localhost local]# chown -R mysql.mysql /opt/data/ [root@localhost local]# ls /opt/data/ 3306 3307 3308
初始化各个实例
[root@localhost local]# mysqld --initialize --user=mysql --datadir=/opt/data/3306 2020-12-31T14:16:38.720321Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-31T14:16:39.095381Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-12-31T14:16:39.276899Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-12-31T14:16:39.416975Z 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: cc7f1bf0-4b72-11eb-9039-000c2928a994. 2020-12-31T14:16:39.420081Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-12-31T14:16:44.864375Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-31T14:16:45.552161Z 1 [Note] A temporary password is generated for root@localhost: fh45Hi,F60(8 [root@localhost local]# echo 'fh45Hi,F60(8' > /root/3306 [root@localhost local]# mysqld --initialize --user=mysql --datadir=/opt/data/3307 2020-12-31T14:17:27.942075Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-31T14:17:28.358493Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-12-31T14:17:28.429775Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-12-31T14:17:28.495154Z 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: e9bfd855-4b72-11eb-9275-000c2928a994. 2020-12-31T14:17:28.498033Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-12-31T14:17:33.192967Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-31T14:17:34.169651Z 1 [Note] A temporary password is generated for root@localhost: ESbrhuW%*4am [root@localhost local]# echo 'ESbrhuW%*4am' >/root/3307 [root@localhost local]# mysqld --initialize --user=mysql --datadir=/opt/data/3308 2020-12-31T14:17:56.635595Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-31T14:17:56.999889Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-12-31T14:17:57.070358Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-12-31T14:17:57.134853Z 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: fad1e2d7-4b72-11eb-9437-000c2928a994. 2020-12-31T14:17:57.137408Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-12-31T14:17:58.584622Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-31T14:17:59.091124Z 1 [Note] A temporary password is generated for root@localhost: mks#ZuR2vR(g [root@localhost local]# echo 'mks#ZuR2vR(g' > /root/3308
配置配置文件/etc/my.cnf
root@localhost local]# 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 80 *:3306 *:* LISTEN 0 80 *:3307 *:* LISTEN 0 80 *:3308
修改密码
[root@localhost ~]# mysql -uroot -p'fh45Hi,F60(8' -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.31 .......................................................... 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.01 sec) mysql> quit Bye [root@localhost ~]# mysql -uroot -p123456 -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.31 MySQL Community Server (GPL) Copyright (c) 2000, 2020, 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> [root@localhost ~]# mysql -uroot -p'ESbrhuW%*4am' -S /tmp/mysql3307.sock ...................... [root@localhost ~]# mysql -uroot -p'mks#ZuR2vR(g' -h127.0.0.1 -P3308