mysql主主同步

Posted zc1741845455

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主主同步相关的知识,希望对你有一定的参考价值。

mysql 主主同步方案

第一台机器主

[[email protected] ~]# 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

 

[[email protected] ~]# systemctl restart mariadb

 

[[email protected] ~]# 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

 

[[email protected]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

 

[[email protected] ~]# systemctl restart mariadb

[[email protected]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台机器

 

[[email protected] ~]# 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台机器

[[email protected] ~]# 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主主同步的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主主数据同步

MySQL主主数据同步

mysql主主同步

Keepalived +Mysql 主主同步

Mysql的主主备份与主从备份--同步

mysql主从同步/主主同步