mysql复制-mysql双主热备实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16399991/article/details/82771584

在配置双主热备前 需要先了解 mysql主从复制原理 :https://blog.csdn.net/qq_16399991/article/details/82749333 和 mysql搭建 主从复制https://blog.csdn.net/qq_16399991/article/details/82740881 。

双主热备架构图。

在 mysql搭建 主从复制https://blog.csdn.net/qq_16399991/article/details/82740881 中我已经搭建了mysql主从复制,3307端口为主机,3308为从机。

1.slave开启binlog

打开3308端口数据的配置,新加 log-bin  、 binlog_format 和log_slave_updates  配置项。

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3308]vim my.cnf 

...........
# The MySQL server
[mysqld]
port            = 3308
.........

relay_log = mysql-relay-bin-3308
# binary logging format - mixed recommended
log-bin=mysql-bin-3308
binlog_format=mixed
log_slave_updates=1
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 108

 log-bin 代表bin-log日志的名称, binlog_format是bin-log日志的格式。log_slave_updates =1 是代表通过主机同步过来的数据操作也写入到bin-log 中,这样就能保证 从库的bin-log和主库的bin-log是一致的。编辑完成后,保存退出。重启3308端口数据库。

2.从机授权主机复制

连接上3308从机数据库,授权复制功能:

root@i:[/usr/local/data/mysqlCluster/mysql3308]mysql -S /tmp/mysql3308.sock -P 3308
Welcome to the MySQL monitor.  Commands end with ; or \g.
..........

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

mysql> grant replication slave on *.* to master3307@localhost identified by  '123456';
Query OK, 0 rows affected (0.00 sec)

3.配置3307端口主机 开启relay_log

打开3307端口主机配置文件 ,配置3307主机开启relay_log

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3307]vim my.cnf 

#
...................
# The password the slave will authenticate with when connecting to
# the master - required

# The MySQL server
[mysqld]
port            = 3307
socket          = /tmp/mysql3307.sock
...............
..................

log-bin=mysql-bin-3307  #mysql-bin-3307 日志文件的名称。 

# binary logging format - mixed recommended
binlog_format=mixed

relay_log=msyql-relay-log-3307
log_slave_updates=1

server-id       = 107

新加配置 relay_log=msyql-relay-log-3307 开启relay_log  和 log_slave_updates=1 。保存退出。重启3307端口数据库。

扫描二维码关注公众号,回复: 3608214 查看本文章

4.查看3308端口数据库master信息 并配置3307端口数据库开启监听

查看3308 master 信息

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3308]mysql -S /tmp/mysql3308.sock -P 3308
Welcome to the MySQL monitor.  Commands end with ; or \g.
....................

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

mysql> show master status;
+-----------------------+----------+--------------+------------------+
| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------------+----------+--------------+------------------+
| mysql-bin-3308.000003 |      107 |              |                  |
+-----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

配置3307 端口数据 开始监听

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3307]mysql -S /tmp/mysql3307.sock -P 3307
Welcome to the MySQL monitor.  Commands end with ; or \g.
..........
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to
    -> master_host='localhost',
    -> master_port=3308,
    -> master_user='master3307',
    -> master_password='123456',
    -> master_log_file='mysql-bin-3308.000003',
    -> master_log_pos=107;
Query OK, 0 rows affected (0.06 sec)

### 此时3307 数据库已经成为了3308数据库的从机

##开启复制
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

此时3307 和3308 已经互为主从了。

测试

1.在3308端口库插入一条数据

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3308]mysql -S /tmp/mysql3308.sock -P 3308
Welcome to the MySQL monitor.  Commands end with ; or \g.
........
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| abc            |
+----------------+
1 row in set (0.00 sec)

mysql> desc abc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| title | varchar(20) | NO   |     | NULL    |                |
| des   | varchar(40) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)

mysql> insert into abc (title, des) value ('1', '1');
Query OK, 1 row affected (0.00 sec)

在3307数据查看是否复制成功

root@iZ23iuzu9fvZ:[/usr/local/data/mysqlCluster/mysql3307]mysql -S /tmp/mysql3307.sock -P 3307
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.34-log Source distribution
.......

mysql> use test;
Database changed
mysql> select * from abc;
+----+-------+-----+
| id | title | des |
+----+-------+-----+
|  1 | 1     | 1   |
+----+-------+-----+
1 row in set (0.00 sec)

##可以看到数据已经复制完成

同样在3307插入数据 3308数据库同样复制完成,至此双主备份 配置完成。

猜你喜欢

转载自blog.csdn.net/qq_16399991/article/details/82771584