1. 程式人生 > >mysql複製-mysql雙主熱備實現

mysql複製-mysql雙主熱備實現

雙主熱備架構圖。

1.slave開啟binlog

開啟3308埠資料的配置,新加 log-bin  、 binlog_format 和log_slave_updates  配置項。

[email protected]:[/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從機資料庫,授權複製功能:

[email protected]:[/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 
[email protected]
identified by '123456'; Query OK, 0 rows affected (0.00 sec)

3.配置3307埠主機 開啟relay_log

開啟3307埠主機配置檔案 ,配置3307主機開啟relay_log

[email protected]:[/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埠資料庫。

4.檢視3308埠資料庫master資訊 並配置3307埠資料庫開啟監聽

檢視3308 master 資訊

[email protected]:[/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 埠資料 開始監聽

[email protected]:[/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埠庫插入一條資料

[email protected]:[/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資料檢視是否複製成功

[email protected]:[/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資料庫同樣複製完成,至此雙主備份 配置完成。