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 //使用wget命令进行下载
--2022-07-31 15:08:46-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 104.89.9.126, 2600:1405:1800:184::2e31, 2600:1405:1800:19c::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|104.89.9.126|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz [following]
--2022-07-31 15:08:48-- https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.203.29.47
Connecting to cdn.mysql.com (cdn.mysql.com)|23.203.29.47|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
mysql-5.7.38-li 100%[=======>] 643.57M 1.86MB/s in 9m 52s
2022-07-31 15:18:41 (1.09 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
配置用户并解压二进制程序至/usr/local/下
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql //创建用户和组
[root@localhost src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ //解压软件至/usr/local/
[root@localhost src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# cd
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# ls /usr/local/
bin include libexec share
etc lib mysql-5.7.38-linux-glibc2.12-x86_64 src
games lib64 sbin
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-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 Jun 27 19:38 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 root root 36 Jul 31 15:23 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 31 15:21 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 5 root root 49 Jun 27 19:38 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql //修改目录/usr/local/mysql的属主属组
[root@localhost ~]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 Jul 31 15:23 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh //配置环境变量
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
创建目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]#
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 31 15:25 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 31 15:25 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 31 15:25 3308
初始化各实例
//初始化3306实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-31T07:27:22.844048Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T07:27:22.967550Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T07:27:22.988498Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T07:27:23.043721Z 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: 381bab10-10a2-11ed-823b-000c296afe0a.
2022-07-31T07:27:23.044355Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T07:27:23.287901Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:27:23.287910Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:27:23.288216Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T07:27:23.340381Z 1 [Note] A temporary password is generated for root@localhost: o7kHMpi4Df)q
[root@localhost ~]# echo 'o7kHMpi4Df)q' > 3306_pass
[root@localhost ~]# ls
3306_pass anaconda-ks.cfg
[root@localhost ~]# cat 3306_pass
o7kHMpi4Df)q
//初始化3307实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-31T07:29:16.704361Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T07:29:16.818612Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T07:29:16.846711Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T07:29:16.901854Z 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: 7bf90a75-10a2-11ed-9716-000c296afe0a.
2022-07-31T07:29:16.902509Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T07:29:17.059790Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:29:17.059801Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:29:17.060093Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T07:29:17.209983Z 1 [Note] A temporary password is generated for root@localhost: Xy/lfLU#j2f_
[root@localhost ~]# echo 'Xy/lfLU#j2f_' > 3307_pass
[root@localhost ~]# ls
3306_pass 3307_pass anaconda-ks.cfg
[root@localhost ~]# cat 3307_pass
Xy/lfLU#j2f_
//初始化3308实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-31T07:30:55.317345Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T07:30:55.429328Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T07:30:55.447016Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T07:30:55.502501Z 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: b6be4e3a-10a2-11ed-996f-000c296afe0a.
2022-07-31T07:30:55.502960Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T07:30:55.773249Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:30:55.773261Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T07:30:55.773699Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T07:30:55.785725Z 1 [Note] A temporary password is generated for root@localhost: -C1C:uDrXpT-
[root@localhost ~]# echo '-C1C:uDrXpT-' > 3308_pass
[root@localhost ~]# ls
3306_pass 3307_pass 3308_pass anaconda-ks.cfg
[root@localhost ~]# cat 3308_pass
-C1C:uDrXpT-
配置配置文件/etc/my.cnf
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe //定义mysqld程序命令存放路径
mysqladmin = /usr/local/mysql/bin/mysqladmin //定义mysqladmin程序命令存放路径
[mysqld3306]
datadir = /opt/data/3306 //定义数据目录存放路径
port = 3306 //定义端口
socket = /tmp/mysql3306.sock //定义套接字
pid-file = /opt/data/3306/mysql_3306.pid //定义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
-bash: /usr/local/mysql/bin/mysqld_multi: /usr/bin/perl: bad interpreter: No such file or directory
[root@localhost ~]# dnf -y install perl //安装perl
[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:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
初始化密码
[root@localhost ~]# mysql -uroot -p'o7kHMpi4Df)q' -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.38
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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# ls
3306_pass 3307_pass 3308_pass anaconda-ks.cfg
[root@localhost ~]# cat 3307_pass
Xy/lfLU#j2f_
[root@localhost ~]# mysql -uroot -p'Xy/lfLU#j2f_' -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 ~]# ls
3306_pass 3307_pass 3308_pass anaconda-ks.cfg
[root@localhost ~]# cat 3308_pass
-C1C:uDrXpT-
[root@localhost ~]# mysql -uroot -p'-C1C:uDrXpT-' -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.