mysql数据库AB复制如何配置

62次阅读
没有评论

共计 5546 个字符,预计需要花费 14 分钟才能阅读完成。

这篇文章主要介绍了 mysql 数据库 AB 复制如何配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。

使用俩台 mysql 服务器实现 AB,主从复制。
 
一、在主MASTER服务器配置
 
MASTER  172.16.1.3
BACKUP 172.16.1.2
 
 1、编辑 my.cnf 文件
 # 在原有基础上添加这俩行
 
[root@zhaoyun ~]# cat /etc/my.cnf
[mysqld]
log-bin=/mysql/bin  #开启 binlog
server-id=1  #配置不和另一台重复就行
2、重启服务
[root@zhaoyun ~]# service mysqld restart
停止 MySQL:[确定]
启动 MySQL:[确定]
3、授权用户
mysql grant replication slave on *.* to zhaoyun@172.16.1.2 identified by 123456
[root@zhaoyun ~]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77-log Source distribution
Type help; or \h for help. Type \c to clear the buffer.
mysql
mysql grant replication slave on *.* to zhaoyun@172.16.1.2 identified by 123456
Query OK, 0 rows affected (0.15 sec)
mysql
4、在 B 服务器测试是否可以登录
[root@BACKUP ~]# mysql -uzhaoyun -p123456 -h272.16.1.3
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.77-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type help; or \h for help. Type \c to clear the current input statement.
mysql show grants ;
+—————————————————————————————————-+
| Grants for zhaoyun@172.16.1.2  |
+—————————————————————————————————-+
| GRANT REPLICATION SLAVE ON *.* TO zhaoyun @ 172.16.1.2 IDENTIFIED BY PASSWORD 565491d704013245 |
+—————————————————————————————————-+
1 row in set (0.00 sec)
mysql
5、查看 master 的状态
mysql show master status ;
+————+———-+————–+——————+
| File  | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+————+———-+————–+——————+
| bin.000001 |  315  |  |  |
+————+———-+————–+——————+
1 row in set (0.00 sec)
 
file 字段是 master 的 binlog 文件名,position 是 binlog 的节点。
二、配置 BACKUP
1、编辑配置文件 my.cnf, 添加 4 行。
[mysqld]
server-id=2
master-host=172.16.1.3  #MASTER 服务器的 ip 地址
master-user=zhaoyun  #连接 MASTER 服务器的用户名
master-password=123456  #密码
2、重启服务
[root@BACKUP ~]# service mysqld restart
Stopping mysqld:  [  OK ]
Starting mysqld:  [  OK ]
3、重启服务后会在目录下生成几个文件
[root@BACKUP]# ls
   ib_logfile1  mysqld-relay-bin.000001  mysqld-relay-bin.index  test
ibdata1  master.info  mysql.sock
ib_logfile0  mysql  relay-log.info
[root@BACKUP mysql]# pwd
/var/lib/mysql
mysqld-relay-bin.000001  #binload 文件,从 master 复制而来
mysqld-relay-bin.index  #binload 的信息
master.info  #master 信息
 relay-log.info  #中继日志信息
4、查看 slave 的状态
[root@BACKUP ~]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.77 Source distribution
Type help; or \h for help. Type \c to clear the buffer.
mysql show slave status \G ;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
  Master_Host: 172.16.1.3
  Master_User: zhaoyun
  Master_Port: 3306
  Connect_Retry: 60
  Master_Log_File: mysqld-bin.000001
  Read_Master_Log_Pos: 315
  Relay_Log_File: mysqld-relay-bin.000002
  Relay_Log_Pos: 453
  Relay_Master_Log_File: mysqld-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:
  Last_Errno: 0
  Last_Error:
  Skip_Counter: 0
  Exec_Master_Log_Pos: 315
  Relay_Log_Space: 453
  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
1 row in set (0.00 sec)
ERROR:
No query specified
mysql
# 这个是主服务的 binlog 文件的状态,如果出现 IO 是 NO 的话,需检查这俩个文件的状态。
  Master_Log_File: mysqld-bin.000001
  Read_Master_Log_Pos: 315
 
 # 这俩条是 slave 的 IO 进程,和 SQL 进程的状态,AB 复制的服务只有都为 yes 时才可用。
 
  Slave_IO_Running: YES
  Slave_SQL_Running: YES
#IO 进程为 NO 可以将 BACKUP 的数据文件删除,重启服务重新同步就行了。
 
5、到现在配置基本完成
 
三、创建一个表进行测试,是否同步成功。
1、在 master 上创建。
mysql create database master ;
Query OK, 1 row affected (0.00 sec)
mysql use master
Database changed
mysql create table master(id int,name char(5));
Query OK, 0 rows affected (0.04 se
2、在 backup 查看
[root@BACKUP ~]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.77 Source distribution
Type help; or \h for help. Type \c to clear the buffer.
mysql show database ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near database at line 1
mysql show databases ;
+——————–+
| Database  |
+——————–+
| information_schema |
| master  |
| mysql  |
| test  |
+——————–+
4 rows in set (0.00 sec)
mysql use master
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql show tables ;
+——————+
| Tables_in_master |
+——————+
| master  |
+——————+
1 row in set (0.00 sec)
mysql
可以看到数据已经同步过来了。到此实验成功。
 
故障排除:
 
IO 等于 NO:需要检查节点和 binlog 文件名是否和在 master 看到的一致,如果不一致可以手动改写
命令
先停止 slave 服务
mysql slave stop;
mysql change master to master_log_file= 在 master 看到的 binlog 文件名
mysql change master to master_log_pos=100; 这个数字是在 master 看到的。
mysql slave start ;
mysql show master status ;
+——————-+———-+————–+——————+
| File  | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————-+———-+————–+——————+
| mysqld-bin.000001 |  507 |  |  |
+——————-+———-+————–+——————+
1 row in set (0.00 sec)
SQL 等于 NO,可以试着删除几个文件重启服务重新同步
mysqld-relay-bin.000001  #binload 文件,从 master 复制而来
mysqld-relay-bin.index  #binload 的信息
master.info  #master 信息
 relay-log.info  #中继日志信息

感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“mysql 数据库 AB 复制如何配置”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-18发表,共计5546字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)