mysql多实例部署
mysql多实例部署
下载二进制格式的mysql软件包
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-31 12:24:36-- 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)... 23.2.135.207, 2600:140b:2:99c::2e31, 2600:140b:2:99d::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.2.135.207|: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 12:24:37-- 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.78.91.208
Connecting to cdn.mysql.com (cdn.mysql.com)|23.78.91.208|: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-linux-g 100%[===================>] 643.57M 88.4KB/s in 32m 11s
2022-07-31 12:56:50 (341 KB/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
//解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.38-linux-glibc2.12-x86_64 share
//软链接
[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 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Jul 26 09:28 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 36 Jul 31 13:36 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 31 13:34 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Jul 26 09:28 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Jul 31 13:36 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
//配置环境变量
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh
//添加头文件
[root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost mysql]# chown -R mysql.mysql /usr/include/mysql
[root@localhost mysql]# ll -d /usr/include/mysql
lrwxrwxrwx. 1 mysql mysql 25 Jul 31 13:41 /usr/include/mysql -> /usr/local/mysql/include/
//配置lib和man
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@localhost mysql]# ldconfig
[root@localhost mysql]# vim /etc/man_db.conf //进入后向下翻一些
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
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 -d /opt/data/
drwxr-xr-x. 5 mysql mysql 42 Jul 31 13:47 /opt/data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 31 13:47 3306
drwxr-xr-x. 2 mysql mysql 6 Jul 31 13:47 3307
drwxr-xr-x. 2 mysql mysql 6 Jul 31 13:47 3308
//tree
[root@localhost ~]# dnf -y install tree //tree下载tree命令相关软件包
[root@localhost ~]# tree /opt/data/
/opt/data/
|-- 3306
|-- 3307
`-- 3308
3 directories, 0 files
初始化各实例
//初始化3306实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2022-07-31T06:02:35.117988Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T06:02:35.244310Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T06:02:35.266037Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T06:02:35.270028Z 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: 5f8f02bb-1096-11ed-879a-000c29b59a13.
2022-07-31T06:02:35.270556Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T06:02:35.394298Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:02:35.394323Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:02:35.394644Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T06:02:35.445761Z 1 [Note] A temporary password is generated for root@localhost: y,4Z>aD6CwZd
[root@localhost ~]# echo 'y,4Z>aD6CwZd' > 3306pass
//初始化3307实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2022-07-31T06:03:00.636773Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T06:03:00.745515Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T06:03:00.766233Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T06:03:00.769480Z 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: 6ec1eb9e-1096-11ed-98c1-000c29b59a13.
2022-07-31T06:03:00.769949Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T06:03:00.941768Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:03:00.941800Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:03:00.942142Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T06:03:01.023855Z 1 [Note] A temporary password is generated for root@localhost: dEm=UqPWo5hR
[root@localhost ~]# echo 'dEm=UqPWo5hR' > 3307pass
//初始化3308实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2022-07-31T06:03:18.086764Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-31T06:03:18.195678Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-31T06:03:18.214198Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-31T06:03:18.217448Z 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: 792844d6-1096-11ed-bd16-000c29b59a13.
2022-07-31T06:03:18.217988Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-31T06:03:18.304429Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:03:18.304457Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-31T06:03:18.304783Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-31T06:03:18.374868Z 1 [Note] A temporary password is generated for root@localhost: <fq<,F5UkQkE
[root@localhost ~]# echo '<fq<,F5UkQkE' > 3308pass
配置配置文件/etc/my.cnf
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /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
安装perl
[root@localhost ~]# dnf -y install perl
配置启动文件
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/3306.service
[root@localhost ~]# vim /usr/lib/systemd/system/3306.service
[root@localhost ~]# cat /usr/lib/systemd/system/3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306
Execstop=ps -ef | grep 3306 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# cp 3306.service 3307.service
[root@localhost system]# vim 3307.service
[root@localhost system]# cat 3307.service
[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307
Execstop=ps -ef | grep 3307 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# cp 3306.service 3308.service
[root@localhost system]# vim 3308.service
[root@localhost system]# cat 3308.service
[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308
Execstop=ps -ef | grep 3308 | grep -v grep |awk '{print$2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
重启配置文件,关闭防火墙和selinux
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# setenforce 0
将my_print_defaults命令加入系统可识别命令
[root@localhost ~]# ln -sv /usr/local/mysql/bin/my_print_defaults /usr/bin/my_print_defaults
'/usr/bin/my_print_defaults' -> '/usr/local/mysql/bin/my_print_defaults'
启动各实例
[root@localhost ~]# systemctl start 3306
[root@localhost ~]# systemctl start 3307
[root@localhost ~]# systemctl 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 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
安装ncurses-compat-libs
[root@localhost ~]# dnf -y install ncurses-compat-libs
初始化密码
[root@localhost ~]# ls
3306pass 3307pass 3308pass anaconda-ks.cfg
[root@localhost ~]# cat 3306pass
y,4Z>aD6CwZd
[root@localhost ~]# mysql -uroot -p'y,4Z>aD6CwZd' -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 ~]# cat 3307pass
dEm=UqPWo5hR
[root@localhost ~]# mysql -uroot -p'dEm=UqPWo5hR' -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.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 ~]# cat 3308pass
<fq<,F5UkQkE
[root@localhost ~]# mysql -uroot -p'<fq<,F5UkQkE' -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.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