夜勿眠 发表于 2018-10-11 06:53:32

Mysql数据库主从同步简单配置

  一、主从同步:(A--->B)
  master:192.168.71.128
  slave:192.168.71.138
  1、Master配置:
  vi /etc/my.cnf
  server-id       = 1
  log-bin=mysql-bin启用二进制日志
  binlog-ignore-db = mysql   不同步mysql库
  授权从数据库192.168.71.138使用账号rsync密码bobo365同步主数据库所有数据

  mysql> grant replication slave on *.* to rsync@'192.168.71.138'>  Query OK, 0 rows affected (0.01 sec)
  刷新权限
  mysql> flush privileges;
  Query OK, 0 rows affected (0.00 sec)
  设置表为只读状态
  mysql> flush tables with read lock;
  Query OK, 0 rows affected (0.00 sec)
  记录file和position值,slave端要用到该数值。
  mysql> show master status;
  +------------------+----------+--------------+------------------+
  | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  +------------------+----------+--------------+------------------+
  | mysql-bin.000025 |      541 |            |                  |
  +------------------+----------+--------------+------------------+
  1 row in set (0.00 sec)
  解锁数据库只读。
  mysql> unlock tables;
  Query OK, 0 rows affected (0.00 sec)
  2、Slave配置:
  vim /etc/my.cnf
  server-id = 2
  mysql> stop slave;
  Query OK, 0 rows affected (0.00 sec)
  mysql> change master to master_host='192.168.71.128',master_user='rsync',master_
  password='bobo365',master_log_file='mysql-bin.000025',master_log_pos=541;
  Query OK, 0 rows affected (0.01 sec)
  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.71.128
  Master_User: rsync
  Master_Port: 3306
  Connect_Retry: 60
  Master_Log_File: mysql-bin.000025
  Read_Master_Log_Pos: 886
  Relay_Log_File: localhost-relay-bin.000002
  Relay_Log_Pos: 598
  Relay_Master_Log_File: mysql-bin.000025
  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: 886
  Relay_Log_Space: 758
  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
  1 row in set (0.00 sec)
  3、测试:
  master:
  mysql> create database m2s;
  Query OK, 1 row affected (0.00 sec)
  mysql> use m2s;
  Database changed
  mysql> create table m2s_tb(id int(2),name char(10));
  Query OK, 0 rows affected (0.01 sec)
  mysql> insert into m2s_tb values(001,'todu');
  Query OK, 1 row affected (0.00 sec)
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  |bb                |
  | db_nagiosql_v3   |
  | m2s                |
  | mysql            |
  | performance_schema |
  | testcopy         |
  +--------------------+
  7 rows in set (0.00 sec)
  Slave:
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | cactidb            |
  | collabtive         |
  | db_nagiosql_v3   |
  | m2s                |
  | my_wiki            |
  | mysql            |
  | performance_schema |
  | test               |
  | testcopy         |
  +--------------------+
  10 rows in set (0.00 sec)
  mysql> use m2s;
  Database changed
  mysql> select * from m2s_tb;
  +------+------+

  |>  +------+------+
  |    1 | todu |
  +------+------+
  1 row in set (0.00 sec)
  
  二、双向同步:(AB)
  使用主主前提:
  A、表的主键自增
  B、程序写库指定ID
  双主互为主从:
  解决主键自增长变量冲突:
  Master1:
  auto_increment_increment = 2#自增ID的间隔,如 1 3 5间隔为2.
  auto_increment_offset      = 1
  Master2:
  auto_increment_increment = 2#自增ID的间隔,如 2 4 6间隔为2.
  auto_increment_offset      = 2
  存在ID不连续问题:
  ----> 1 3 5 6 8 10 11 13 15
  互为主从参数:
  log-bin = /data/3307/mysql-bin
  log-slave-updates#开启从库binlog日志
  三、二次学习内容补充(对比学习)
  环境:
  3306 主
  3307 从
  主库操作:
  开启binlog:log-bin = /data/3306/mysql-bin

  更改server>  添加复制用户:

  grant replication slave on *.* to rep@'192.168.1.%'>  加读锁:
  flush table with read lock;
  show master status;
  mysql-bin.000011 |    43275
  show master logs;
  窗口不关,重新开窗口dump数据:
  mysqldump -S /data/3306/mysql.sock -uroot -pbobo365 -A -B -E --master-data=2 > /home/rep.sql
  或者直接锁表:
  mysqldump -S /data/3306/mysql.sock -uroot -pbobo365 -A -B -E -X --master-data=2 > /home/rep.sql
  或者--master-data=1
  则从库不用如下参数:
  MASTER_LOG_FILE='mysql-bin.000011',
  MASTER_LOG_POS=43275;
  解锁:
  unlock tables;
  从库:
  #log-bin = /data/3307/mysql-bin
  server-id = 3
  把主库备份文件rep.sql灌入主库:
  source /home/rep.sql
  登录到从库执行:
  CHANGE MASTER TO
  MASTER_HOST='192.168.1.61',
  MASTER_PORT=3306,
  MASTER_USER='rep',
  MASTER_PASSWORD='bobo365',
  MASTER_LOG_FILE='mysql-bin.000011',
  MASTER_LOG_POS=43275;
  查看mster info文件:
  # more master.info
  23
  mysql-bin.000011
  43275
  192.168.1.61
  rep
  bobo365
  3306
  开启开关:
  start slave;
  检查:
  show slave status\G
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  附录:
  对比第一次,第二次增加遗漏关键步骤:
  在主库做全备,在从库做全备导入。

页: [1]
查看完整版本: Mysql数据库主从同步简单配置