首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Mysql主从复制

主服务器:

开启二进制日志

配置唯一的server-id

获得master二进制日志文件名及位置

创建一个用于slave和master通信的用户账号

从服务器:

配置唯一的server-id

使用master分配的用户账号读取master二进制日志

启用slave服务

1.主从数据库版本最好一致

2.主从数据库内数据保持一致

主数据库:192.168.24.168/linux

从数据库:192.168.24.169/linux

主数据库master修改:

1.修改mysql配置

找到主数据库的配置文件my.cnf(或者my.ini),我的在/etc/mysql/my.cnf,在[mysqld]部分插入如下两行:

[mysqld]log-bin=mysql-bin #开启二进制日志

server-id=1 #设置server-id

2.重启mysql,创建用于同步的用户账号

GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.24.169' identified by 'xyw';

3.查看master状态,记录二进制文件名(mysql-bin.000003)和位置(73):

mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 120 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

从服务器slave修改:

1.修改mysql配置

同样找到my.cnf配置文件,添加server-id

[mysqld]

server-id=2#设置server-id,必须唯一

2.重启mysql,打开mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):

CHANGE MASTER TO

MASTER_HOST='192.168.24.168',

MASTER_USER='rep1',

MASTER_PASSWORD='xyw',

MASTER_LOG_FILE='mysql-bin.000001',

MASTER_LOG_POS=120;

3.启动slave同步进程:

mysql>start slave;

4.查看slave状态:

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.24.168

Master_User: rep1

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 120

Relay_Log_File: localhost-relay-bin.000002

Relay_Log_Pos: 283

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:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 120

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: 1

Master_UUID: 0556131b-037d-11e9-b99a-000c296c8478

Master_Info_File: /usr/local/mysql/data/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)

ERROR:

No query specified

当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了,比如在主master数据库的test数据库的一张表中插入一条数据,在slave的test库的相同数据表中查看是否有新增的数据即可验证主从复制功能是否有效,还可以关闭slave(mysql>stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了

master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的[mysqld]可添加修改如下选项:

# 不同步哪些数据库

binlog-ignore-db = mysql

binlog-ignore-db = test

binlog-ignore-db = information_schema

# 只同步哪些数据库,除此之外,其他不同步

binlog-do-db = moon_xyw_user

如之前查看master状态时就可以看到只记录了test库,忽略了manual和mysql库。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190129G0NASC00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券