mysql多实例部署配置

下载二进制格式的mysql软件包解压安装包到/usr/local/目录

[root@wyt1 ~]# ls
mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@wyt1 ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@wyt1 ~]# ls
 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

创建用户和组

[root@wyt1 ~]# groupadd -r mysql
[root@wyt1 ~]# useradd -r -M -s /sbin/nologin  -g mysql mysql

创建链接

[root@wyt1 ~]# cd /usr/local
[root@wyt1 local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.30-linux-glibc2.12-x86_64  share
[root@wyt1 local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@wyt1 local]# ll
total 0
drwxr-xr-x. 2 root root   6 Nov  5  2016 bin
drwxr-xr-x. 2 root root   6 Nov  5  2016 etc
drwxr-xr-x. 2 root root   6 Nov  5  2016 games
drwxr-xr-x. 2 root root   6 Nov  5  2016 include
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx. 1 root root  36 Jun 24 18:51 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jun 24 18:50 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root  49 Mar 28 05:47 share
drwxr-xr-x. 2 root root   6 Nov  5  2016 src

修改目录/usr/local/mysql的属主属组

[root@wyt1 local]# chown -R mysql.mysql mysql*
[root@wyt1 local]# ll
total 0
drwxr-xr-x. 2 root  root    6 Nov  5  2016 bin
drwxr-xr-x. 2 root  root    6 Nov  5  2016 etc
drwxr-xr-x. 2 root  root    6 Nov  5  2016 games
drwxr-xr-x. 2 root  root    6 Nov  5  2016 include
drwxr-xr-x. 2 root  root    6 Nov  5  2016 lib
drwxr-xr-x. 2 root  root    6 Nov  5  2016 lib64
drwxr-xr-x. 2 root  root    6 Nov  5  2016 libexec
lrwxrwxrwx. 1 mysql mysql  36 Jun 24 18:51 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Jun 24 18:50 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 Nov  5  2016 sbin
drwxr-xr-x. 5 root  root   49 Mar 28 05:47 share
drwxr-xr-x. 2 root  root    6 Nov  5  2016 src

设置环境变量

[root@wyt1 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@wyt1 local]# source /etc/profile.d/mysql.sh
[root@wyt1 local]# which mysql
/usr/local/mysql/bin/mysql

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

[root@wyt1 local]# mkdir /opt/data
[root@wyt1 local]# mkdir /opt/data/{3306,3307,3308}
[root@wyt1 local]# ll /opt/data
total 0
drwxr-xr-x. 2 root root 6 Jun 24 21:49 3306
drwxr-xr-x. 2 root root 6 Jun 24 21:49 3307
drwxr-xr-x. 2 root root 6 Jun 24 21:49 3308
[root@wyt1 local]# chown -R mysql.mysql /opt/data
[root@wyt1 local]# ll /opt/data
total 0
drwxr-xr-x. 2 mysql mysql 6 Jun 24 21:49 3306
drwxr-xr-x. 2 mysql mysql 6 Jun 24 21:49 3307
drwxr-xr-x. 2 mysql mysql 6 Jun 24 21:49 3308

初始化各实例

//初始化3306实例
[root@wyt1 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306
2020-06-24T14:05:28.246197Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-24T14:05:29.265629Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-24T14:05:29.478618Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-24T14:05:29.868620Z 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: c2eda343-b623-11ea-a96e-000c29a54fe1.
2020-06-24T14:05:30.045357Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-24T14:05:31.220615Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-24T14:05:31.476932Z 1 [Note] A temporary password is generated for root@localhost: O4d8)KqnJqvH
[root@wyt1 ~]# echo 'O4d8)KqnJqvH' > 3306_pass //取出临时密码写入文件
[root@wyt1 ~]# cat 3306_pass
O4d8)KqnJqvH

//初始化3307实例
[root@wyt1 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307
2020-06-24T14:06:58.044017Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-24T14:06:58.809763Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-24T14:06:58.917248Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-24T14:06:58.953156Z 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: f806dcdc-b623-11ea-acab-000c29a54fe1.
2020-06-24T14:06:58.954695Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-24T14:07:00.106479Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-24T14:07:00.353680Z 1 [Note] A temporary password is generated for root@localhost: sP:C)vnsD1=X
[root@wyt1 ~]# echo 'sP:C)vnsD1=X' > 3307_pass
[root@wyt1 ~]# cat 3307_pass 
sP:C)vnsD1=X

//初始化3308实例
[root@wyt1 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308
2020-06-24T14:07:52.619275Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-24T14:07:53.208914Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-24T14:07:53.310933Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-24T14:07:53.393911Z 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: 1879da82-b624-11ea-ae61-000c29a54fe1.
2020-06-24T14:07:53.396818Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-24T14:07:54.626380Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-24T14:07:54.751997Z 1 [Note] A temporary password is generated for root@localhost: !qtxzoll)1)O
[root@wyt1 ~]# echo '!qtxzoll)1)O' > 3308_pass
[root@wyt1 ~]# cat 3308_pass 
!qtxzoll)1)O

安装perl

[root@wyt1 ~]# yum -y install perl

编辑配置文件/etc/my.cnf

[root@wyt1 ~]# 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/mysql/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/mysql/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/mysql/3308.log
[root@wyt1 ~]# mkdir /var/log/mysql //创建日志存放目录
[root@wyt1 ~]# chown -R mysql.mysql /var/log/mysql* //修改存放日志属主属组

启动各实例

[root@wyt1 ~]# mysqld_multi start 3306
[root@wyt1 ~]# mysqld_multi start 3307
[root@wyt1 ~]# mysqld_multi start 3308
[root@wyt1 ~]# 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      80        :::3307                  :::*                  
LISTEN      0      80        :::3308                  :::*                  
LISTEN      0      128       :::22                    :::*                  
LISTEN      0      100      ::1:25                    :::*                  
LISTEN      0      80        :::3306                  :::*    

初始化密码

//登录mysql3306并修改密码
[root@wyt1 ~]# cat 3306_pass 
O4d8)KqnJqvH
[root@wyt1 ~]# mysql -uroot -p'O4d8)KqnJqvH' -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 28
Server version: 5.7.30

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> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

//不登录mysql3307修改密码
[root@wyt1 ~]# cat 3307_pass 
sP:C)vnsD1=X
[root@wyt1 ~]# mysql -uroot -p'sP:C)vnsD1=X' -S /tmp/mysql3307.sock --connect-expired-password -e 'set password=password("123456");'
mysql: [Warning] Using a password on the command line interface can be insecure.

//不登录mysql3308修改密码
[root@wyt1 ~]# cat 3308_pass 
!qtxzoll)1)O
[root@wyt1 ~]# mysql -uroot -p'!qtxzoll)1)O' -S /tmp/mysql3308.sock --connect-expired-password -e 'set password=password("123456");'
mysql: [Warning] Using a password on the command line interface can be insecure.

//验证
[root@wyt1 ~]# mysql -uroot -p'123456' -P3306 -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 36
Server version: 5.7.30 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> 

猜你喜欢

转载自blog.csdn.net/lnsistw/article/details/106950975
今日推荐