这段时间觉得一个数据库会导致不高可用,所以想到做mysql的主从复制,用于提高mysql的高可用。记录一下方便复习。
1、需要两台centos7 内核的服务器。并且安装mysql服务。
2、两台服务器ip分别为192.168.1.187(主)、192.168.1.189(从)。修改两台mysql的配置 /etc/my.cnf文件添加如下配置:
master(主):
#mysql master1 config
[mysqld]
server-id = 1 # 节点ID,确保唯一
# log config
log-bin = mysql-bin #开启mysql的binlog日志功能
sync_binlog = 1 #控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
binlog_format = mixed #binlog日志格式,mysql默认采用statement,建议使用mixed
expire_logs_days = 7 #binlog过期清理时间
max_binlog_size = 100m #binlog每个日志文件大小
binlog_cache_size = 4m #binlog缓存大小
max_binlog_cache_size= 512m #最大binlog缓存大
binlog-ignore-db=mysql #不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
auto-increment-offset = 1 # 自增值的偏移量
auto-increment-increment = 1 # 自增值的自增量
slave-skip-errors = all #跳过从库错误
slave(从):
[mysqld]
server-id = 2
log-bin=mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
#这里主要要注意 replicate-wild-ignore-table这个参数,这个参数可以用来设置不需要拷贝的库;还有一个相对应的属性是用来设置只拷贝某个库。我一直这里配置错,导致配置不成功。
3、启动两个数据库,添加一个库,用于测试。我这里的库叫book。可以使用客户端,也可以使用工具连接。
4、配置master数据库:
CREATE USER repl_user IDENTIFIED BY 'repl_passwd'; # 我们一般不会在slave连接master中直接使用root用户,而是给他分配一个其他的账户。
#赋予该用户复制的权利
grant replication slave on *.* to 'repl_user'@'192.168.1.187' identified by 'repl_passwd';
#需要flush 数据库,是的配置生效
FLUSH PRIVILEGES;
select host,user from mysql.user; #查看用户是否创建成功
#使用命令查看master的信息,需要着重注意的参数是File,Position,这个在配置从库的时候需要。
show master status \G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 4431
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
5、配置slave(从) 数据库:
#如果懒得看slave的运行状态,最好先停止slave
stop slave;
#设置从库指向主库,是的两个数据库之间有桥梁
CHANGE MASTER TO
MASTER_HOST = '192.168.1.187',
MASTER_USER = 'repl_user',
MASTER_PASSWORD = 'repl_passwd',
MASTER_PORT = 3307,
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=120;
# MASTER_LOG_FILE='mysql-bin.000005',#与主库File 保持一致
# MASTER_LOG_POS=120 , #与主库Position 保持一致
#启动slave
start slave;
#如果在第二步的时候出现报错,可以刷新slave
reset slave;
#查看slave启动状态
show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.187
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4431
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
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: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 4431
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: 87fd8873-83b3-11ea-b3c4-000c292df525
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: 60
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)
# 其中最重要的两个指标是:Slave_IO_Running: Yes以及Slave_SQL_Running: Yes,如果都是yes,那么启动成功。
6、在master数据库book中创建一个表,并添加一条数据。打开slave数据库book,查看表和数据是否同步。