Ubuntu18.04_mysql5.7 主從備份配置

 

 

mysql5.7
服務器信息如下(實驗環境,關閉機器的iptables防火牆和selinux):

node1  10.1.6.251 物理數據庫Master數據庫
node2 10.1.6.252

物理數據庫Slave數據庫

os ubuntu18.04  

 

在Master服務器上,增加一個用戶賬號(mysqlsync)作爲同步用戶賬號.

mysql> GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

刷新授權,不然授權在MySQL重啓前不生效,執行這條指令後,即刻生效

flush privileges;

 

修改mysqld.cnf

libo@node1:/etc/mysql/mysql.conf.d$ sudo vim  /etc/mysql/mysql.conf.d/mysqld.cnf;

更改如下內容:

server-id               = 251
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = asd     #asd是需要同步的數據庫名稱

重啓mysql服務:

libo@node1:/etc/mysql/mysql.conf.d$ systemctl restart mysql;
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: libo
Password: 
==== AUTHENTICATION COMPLETE ===
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
#把數據表先鎖住,不讓Position 變化!

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 | asd          |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

slave端:

libo@node2:/etc/mysql/mysql.conf.d$ sudo vim  /etc/mysql/mysql.conf.d/mysqld.cnf;

 

server-id               = 252
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = asd

 

 

slave端重啓mysql

systemctl restart mysql

 在mysql控制檯下輸入匹配主從模式的命令

(此處使用的是root用戶, 將當前數據庫作爲從庫匹配到主數據庫10.1.6.251的mysql服務上)

mysql> change master to \
    -> master_host="10.1.6.251", \
    -> master_user="root", \
    -> master_password="123456", \
    -> master_log_file="mysql-bin.000001", \
    -> master_log_pos=154;
ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
mysql> show variables like '%repository%';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| master_info_repository    | FILE  |
| relay_log_info_repository | FILE  |
+---------------------------+-------+
2 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table slave_master_info;
Query OK, 0 rows affected (0.04 sec)

mysql> drop table slave_relay_log_info;
Query OK, 0 rows affected (0.08 sec)

mysql> drop table slave_worker_info;
Query OK, 0 rows affected (0.03 sec)

mysql> drop table innodb_index_stats;
Query OK, 0 rows affected (0.06 sec)

mysql> drop table innodb_table_stats;
Query OK, 0 rows affected (0.05 sec)

slave端:

 重新啓動mysql服務

root@node2:~# systemctl restart mysql

重新配置在mysql控制檯下輸入匹配主從模式的命令

mysql> change master to \
    -> master_host="10.1.6.251", \
    -> master_user="root", \
    -> master_password="123456", \
    -> master_log_file="mysql-bin.000001", \
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.27 sec)
mysql> show slave status\G

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章