作者介绍:简历上没有一个精通的运维工程师。请点击上方的蓝色《运维小路》关注我,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。
数据库是一个系统(应用)最重要的资产之一,所以我们的数据库将从以下几个数据库来进行介绍。
MySQL**(本章节)**
PostgreSQL
MongoDB
Redis
Etcd
前面用了多个小节单独介绍主从详细原理是因为他是MYSQL里面最重要的技能点之一,基本面试必问,下面就来搭建基于Binlog搭建他最重要的应用:主从复制。
1.主节点
我们复用前面的master节点,可以不做任何调整。
# 开启binlog,并设置基础路径和文件名
log-bin=/data/mysql-binlogs/mysql-bin
# 设置binlog索引文件路径
log_bin_index=/data/mysql-binlogs/mysql-bin.index
# 必须设置server-id
server-id=1
# 设置binlog格式(推荐ROW格式)
binlog_format=ROW
# 设置binlog过期时间(天)
expire_logs_days=2
# 设置单个binlog文件大小
max_binlog_size=5M
2.从节点配置
这里的配置添加了部分从部分专属配置。
# 必须设置server-id,且必须与主库不同
server-id=2
# 从库也可以开启binlog(推荐),便于后续级联复制或故障切换
log-bin=/data/mysql-binlogs/mysql-bin
log_bin_index=/data/mysql-binlogs/mysql-bin.index
# 设置binlog格式,建议与主库保持一致
binlog_format=ROW
# 从库特有的复制配置
# 忽略复制某些系统库的错误(可选)
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
# 中继日志配置
relay-log=/data/mysql-relaylogs/mysql-relay-bin
relay-log-index=/data/mysql-relaylogs/mysql-relay-bin.index
relay-log-info-file=relay-log.info
# 设置从库为只读(防止误操作)
read-only=1
# 二进制日志过期时间
expire_logs_days=2
max_binlog_size=5M
# 复制过滤规则(根据实际需求调整)
# replicate-do-db=需要复制的数据库名
# replicate-ignore-db=需要忽略的数据库名
# 从库性能优化参数
slave-parallel-workers=4
slave-parallel-type=LOGICAL_CLOCK
3.将主库的内容备份还原到从库
我们这里复用前面的恢复数据的库作为从库,所有不额外导入数据。一般搭建主从也是会在数据库部署阶段,很少中途从单机变成主从的。
4.创建主从账号(主库操作)
如果是正在使用的数据库,还需要进行锁表以后,再记录这个Binlog日志的位置,否则容易出现数据误差。这里需要注意的就是允许连接ip地址。
mysql> CREATE USER 'repl'@'192.168.31.182' IDENTIFIED BY 'A2ecure_password';
Query OK, 0 rows affected (0.05 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.31.182';
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> -- 查看主库状态,记录File和Position
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 621 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
5.从库连接主库(从库操作)
这里填写的就是主库创建的账号和当前Binlog的位置。
mysql> -- 配置复制连接
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.31.181',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='A2ecure_password',
-> MASTER_LOG_FILE='mysql-bin.000006', -- 主库show master status的结果
-> MASTER_LOG_POS=621; -- 主库show master status的结果
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql>
mysql> -- 启动复制
mysql> START SLAVE;
Query OK, 0 rows affected (0.11 sec)
6.检查主从状态(从库操作)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这2个线程都是Yes状态,才证明这个主从是OK的,IO线程代表从节点正常从主节点拉取Binlog并写入本地中继日志,SQL线程则是正确把中继日志转换成功sql语句写入到数据库。
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.181
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 621
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 621
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: 1
Master_UUID: 3f4e492c-917c-11f0-abff-5254005fe59e
Master_Info_File: /var/lib/mysql/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)
到这里我们的主从就算搭建完成,可以正常使用,如果主的写的数据特别繁忙,则可能从节点的数据会延迟一点主节点的数据。
运维小路
一个不会开发的运维!一个要学开发的运维!一个学不会开发的运维!欢迎大家骚扰的运维!
关注微信公众号《运维小路》获取更多内容。