cyc1111 发表于 2018-10-8 11:47:44

mysql双主同步配置

  一.双主复制实现方案
  1.让表的ID自增,然后主1写1、3、5,主2写2、4、6
  2.不让表的ID自增,然后通过web端程序去seq服务器取ID,写入双主
  双主工作场景为高并发写的场景,慎用。
  二.双主配置,以多实例3306和3308为例


[*]  在3306上创建一个专门用来同步数据库的用户

  mysql> grant replicationslave on *.* to rep@'10.0.0.%'>  ###*.*代表所有库和所有表
  mysql> flush privileges;
  mysql> select user,hostmysql.user;
  mysql> show grants forrep@'10.0.0.%';
  2.两个数据库的my.cnf文件里的mysqld模块添加如下黄色部分
  3306配置
  # sed -n '36,43p'/data/3306/my.cnf
  #log-slow-queries =/data/3306/slow.log
  pid-file = /data/3306/mysql.pid
  log-bin = /data/3306/mysql-bin
  auto_increment_increment = 2   #自增ID的间隔,如1 3 5间隔为2
  auto_increment_offset    = 1    #ID的初始位置
  log-slave-updates
  relay-log =/data/3306/relay-bin
  relay-log-info-file =/data/3306/relay-log.info
  3308配置
  # sed -n '36,43p'/data/3308/my.cnf
  #log-slow-queries =/data/3308/slow.log
  pid-file = /data/3308/mysql.pid
  log-bin = /data/3308/mysql-bin
  auto_increment_increment = 2   #自增ID的间隔,如1 3 5间隔为2
  auto_increment_offset    = 2   #ID的初始位置
  log-slave-updates
  relay-log = /data/3308/relay-bin
  relay-log-info-file =/data/3308/relay-log.info
  3.重启两个数据库实例
  # /data/3306/mysql restart
  # /data/3308/mysql restart
  4.登陆数据库确认添加的内容是否生效
  3306
  # mysql -uroot-p123456 -S /data/3306/mysql.sock
  mysql> show variables like"log_%";
  +------------------------------------------------------+----------------------------------------------------+
  | Variable_name                  |Value                            |
  +-----------------------------------------------------+-----------------------------------------------------+
  | log_bin                        | ON                              |
  |log_bin_trust_function_creators      | OFF                           |
  | log_error                        | /data/3306/mysql_oldboy3306.err   |
  | log_output                     |FILE                              |
  |log_queries_not_using_indexes      | OFF                               |
  | log_slave_updates               | ON                               |
  | log_slow_queries                  |OFF                              |
  | log_warnings                     | 1                                 |
  +----------------------------------------------------+--------------------------------------------------------+
  8 rows in set (0.00 sec)
  mysql> show variables like"auto_%";
  +--------------------------+-------+
  | Variable_name            | Value |
  +--------------------------+-------+
  | auto_increment_increment | 2   |
  | auto_increment_offset   | 1   |
  | autocommit               | ON    |
  | automatic_sp_privileges| ON   |
  +--------------------------+-------+
  4 rows in set (0.00 sec)
  3308
  mysql> show variables like"log_%";
  +---------------------------------+---------------------------------+
  | Variable_name                   | Value                           |
  +---------------------------------+---------------------------------+
  | log_bin                         | ON                              |
  |log_bin_trust_function_creators | OFF                           |
  | log_error                     |/data/3308/mysql_oldboy3308.err |
  | log_output                      | FILE                            |
  |log_queries_not_using_indexes   |OFF                           |
  | log_slave_updates               | ON                              |
  | log_slow_queries                | OFF                           |
  | log_warnings                  | 1                               |
  +---------------------------------+---------------------------------+
  8 rows in set (0.00 sec)
  mysql> show variables like"auto_%";
  +--------------------------+-------+
  | Variable_name            | Value |
  +--------------------------+-------+
  | auto_increment_increment | 2   |
  | auto_increment_offset   | 2   |
  | autocommit               | ON    |
  | automatic_sp_privileges| ON   |
  +--------------------------+-------+
  4 rows in set (0.00 sec)
  二.主库3306备份数据,导入主库3308
  1.在主库3306上备份数据
  # mysqldump-uroot -p123456 -S /data/3306/mysql.sock -A --events -B -x --master-data=1|gzip>/opt/3306_$(date +%F).sql.gz
  -A:所有的库    -x:全局锁表
  2.将数据导入主库3308
  # ls /opt
  3306_2017-07-03.sql.gz
  # cd /opt
  # gzip -d 3306_2017-07-03.sql.gz
  # ls
  3306_2017-07-03.sql
  # mysql -uroot-p123456 -S /data/3308/mysql.sock
页: [1]
查看完整版本: mysql双主同步配置