武英斌-博客

Mysql 主从数据同步 分布式数据存储

发布时间:5年前热度: 515 ℃评论数:

数据库想要实现主从,并且实现读写分离的话

例如TP配置了读写分离和主从

 'DB_TYPE' => 'mysqli',
    'DB_HOST' => '154.85.193.115,47.107.79.558',//数据库连接IP
    'DB_NAME' => 'sql_55_15_198_1',
    'DB_USER' => 'sql_55_15_198_1',
    'DB_PWD' => 'xxx',
    'DB_PORT' => '3306',
    'DB_PREFIX' => 'cmf_',
    'DB_CHARSET' => 'utf8mb4',
    "DB_DEPLOY_TYPE"=>1,//分布式配置
    "DB_RW_SEPARATE"=>true,//启用读写分离

如果不指定主库,tp默认会使用第一个会主库进行写,其他是从库,也可以配置多主多从

配置好这个后,主库插入数据还是不能同步到从库去,所以还需要Mysql配置下数据自动同步,原理是从服务器连接主服务器,利用binlog日志进行自动同步

主服务器操作

1.创建一个连接用户

mysql>grant replication slave on *.* to 'repl'@'192.168.0.109' identified by 'repl';

2.编辑my.cnf文件

server-id=1

并开启log-bin二进制日志文件(Mysql需要有/var/lib/mysql/目录的读写权限【可通过chown -R mysql:mysql /var/lib/mysql命令进行更改】

其他扩展配置项: 

binlog-do-db=mysql1 #需要备份的数据库名,如果备份多个数据库,重复设置这个选项 即可
binlog-ignore-db=mysql2 #不需要备份的数据库名,如果备份多个数据库,重复设置这 个选项即可
log-slave-updates=1 #这个参数一定要加上,否则不会给更新的记录些到二进制文件 里

slave-skip-errors=1 #是跳过错误,继续执行复制操作(可选)

重新启动mysql

3.得到binlog日志文件名和偏移量(此处记住File名称和Position值,后面slave服务器配置时需要用到

mysql> show master status;

从服务器操作

1.编辑my.cnf文件

rpl_semi_sync_slave_enabled=1 绑定主数据库的服务器ID

server-id=2 此数据服务器的id

replicate-do-db=xxx要同步的数据库

2.重启从数据库

3.从数据库进行相应设置

mysql> change master to
   -> master_host='192.168.0.107',
   -> master_user='repl',
   -> master_password='repl',
   -> master_log_file='mysql-bin.000001',
   -> master_log_pos=713;

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
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: 1079
Relay_Log_Space: 407
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:
1 row in set (0.00 sec)

ERROR:
No query specified


在这里主要是看:

  Slave_IO_Running=Yes
  Slave_SQL_Running=Yes

如果出现Slave_IO_Running: No或Slave_SQL_Running: NO,需要对从数据库进行相应设置

如果主从数据库的版本不一致,可能会出现

Got fatal error 1236 from master when reading data from binary log: 'Slave can not handle replication events with the checksum that master is configured to log; the first event 'mysql-bin.000006' at 29861, the last event read from './mysql-bin.000006' at 29861, the last byte read from './mysql-bin.000006' at 120.'

解决方法:mysql> set GLOBAL  binlog_checksum =none;

打完收工,现在主库插入数据,从库就会自动同步




Mysql,主从数据同步,分布式数据存储

手机扫码访问