首页 > 技术文章 > mysql多实例部署

itwangqiang 2020-10-14 08:30 原文

mysql多实例部署

二进制文件下载

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
.......
[root@localhost src]# ls 
debug  kernels  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

创建用户和组,然后解压至 /usr/local目录下

#创建用户和组
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -M -r -s /sbin/nologin -g mysql mysql


#解压软件至/usr/local/
[root@localhost src]# tar zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.31-linux-glibc2.12-x86_64  sbin  share  src


#创建软连接
[root@localhost local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
[root@localhost 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   35 Jan 10 16:05 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 7161 31415 129 Jun  2  2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root    6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root   49 Dec 24 13:10 share
drwxr-xr-x. 2 root root   56 Jan 10 15:59 src


#修改目录 /usr/local/mysql 的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql*
[root@localhost local]# ll -d mysql
lrwxrwxrwx. 1 mysql mysql 35 Jan 10 16:05 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64


#配置环境变量
[root@localhost ~]# vim /etc/profile.d/mysql.sh
[root@localhost ~]# cat /etc/profile.d/mysql.sh 
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost ~]# source /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 ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jan 10 16:10 3306
drwxr-xr-x. 2 mysql mysql 6 Jan 10 16:10 3307
drwxr-xr-x. 2 mysql mysql 6 Jan 10 16:10 3308
[root@localhost ~]# tree /opt/data/
/opt/data/
├── 3306
├── 3307
└── 3308

初始化各实例

#初始化3306实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306 --basedir=/usr/local/mysql
2021-01-10T08:18:53.784749Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-10T08:18:54.300749Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-10T08:18:54.399807Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-10T08:18:54.416651Z 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: 7a7d7c56-531c-11eb-87a7-000c29decd5a.
2021-01-10T08:18:54.420048Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-10T08:18:55.945722Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-10T08:18:56.572819Z 1 [Note] A temporary password is generated for root@localhost: r4=aYD:fkigr
[root@localhost ~]# echo 'r4=aYD:fkigr' > 3306-pass


#初始化3307实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307 --basedir=/usr/local/mysql
2021-01-10T08:19:50.903564Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-10T08:19:51.400719Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-10T08:19:51.472212Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-10T08:19:51.541330Z 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: 9c8a0685-531c-11eb-8950-000c29decd5a.
2021-01-10T08:19:51.543044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-10T08:19:52.480641Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-10T08:19:53.130092Z 1 [Note] A temporary password is generated for root@localhost: 7Hpj>%,AFS9K
[root@localhost ~]# echo '7Hpj>%,AFS9K' > 3307-pass


#初始化3308实例
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308 --basedir=/usr/local/mysql
2021-01-10T08:20:34.279143Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-10T08:20:34.916792Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-10T08:20:35.004523Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-10T08:20:35.098313Z 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: b6804a84-531c-11eb-8bf8-000c29decd5a.
2021-01-10T08:20:35.116655Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-10T08:20:36.520845Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-10T08:20:36.836153Z 1 [Note] A temporary password is generated for root@localhost: l4LsRt=Z>#00
[root@localhost ~]# echo 'l4LsRt=Z>#00' > 3308-pass

安装 perl

[root@localhost ~]# yum install -y perl
[root@localhost ~]# rpm -qa perl
perl-5.16.3-297.el7.x86_64

修改配置文件 /etc/my.cnf

[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
socket=/tmp/mysql_3306.sock
basedir=/usr/local/mysql
port=3306
pid-file=/opt/data/3306/mysql_3306.pid
character-set-server=utf8
log-error=/var/log/mysql_3306.log

[mysqld3307]
datadir=/opt/data/3307
socket=/tmp/mysql_3307.sock
basedir=/usr/local/mysql
port=3307
pid-file=/opt/data/3307/mysql_3307.pid
character-set-server=utf8
log-error=/var/log/mysql_3307.log

[mysqld3308]
datadir=/opt/data/3308
socket=/tmp/mysql_3308.sock
basedir=/usr/local/mysql
port=3308
pid-file=/opt/data/3308/mysql_3308.pid
character-set-server=utf8
log-error=/var/log/mysql_3308.log

启动3306、3307、3308实例

[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -antlp
State      Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN     0      128                                                                *:22                                                                             *:*                   users:(("sshd",pid=1026,fd=3))
LISTEN     0      80                                                                :::3307                                                                          :::*                   users:(("mysqld",pid=11061,fd=20))
LISTEN     0      80                                                                :::3308                                                                          :::*                   users:(("mysqld",pid=11251,fd=20))
LISTEN     0      128                                                               :::22                                                                            :::*                   users:(("sshd",pid=1026,fd=4))
LISTEN     0      80                                                                :::3306                                                                          :::*                   users:(("mysqld",pid=10871,fd=20))

初始化密码

[root@localhost ~]# cat 3306-pass 
r4=aYD:fkigr
[root@localhost ~]# mysql -uroot -p'r4=aYD:fkigr' -S /tmp/mysql_3306.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

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> \q
Bye

#3307、3308修改密码一模一样,就是换个密码和套接字即可,不一一演示了

推荐阅读