主从复制搭建过程
准备多实例:
第一步:搭建MySQL多实例
1)创建数据存储及my.cnf配置文件目录
[root@db01 system]# mkdir -p /data/{3307,3308}/data
2)编写my.cnf配置文件
[root@db01 system]# vim /data/3307/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql/
datadir=/data/3307/data/
pid-file=/data/3307/data/mysql.pid
socket=/data/3307/mysql.sock
server_id=7
port=3307
[mysql]
socket=/data/3307/mysql.sock
[root@db01 system]# vim /data/3308/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql/
datadir=/data/3308/data/
pid-file=/data/3308/data/mysql.pid
socket=/data/3308/mysql.sock
server_id=8
port=3308
[mysql]
socket=/data/3308/mysql.sock
3)将存储目录的属主修改为mysql
[root@db01 system]# chown -R mysql.mysql /data/
4)初始化数据
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data
5)编写启动文件
[root@db01 system]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmL
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@db01 system]# cp -a mysqld.service mysql3307.service
[root@db01 system]# cp -a mysqld.service mysql3308.service
[root@db01 system]# vim mysql3307.service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
[root@db01 system]# vim mysql3308.service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
6)启动数据库
[root@db01 system]# systemctl start mysql3307
[root@db01 system]# systemctl start mysql3308
[root@db01 system]# netstat -lntup | grep mysql
tcp6 0 0 :::3307 :::* LISTEN 12194/mysqld
tcp6 0 0 :::3308 :::* LISTEN 12305/mysqld
查看server_id
mysql -S /tmp/mysql.sock -e "select @@server_id"
mysql -S /data/3307/mysql.sock -e "select @@server_id"
mysql -S /data/3308/mysql.sock -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
| 6 |
+-------------+
第二步:编写配置文件
主库:
[root@db01 system]# vim /data/3307/my.cnf
server_id=7
log_bin = 3307-mysql-bin
从库:
[root@db01 system]# vim /data/3308/my.cnf
server_id=8
log_bin = 3308-mysql-bin
第三步:主库创建复制用户
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@‘172.16.1.%‘ identified by ‘123456‘"
第四步:将主库之前的数据备份到从库
主库:
mysqldump -uroot -p123456 -S /data/3307/mysql.sock -A --master-data=2 --single-transaction -R -E --triggers >/tmp/full.sql
从库:
[root@db01 system]# mysql -uroot -p123456 -S /data/3308/mysql.sock
mysql> set sql_log_bin=0;
mysql> source /tmp/full.sql
第五步:查看连接主库需要哪些参数,并建立连接,实现主从
mysql> help change master to
CHANGE MASTER TO
MASTER_HOST=‘master2.example.com‘,
MASTER_USER=‘replication‘,
MASTER_PASSWORD=‘password‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘master2-bin.001‘,
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
mysql> show master status; 获取主库的binlog信息及位置信息
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| 3307-mysql-bin.000001 | 475 | | | |
+-----------------------+----------+--------------+------------------+-------------------+
与主库建立连接
mysql> CHANGE MASTER TO
MASTER_HOST=‘172.16.1.51‘,
MASTER_USER=‘repl‘,
MASTER_PASSWORD=‘123456‘,
MASTER_PORT=3307,
MASTER_LOG_FILE=‘3307-mysql-bin.000001‘,
MASTER_LOG_POS=475,
MASTER_CONNECT_RETRY=10;
第六步:启用主从机制
mysql> start slave; 开启复制线程
mysql> reset slave all; 重新建立主从复制
第七步:检查主从复制状态
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.51
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: 3307-mysql-bin.000001
Read_Master_Log_Pos: 475
Relay_Log_File: db01-relay-bin.000002
Relay_Log_Pos: 325
Relay_Master_Log_File: 3307-mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@db01 ~]# mysql -urepl -p123456 -h 172.16.1.51 -P3307 测试主库能否登录
mysql> show processlist; 查看当前线程情况
mysql> show full processlist \G
+--+-----------+---------+----+-------+----+---------------------------------+----------------+
|Id|User |Host |db |Command|Time|State | Info |
+--+-----------+---------+----+-------+----+---------------------------------+----------------+
|10| root |localhost|NULL|Query | 0|starting |show processlist|
|13|system user| |NULL|Connect| 551|Waiting for master to send event | NULL |
|14|system user| |NULL|Connect| 450|Slave has read all relay log; | NULL |
+--+-----------+---------+----+-------+----+---------------------------------+----------------+
主从复制监控主要用于查看或判断主从复制的同步情况是否有报错,是否有延迟
mysql> show slave status \G --- 查看从库状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event --- 等待新的日志数据
主库相关信息:
Master_Host: 172.16.1.51
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: 3307-mysql-bin.000002
Read_Master_Log_Pos: 154
从库relay应用信息
Relay_Log_File: db01-relay-bin.000005
Relay_Log_Pos: 377
Relay_Master_Log_File: 3307-mysql-bin.000002
从库线程运行状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
过滤复制有关的信息
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
从库延时主库的时间(秒)
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: 7
Master_UUID: fa38d758-78d1-11ea-9a58-000c29aaffc0
Master_Info_File: /data/3308/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:
GTID复制有关的状态信息
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version: