mysql的热备方案目前能想到的就是互为主从了,一个down机的时候另外一个直接可用,恢复后数据也会同步,省去了人工同步,也免了同步的时候导致业务中断的问题。
1、准备环境:
2台已经装好 版本一致mysql 的主机,本文所采用mysql版本为 5.5.62
mysql-A: 10.0.0.1 database1
mysql-B: 10.0.0.2 database1
如果2台主机的mysql数据库内容不一致,先手动同步一次使得两台需要设置同步的数据库内容一致,避免后续出现问题。
2、设置 mysql-A
配置文件修改:
# /etc/my.cnf 修改添加如下内容 log-bin=mysql-bin #开启二进制日志 server-id = 1 auto_increment_offset=2 #步进值auto_imcrement。一般有n台主MySQL就填n auto_increment_increment=1 #起始值。一般填第n台主MySQL。此时为第一台主MySQL binlog-ignore=mysql #忽略的数据库 binlog-ignore=information_schema #忽略的数据库 replicate-do-db=database1 #需要同步的数据库
数据库中添加内容:
# 创建并授权 mysql-B 所需的账号,修改下方地址和密码 grant all on *.* to 'mysql-B_user'@'mysql-B的地址' identified by 'mysql-B_password'; flush privileges; # 重置master文件和计数 reset master; show master status # 获取master状态以便 mysql-B 设置(上方 reset master之后基本下方固定值) +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000001 | 107 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+ # 5.6.51 版本 reset master 之后的 position 值为 120,具体以命令执行结果为准,不reset好像也行。
3、 设置 mysql-B
配置文件修改:
# /etc/my.cnf 修改添加如下内容 log-bin=mysql-bin #开启二进制日志 server-id = 2 auto_increment_offset=2 #步进值auto_imcrement。一般有n台主MySQL就填n auto_increment_increment=2 #起始值。一般填第n台主MySQL。此时为第一台主MySQL binlog-ignore=mysql #忽略的数据库 binlog-ignore=information_schema #忽略的数据库 replicate-do-db=database1 #需要同步的数据库
数据库中添加内容:
# 创建并授权 mysql-A 所需的账号,修改下方地址和密码 grant all on *.* to 'mysql-A_user'@'mysql-A的地址' identified by 'mysql-A_password'; flush privileges; # 重置master文件和计数 reset master; show master status # 获取master状态以便 mysql-B 设置(上方 reset master之后基本下方固定值) +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000001 | 107 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+ # 设置 本机slave, 即指向 mysql-A 为 master,从其获取数据更新 stop slave; reset slave; change master to master_host='mysql-A的地址',master_user='mysql-B_user', master_password='mysql-B_password',master_log_file='mysql-bin.000001',master_log_pos=107; start slave; show slave status \G; #查看 slave运行状态 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: mysql-A的地址 Master_User: mysql-B_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 107 Relay_Log_File: mysql-B-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: master 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: 107 Relay_Log_Space: 412 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: 2 1 row in set (0.00 sec) ERROR: No query specified
4、 设置 mysql-A
# 设置 本机slave, 即指向 mysql-B 为 master,从其获取数据更新 stop slave; reset slave; change master to master_host='mysql-B的地址',master_user='mysql-A_user', master_password='mysql-A_password',master_log_file='mysql-bin.000001',master_log_pos=107; start slave; show slave status \G; #查看 slave运行状态 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: mysql-B的地址 Master_User: mysql-A_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 107 Relay_Log_File: mysql-A-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: master 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: 107 Relay_Log_Space: 412 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: 2 1 row in set (0.00 sec) ERROR: No query specified
至此, mysql-A 和 mysql-B 互相主从 备份设置完成,任意一台数据写入会同步到另外一台,一台down了之后,另外一台可设置使用,新增的数据待之前down了的up之后会自动同步。
参考:
https://blog.51cto.com/u_11573159/2410128
2023-04-03
新状况,从服务器挂了挺久,以至于从服务器读的bin文件编号都在主服务器不存在了,主服务器一直有数据写入,这时候要重新同步,或者重新做主从同步,在考虑不停主服务器的情况下,可以在使用 mysqldump 备份的时候,添加 --master-data 参数,让备份文件自动生成且记录备份的bin文件和pos位置,这样从服务器直接导入这个sql文件之后,再 slave start 就可以无缝对接从主服务器继续备份。
2023-12-19
从mysql如出现start slave 之后连接失败,切确认账户密码能连接的情况下,可能是权限不够,需要复制权限:
GRANT REPLICATION SLAVE ON *.* TO '从mysql使用的账户'@'从mysql的地址' IDENTIFIED BY '密码'; FLUSH PRIVILEGES;
© 著作权归作者所有
文章评论(0)