码迷,mamicode.com
首页 > 数据库 > 详细

mysql主主同步

时间:2019-05-25 09:25:48      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:server-id   init   names   query   ons   sys   ini   mysql主主同步   base   

Mysql 主主同步方案

第一台机器主

[root@master ~]# vim /etc/my.cnf

[mysqld]

server-id=1

log-bin=mysql-binlog

log-slave-updates=true

max_binlog_size=1024M

auto_increment_offset = 1

auto_increment_increment = 2

 

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = test

replicate-ignore-db = mysql

 

max_connections = 3000

max_connect_errors = 30

 

skip-character-set-client-handshake

init-connect=‘SET NAMES utf8‘

character-set-server=utf8

wait_timeout=1800

interactive_timeout=1800

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

 

[root@master ~]# systemctl restart mariadb

 

[root@master ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 5.5.56-MariaDB MariaDB Server

 

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

 

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

 

MariaDB [(none)]> grant replication slave on *.* to ‘repl‘@‘192.168.30.24‘identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

MariaDB [(none)]> show master status;

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

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-binlog.000004 |      483 |              |                  |

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

1 row in set (0.00 sec)

 

 

2

 

[root@master1 ~]# vim /etc/my.cnf

[mysqld]

server-id=2

log-bin=mysql-binlog

log-slave-updates=true

max_binlog_size=1024M

auto_increment_offset = 2

auto_increment_increment = 2

 

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = test

replicate-ignore-db = mysql

 

max_connections = 3000

max_connect_errors = 30

 

skip-character-set-client-handshake

init-connect=‘SET NAMES utf8‘

character-set-server=utf8

wait_timeout=1800

interactive_timeout=1800

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

 

[root@master1 ~]# systemctl restart mariadb

[root@master1 ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 5.5.56-MariaDB MariaDB Server

 

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

 

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

 

MariaDB [(none)]> grant replication slave on *.* to ‘repl‘@‘192.168.30.25‘identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show master status;

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

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-binlog.000001 |      483 |              |                  |

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

1 row in set (0.00 sec)

 

 

 

测试环境,可以保证没数据写入,否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA数据表--> masterB 导入数据--> masterB设置主从-->查看主从

1台机器

 

[root@master ~]# mysql -u root

MariaDB [(none)]> stop slave;

MariaDB [(none)]> change master to  master_host=‘192.168.30.24‘,master_port=3306,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000001‘,master_log_pos=483;

 

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G;

 

 

 

2台机器

[root@master1 ~]# mysql -u root

MariaDB [(none)]> stop slave;

MariaDB [(none)]> change master to

    -> master_host=‘192.168.30.25‘,master_port=3306,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000004‘,master_log_pos=483;

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G;

 

 

 

第一台机器

MariaDB [(none)]> create database test01;

第二台机器查看是否同步

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

| test01             |

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

 

第二台机器

 

MariaDB [(none)]> create database test02;

查看第一台机器

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

| test01             |

| test02             |

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

7 rows in set (0.00 sec)

mysql主主同步

标签:server-id   init   names   query   ons   sys   ini   mysql主主同步   base   

原文地址:https://www.cnblogs.com/zc1741845455/p/10921253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!