一.MySQL多实例部署
1.安装MySQL
//创建MySQL用户
[root@192 ~]# useradd -r -M -s /sbin/nologin mysql
[root@192 ~]# id mysql
uid=994(mysql) gid=991(mysql) 组=991(mysql)
[root@192 ~]# cd /usr/src/
[root@192 src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ //解压MySQL软件包到指定目录下
[root@192 src]# cd /usr/local/
[root@192 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.37-linux-glibc2.12-x86_64 share
[root@192 local]# mv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql //更改名称为mysql
[root@192 local]# chown -R mysql.mysql mysql/ //修改属主属组
[root@192 local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 6月 22 2021 bin
drwxr-xr-x. 2 root root 6 6月 22 2021 etc
drwxr-xr-x. 2 root root 6 6月 22 2021 games
drwxr-xr-x. 2 root root 6 6月 22 2021 include
drwxr-xr-x. 2 root root 6 6月 22 2021 lib
drwxr-xr-x. 3 root root 17 7月 1 06:45 lib64
drwxr-xr-x. 2 root root 6 6月 22 2021 libexec
drwxr-xr-x. 9 mysql mysql 129 7月 3 19:06 mysql
drwxr-xr-x. 2 root root 6 6月 22 2021 sbin
drwxr-xr-x. 5 root root 49 7月 1 06:45 share
drwxr-xr-x. 2 root root 6 6月 22 2021 src
[root@192 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@192 local]# source /etc/profile.d/mysql.sh
[root@192 local]# which mysql
/usr/local/mysql/bin/mysql //配置环境变量
[root@192 local]# cd mysql/
[root@192 mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@192 mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql //创建软连接
[root@192 mysql]# vim /etc/ld.so.conf.d/mysql.conf //配置lib库
[root@192 mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@192 mysql]# vim /etc/man_db.conf //添加man文档
MANDATORY_MANPATH /usr/local/mysql/man
//创建数据库目录存放文件
[root@192 ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@192 ~]# chown -R mysql.mysql /opt/data/
[root@192 ~]# ll /opt/data/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 7月 3 19:21 3306
drwxr-xr-x. 2 mysql mysql 6 7月 3 19:21 3307
drwxr-xr-x. 2 mysql mysql 6 7月 3 19:21 3308
2.配置多实例
//mysql初始化
//3306
[root@192 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306
2022-07-03T11:23:32.197007Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T11:23:32.508362Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T11:23:32.603687Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T11:23:32.687950Z 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: 924ec9b7-fac2-11ec-902c-000c291f07bc.
2022-07-03T11:23:32.690049Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T11:23:33.410250Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:23:33.410287Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:23:33.411445Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T11:23:33.828717Z 1 [Note] A temporary password is generated for root@localhost: ChSdk6kudh!u
[root@192 ~]# echo 'ChSdk6kudh!u' > 3306
//3307
[root@192 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307
2022-07-03T11:24:06.138367Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T11:24:06.391342Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T11:24:06.452123Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T11:24:06.527877Z 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: a67a59eb-fac2-11ec-8ef8-000c291f07bc.
2022-07-03T11:24:06.529278Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T11:24:07.156304Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:24:07.156321Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:24:07.156867Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T11:24:07.304598Z 1 [Note] A temporary password is generated for root@localhost: 6xhwKdt_rc#n
[root@192 ~]# echo '6xhwKdt_rc#n' > 3307
//3308
[root@192 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308
2022-07-03T11:24:45.260949Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-03T11:24:45.488489Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-03T11:24:45.538620Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-03T11:24:45.594227Z 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: bdc36793-fac2-11ec-97c4-000c291f07bc.
2022-07-03T11:24:45.597135Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-03T11:24:46.640785Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:24:46.640899Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-03T11:24:46.641371Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-03T11:24:46.822969Z 1 [Note] A temporary password is generated for root@localhost: ;oqx5).fwvtF
[root@192 ~]# echo ';oqx5).fwvtF' > 3308
安装软件包,配置my.cnf文件
[root@localhost ~]# yum -y install perl //执行脚本需要
[root@localhost ~]# yum -y install ncurses-compat-libs //连接mysql需要
//配置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
启动实例
[root@192 ~]# mysqld_multi start 3306
[root@192 ~]# mysqld_multi start 3307
[root@192 ~]# mysqld_multi start 3308
[root@192 ~]# 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@192 ~]# mysql -uroot -p'ChSdk6kudh!u' -S /tmp/mysql3306.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)
//3307修改密码
[root@192 ~]# mysql -uroot -p'6xhwKdt_rc#n' -S /tmp/mysql3307.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//3308修改密码
[root@192 ~]# mysql -uroot -p';oqx5).fwvtF' -S /tmp/mysql3308.sock
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)
设置开机自启
[root@192 ~]# cd /usr/lib/systemd/system
[root@192 system]# cp sshd.service 3306.service
[root@192 system]# cat 3306.service
[Unit]
Description=3306 server daemon
After=network.target sshd-keygen.target
Wants=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
关闭selinux并且设置软连接
//关闭selinux
[root@192 ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
//设置软连接
[root@192 ~]# which my_print_defaults
/usr/local/mysql/bin/my_print_defaults
[root@192 ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin/
测试
//可以正常启动和关闭
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl start 3306.service
[root@192 ~]# systemctl enable --now 3306.service
Created symlink /etc/systemd/system/multi-user.target.wants/3306.service → /usr/lib/systemd/system/3306.service.
[root@192 ~]# systemctl status 3306.service
● 3306.service - 3306 server daemon
Loaded: loaded (/usr/lib/systemd/system/3306.service; enabled; vendor preset: d>
Active: active (running) since Sat 2022-07-02 12:26:01 CST; 23s ago
Main PID: 251609 (mysqld_safe)
Tasks: 28 (limit: 4596)
Memory: 155.9M
CGroup: /system.slice/3306.service
├─251609 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/3>
└─251743 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datad>
//关闭
[root@192 ~]# systemctl stop 3306.service
[root@192 ~]# systemctl status 3306.service //查看状态,已经有开机自启
● 3306.service - 3306 server daemon
Loaded: loaded (/usr/lib/systemd/system/3306.service; enabled; vendor preset: d>
Active: failed (Result: exit-code) since Sat 2022-07-02 12:27:06 CST; 1s ago
Process: 257522 ExecStop=/usr/bin/ps -ef | grep 3306 | grep -v grep|awk {print $>
Main PID: 251609 (code=exited, status=0/SUCCESS)
同理配置3307与3308
[root@192 system]# cp 3306.service 3308.service
[root@192 system]# cp 3306.service 3307.service
//3307配置
[root@192 system]# cat 3307.service
[Unit]
Description=3307 server daemon
After=network.target sshd-keygen.target
Wants=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
//3308配置
[root@192 system]# cat 3308.service
[Unit]
Description=3308 server daemon
After=network.target sshd-keygen.target
Wants=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
//开机自启
[root@192 system]# systemctl enable --now 3307.service
Created symlink /etc/systemd/system/multi-user.target.wants/3307.service → /usr/lib/systemd/system/3307.service.
[root@192 system]# systemctl enable --now 3308.service
Created symlink /etc/systemd/system/multi-user.target.wants/3308.service → /usr/lib/systemd/system/3308.service.
二.MySQL主从配置
1.主从介绍
作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
主从形式
- 一主一从
- 主主复制
- 一主多从---扩展系统读取的性能,因为读是在从库读取的
- 多主一从---5.7开始支持
- 联级复制
2.主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3.主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
环境:
主数据库: ip:192.168.80.128
从数据库: ip:192.168.80.129
1.确保从数据库与主数据库里的数据一样
全备主库
//主机开启新终端进入mysql给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.14 sec)
//进行全备
[root@192 ~]# mysqldump -uroot -p'runtime123!' --all-databases > all-$(date '+%Y%m%d%H%M%S').sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@192 ~]# ls
all-20220703052259.sql initial-setup-ks.cfg anaconda-ks.cfg
//将数据库从主库传到从库
[root@192 ~]# scp all-20220703052259.sql 192.168.80.129:/root/
all-20220703052259.sql
//从库主机
[root@192 ~]# ls
all-20220703052259.sql anaconda-ks.cfg initial-setup-ks.cfg
//在从库将数据库恢复
[root@192 ~]# mysql -uroot -p'runtime123!' < all-20220703052259.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@192 ~]# mysql -uroot -p'runtime123!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
//此时从库与主库数据一致
2.在主数据库里创建一个同步账号授权给从数据库使用
mysql> CREATE USER 'qinyu'@'192.168.80.129' IDENTIFIED BY 'qinyu123';
Query OK, 0 rows affected (0.27 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'qinyu'@'192.168.80.129';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
3.配置主数据库
[root@192 ~]# vim /etc/my.cnf
[root@192 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id=10 //mysql服务器标识符,主库必须小于从库
log-bin=mysql_bin //启用binlog日志
[root@192 ~]# systemctl restart mysqld.service //重启mysql
//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4.配置从数据库
[root@192 ~]# vim /etc/my.cnf
[root@192 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id=20
relay-log=mysql-relay-bin
[root@192 ~]# systemctl restart mysqld.service
5.配置并且启动主从复制
//登录到从库
[root@192 ~]# mysql -uroot -p'runtime123!'
mysql> CHANGE MASTER TO
-> MASTER_HOST= '192.168.80.128', //主库的ip地址
-> MASTER_USER='qinyu', //配置主库授权的用户和密码
-> MASTER_PASSWORD='qinyu123',
-> MASTER_LOG_FILE='mysql_bin.000001',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
//关闭防火墙
[root@192 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@192 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2022-07-03 21:08:47 EDT; 1h 37min ago
Docs: man:firewalld(1)
Process: 1061 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 1061 (code=exited, status=0/SUCCESS)
//启动主从
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //为yes才算生效
Slave_SQL_Running: Yes //为yes才算生效
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 527
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_UUID: 4c6c9441-f9ae-11ec-8757-000c2996f1b8
Master_Info_File: /opt/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
测试
在主库随意创建一个库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.26 sec)
mysql> create database hhhhhhhh;
Query OK, 1 row affected (0.12 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| hhhhhhhh |
+--------------------+
6 rows in set (0.01 sec)
在从库查看
[root@192 ~]# mysql -uroot -p'runtime123!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| hhhhhhhh |
+--------------------+