多实例就是在一台服务器上同时开启多个不同的数据库服务端口(例如3306、3307),同时运行多个MYSQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。
优点
MySQL多实例,可以通过多个端口向用户提供服务,充分利用一台服务器的闲置资源
缺点
无论是多少个端口提供服务,但始终使用的是一台服务器的资源,所以当访问量过大时,依然存在高并发的问题
#下载二进制格式的mysql软件包 [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#创建mysql用户和组 [root@localhost src]# useradd -r -M -s /sbin/nologin mysql [root@localhost src]# id mysql uid=994(mysql) gid=991(mysql) groups=991(mysql) #解压软件至/usr/local/ [root@localhost src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost src]# ls /usr/local/ bin games lib libexec sbin src etc include lib64 mysql-5.7.37-linux-glibc2.12-x86_64 share #设置软链接 [root@localhost local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql 'mysql' -> 'mysql-5.7.37-linux-glibc2.12-x86_64/' [root@localhost local]# ll total 0 drwxr-xr-x. 2 root root 6 May 19 2020 bin drwxr-xr-x. 2 root root 6 May 19 2020 etc drwxr-xr-x. 2 root root 6 May 19 2020 games drwxr-xr-x. 2 root root 6 May 19 2020 include drwxr-xr-x. 2 root root 6 May 19 2020 lib drwxr-xr-x. 3 root root 17 Jul 23 21:08 lib64 drwxr-xr-x. 2 root root 6 May 19 2020 libexec lrwxrwxrwx. 1 root root 36 Jul 29 01:58 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/ drwxr-xr-x. 9 root root 129 Jul 29 01:55 mysql-5.7.37-linux-glibc2.12-x86_64 drwxr-xr-x. 2 root root 6 May 19 2020 sbin drwxr-xr-x. 5 root root 49 Jul 23 21:08 share drwxr-xr-x. 2 root root 6 May 19 2020 src #修改目录/usr/local/mysql*的属主属组 [root@localhost local]# chown -R mysql.mysql mysql* [root@localhost local]# ll total 0 drwxr-xr-x. 2 root root 6 May 19 2020 bin drwxr-xr-x. 2 root root 6 May 19 2020 etc drwxr-xr-x. 2 root root 6 May 19 2020 games drwxr-xr-x. 2 root root 6 May 19 2020 include drwxr-xr-x. 2 root root 6 May 19 2020 lib drwxr-xr-x. 3 root root 17 Jul 23 21:08 lib64 drwxr-xr-x. 2 root root 6 May 19 2020 libexec lrwxrwxrwx. 1 mysql mysql 36 Jul 29 01:58 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/ drwxr-xr-x. 9 mysql mysql 129 Jul 29 01:55 mysql-5.7.37-linux-glibc2.12-x86_64 drwxr-xr-x. 2 root root 6 May 19 2020 sbin drwxr-xr-x. 5 root root 49 Jul 23 21:08 share drwxr-xr-x. 2 root root 6 May 19 2020 src #添加环境变量 [root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh [root@localhost local]# source /etc/profile.d/mysql.sh [root@localhost local]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin #配置头文件—include,库文件—lib,man文件 a).头文件—include [root@localhost mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql [root@localhost mysql]# ldconfig b).库文件—lib [root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib/ c).man文件 [root@localhost mysql]# vim /etc/man_db.conf ...... MANDATORY_MANPATH /usr/local/mysql/man ......
[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 Jul 29 02:08 3306 drwxr-xr-x. 2 mysql mysql 6 Jul 29 02:08 3307 drwxr-xr-x. 2 mysql mysql 6 Jul 29 02:08 3308
#初始化3306实例 [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306 2022-07-28T18:10:55.688238Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-28T18:10:55.870431Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-28T18:10:55.900700Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-28T18:10:55.958646Z 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: 9ff5a837-0ea0-11ed-8af7-000c29e16842. 2022-07-28T18:10:55.959533Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-28T18:10:56.479019Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:10:56.479035Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:10:56.479478Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-28T18:10:56.541878Z 1 [Note] A temporary password is generated for root@localhost: glUI5k%_eY0) [root@localhost ~]# echo 'glUI5k%_eY0)' > 3306_passwd #初始化3307实例 [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307 2022-07-28T18:13:08.002396Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-28T18:13:08.190046Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-28T18:13:08.216254Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-28T18:13:08.220706Z 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: eecb3f15-0ea0-11ed-89c1-000c29e16842. 2022-07-28T18:13:08.221521Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-28T18:13:08.894461Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:13:08.894488Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:13:08.894929Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-28T18:13:09.105418Z 1 [Note] A temporary password is generated for root@localhost: s3#Y-hhyBKve [root@localhost ~]# echo 's3#Y-hhyBKve' > 3307_passwd #初始化3308实例 [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308 2022-07-28T18:13:53.110334Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-28T18:13:53.283030Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-28T18:13:53.308723Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-28T18:13:53.364947Z 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: 09b3b5f7-0ea1-11ed-926a-000c29e16842. 2022-07-28T18:13:53.365788Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-28T18:13:54.027096Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:13:54.027135Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-28T18:13:54.027592Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-28T18:13:54.447992Z 1 [Note] A temporary password is generated for root@localhost: (+d7r!3v%lVe [root@localhost ~]# echo '(+d7r!3v%lVe' > 3308_passwd
[root@localhost ~]# yum -y install perl
[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 Process 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 *:*
#3306初始密码 [root@localhost ~]# cat 3306_passwd glUI5k%_eY0) [root@localhost ~]# mysql -p'glUI5k%_eY0)' -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.37 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> set password = password('Passwd123!'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit Bye #3307初始密码 [root@localhost ~]# cat 3307_passwd s3#Y-hhyBKve [root@localhost ~]# mysql -p's3#Y-hhyBKve' -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 2 Server version: 5.7.37 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> set password = password('Passwd123!'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit; Bye #3308初始密码 [root@localhost ~]# cat 3308_passwd (+d7r!3v%lVe [root@localhost ~]# mysql -p'(+d7r!3v%lVe' -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 2 Server version: 5.7.37 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> set password = password('Passwd123!'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit; Bye