• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mysql主主复制_MySQL

mysql 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论
文章目录[隐藏]

MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。 下面是配置步骤 需要两台MySQL主机做服务器: MySQL的创建方法参见:创建MySQL数据库 Server-1:192.168.0.1 Server-2:192.168.0.2

一、创建并授权用户

这一步在每一台(主)服务器上创建一个用户,并为之授权,使它们可以互相访问彼此的数据库 在Server-1上: 创建一个充许Server-1来访问的用户server2,密码为:server2 mysql> GRANT REPLICATION SLAVE ON *.* > TO 'server2'@'192.168.0.2' IDENTIFIED BY 'server2'; 在Server-2上: 创建一个充许Server-1来访问的用户server1,密码为:server1 mysql> GRANT REPLICATION SLAVE ON *.* > TO 'server1'@'192.168.0.1' IDENTIFIED BY 'server1';

二、修改MySQL主配置文件

在MySQL的主配置文件中修改/添加如下内容: Server-1上: [mysqld] server-id = 10 log-bin = mysql-bin replicate-do-db = mydb auto-increment-increment = 2 auto-increment-offset = 1 # service mysqld restart Server-2上: [mysqld] server-id = 20 log-bin = mysql-bin replicate-do-db = mydb auto-increment-increment = 2 auto-increment-offset = 2 # service mysqld restart 注:二都只有server-id不同和 auto-increment- offset不同 auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突 replicate-do-db 指定同步的数据库,我们只在两台服务器间同步mydb数据库 另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2

三、复制其中一台服务器的数据库到别外一台服务器

这一步中谁做为源数据不重要,重要的是在正式的复制(Replication)开始之前两台服务都能准备的反应彼此的数据。 我们可以先从源数据库中导出数据到备份文件,这里我们使用mysqldump命令 以Server-1上数据库为源数据库 备份数据前先锁表,保证数据一致性 mysql> FLUSH TABLES WITH READ LOCK; > SHOW MASTER STATUS; +—————–+————+—————-+——————–+ |File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +—————–+————+—————-+——————–+ |mysql-bin.000006 | 213 | | | +—————–+————+—————-+——————–+ 1 row in set (0.01 sec) 该结果显示,源服务器现在处于6号二进制文件的213位置 开始备份数据库 # mysqldump –user=root -p mydb > /tmp/mydb.sql 备份完毕,现在可以解锁数据库表 mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)

四、将备份数据导入Server-2

先在Server-2上创建一个与mydb同名的空数据库 # mysql > CREATE DATABASE mydb; >\q # scp 192.168.0.1:/tmp/mydb.sql ./ # mysql -uroot -p mydb LOCK TALBES WITH READ LOCK; > SHOW MASTER STATUS; +—————–+————+—————-+——————–+ |File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +—————–+————+—————-+——————–+ |mysql-bin.000001 | 106 | | | +—————–+————+—————-+——————–+ 1 row in set (0.00 sec) > UNLOCK TABLES; > \q 五、互相通告二进制日志位置 在Server-1上: # mysql > CHANGE MASTER TO > MASTER_HOST='192.168.0.2', > MASTER_USER='master1', > MASTER_PASSWORD='master1', > MASTER_LOG_FILE='mysql-bin.000001', > MASTER_LOG_POS=106; 在Server-2上: # mysql > CHANGE MASTER TO > MASTER_HOST='192.168.0.1', > MASTER_USER='master2', > MASTER_PASSWORD='master2', > MASTER_LOG_FILE='mysql-bin.000006', > MASTER_LOG_POS=213;

六、启动复制(Replication)功能

在两台主机上分别执行 # mysql > START SLAVE; 配置到此完成!

测试:

在任意一台服务器上创建一个数据库 # mysql > CREATE DATABASE repltest; > USE repltest; > CREATE TABLE test( > name char(30)); > \q # 在另一台服务器上查看数据库信息: # mysql > SHOW DATABASES; 结果中将显示有一个名为replrtest的数据库

eg: 192.168.0.186 master1 192.168.0.167 master 2
MASTER 1 [mysql@master1~]$ cat /usr/local/mysql/my.cnf # For advice on how to change settings please see
[mysqld]
port=3308
skip-name-resolve
datadir=/cifpay/mysqldb
basedir=/usr/local/mysql
log-error=/cifpay/mysqldb/oracle11g.com.err
pid-file=/cifpay/mysqldb/oracle11g.com.pid
plugin-dir=/usr/local/mysql/lib/plugin
socket=/tmp/mysql.sock

# —-master—–#
server_id=1
log-bin=/cifpay/mysql-bin.log
binlog-ignore-db=mysql
auto-increment-increment=2
auto-increment-offset=1
sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ================ MASTER 2
[mysql@master2~]$ cat /usr/local/mysql/my.cnf # For advice on how to change settings please see
[mysqld]
port=3308
basedir=/usr/local/mysql
datadir=/cifpay/mysqldb
log-error=/cifpay/mysqldb/dominic.mysql1.err
pid-file=/cifpay/mysqldb/dominic.mysql1.pid
plugin-dir=/usr/local/mysql/lib/plugin
# —-master 2 ——-#
server-id=2
read_only=TURE
binlog-ignore-db=mysql
#log_slave_updates=1
log-bin=/cifpay/mysql-bin.log
auto-increment-increment=2
auto-increment-offset=2
sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

重启动 MASTER 1、MASTER 2 数据库,使配置生效:
MASTER 1 : mysql> change master to -> MASTER_HOST='192.168.0.167', -> MASTER_USER='copy1', –用户不要一样 -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000002', –来自 master 2 -> MASTER_LOG_POS=753, –来自 master 2 -> MASTER_PORT=3308; Query OK, 0 rows affected, 2 warnings (0.08 sec)
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> SHOW SLAVE STATUS \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.167 Master_User: copy1 Master_Port: 3308 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 753 Relay_Log_File: oracle11g-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000002 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: 753 Relay_Log_Space: 460 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 Master_UUID: 9b6fc45b-81b3-11e4-95f4-0800273e24cb Master_Info_File: /cifpay/mysqldb/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 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 1 row in set (0.00 sec)
MASTER 2
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.0.186', -> MASTER_USER='copy2', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000005', –来自master 1 -> MASTER_LOG_POS=216, –来自master 1 -> MASTER_PORT=3308; ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first mysql> STOP SLAVE; Query OK, 0 rows affected (0.03 sec)
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.0.186', -> MASTER_USER='copy2', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000005', -> MASTER_LOG_POS=216, -> MASTER_PORT=3308; Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> START SLAVE; Query OK, 0 rows affected (0.03 sec)
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.186 Master_User: copy2 Master_Port: 3308 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 548 Relay_Log_File: dominic-relay-bin.000002 Relay_Log_Pos: 615 Relay_Master_Log_File: mysql-bin.000005 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: 548 Relay_Log_Space: 790 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: 1fb88a50-81b1-11e4-95e4-080027a9d0f9 Master_Info_File: /cifpay/mysqldb/master.info SQL_Delay: 0 SQL_本文来源gao@!dai!ma.com搞$$代^@码5网@Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 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 1 row in set (0.00 sec)

–测试,OK!

–主主 -> 从: 按照主从一样配置


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mysql主主复制_MySQL

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址