在工作中经常碰到需要做mysql热备配置的时候,记录下。
1.主库A:192.168.1.2, 从库B: 192.168.1.3
第一步:配置主库,进入到配置文件中,一般是/etc/my.cnf,找到[mysqld]节点,在该节点最尾部增加以下内容
server-id=1# master server id 此处为短横线,不是下划线
log-bin=mysql-bin # 开启binlog日志,如果已存在,则忽略,热备必须
binlog_do_db=license # 需要binlog做记录的数据库,多个数据库可以用逗号隔开,或者写多条该binlog_do_db配置项
binlog_ignore_db=mysql # 不需要做记录的数据库,同binlog_do_db配置规则一样
配置完成后保存,然后重启数据库服务
然后登录数据库,查看状态
GRANT REPLICATION SLAVE ON *.* TO "rebei"@"192.168.1.3" IDENTIFIED BY "123456";# 增加一个只有热备权限的用户
flush privileges; #刷新权限
###
#保险起见,到从库上通过热备账户远程连接到主库,看是否能正常登录,否则就需要修改该热备账号,直到能正常登录
###
show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 154
Binlog_Do_DB: license
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
此处需要记录下file以及position两个参数在从库配置的时候需要用到。
第二步:配置从库
首先一样,修改配置文件,和主库配置基本相似,只是server-id不能为1,其他基本相似,按需配置,重启服务
然后就是登录到从库
stop slave;
change master to master_host="192.168.1.2",master_port=3306,master_user="rebei",master_password="123456",master_log_file="mysql-bin.000003",master_log_pos=154; #这里的ip是主库ip,端口为主库数据库端口,user是创建的热备账号,password是热备账号密码,log_file是主库上查看状态中的file参数,log_pos是主库查看状态的position参数
start slave;
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.8.1.180
Master_User: replicense
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 422
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: license
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: 732
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_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
这里只需要关注Slave_IO_Running和Slave_SQL_Running参数是yes就说明成功!
特别说明,从库上是不允许修改的,否则主从节点不一致,就会导致同步失败
如果失败,从新查看master状态,然后将从库重新设置以下就OK了,但是数据可能不一致。