系統:Ubuntu14.04(阿里云/騰訊云)
數據庫:MySQL5.6
主:阿里云ubuntu
從:騰訊云ubuntu
默認需要同步的數據庫已經事先創建。
我們設置一個主庫(Master),和一個從庫(Slave或Secondary)。從庫從主庫復制數據內容,目的為災難備份、讀寫分離等。
主庫開啟binary log,開啟后每一次操作更新、修改、刪除等都會記錄在案,所以從庫的同步過程其實就是獲得這些過程,然后將現場還原,就達到了數據同步的目的。
PS:使用mysql的root用戶和遠程是很危險的,而且最后WITH GRANT OPTION還能授權,這里只是示范。最安全肯定是ssh登錄再去訪問數據庫,又或者新建一個mysql用戶專門用于同步。
上面的語句完成后,就可以用:
用戶名為:root
密碼為:123456(這個設置的密碼不是原來ssh登錄進去的密碼,當然你也可以設置成一樣的吧)
shell> mysql -u root -h xx.xx.xx.xx -pshell> 123456查看用戶是否可以通過網絡訪問
root@iZwz96uh8912ewgq9yv5nxZ:~# mysql -u root -pEnter passWord:Welcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 101Server version: 5.6.33-0ubuntu0.14.04.1-log (Ubuntu)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select user , host from user;+------------------+-------------------------+| user | host |+------------------+-------------------------+| root | % || root | 127.0.0.1 || root | ::1 || root | izwz96uh8912ewgq9yv5nxz || debian-sys-maint | localhost || root | localhost |+------------------+-------------------------+6 rows in set (0.01 sec)mysql> mysql -u root -puse mysqlselect user , host from user;看到有個%號說明那個用戶可以任意host遠程直接登錄mysql了。
修改配置文件:
server-id = 1(服務id,這個需要每個不同)log_bin = /var/log/mysql/mysql-bin.log(這是主服務器,必須開啟log_bin,這句就是開啟了)binlog_format =mixed(混合比較好)binlog_do_db = master(需要同步的數據庫,如果多個數據庫,重復這個配置)binlog_ignore_db = mysql(排除不需要同步的數據庫,如果多個數據庫,重復這個配置)都是有默認的,去除前面的#號就行了,binlog_format=mixed這句是沒有的,自己加。
重啟mysql,讓配置生效:
sudo service mysql restart得到當前binlog信息:
mysql> show master status;+------------------+----------+--------------+------------------+--------------- ----+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_ Set |+------------------+----------+--------------+------------------+--------------- ----+| mysql-bin.000003 | 232 | master | mysql | |+------------------+----------+--------------+------------------+--------------- ----+1 row in set (0.03 sec)我們關注File(mysql-bin.000003)和Position(232),這兩個狀態是我們需要同步需要用到的,此時不要動數據庫了,不管是增刪改查都會改變Posituion。
你這里看到Binlog_Do_DB是叫做master,是因為我數據庫名就叫master,其他名字也可以的,看你具體創建。
首先檢查能不能遠程主數據庫:
shell> mysql -u root -h xx.xx.xx.xx -pshell> (輸入密碼123456,因為我們主服務其授權的時候的密碼是123456)編輯mysql配置文件:
cd /etc/mysqlcp my.cnf mycnf.baknano my.cnf修改配置文件:
server-id = 2(服務id,這個需要每個不同)log_bin = /var/log/mysql/mysql-bin.log(這是主服務器,必須開啟log_bin,這句就是開啟了)binlog_format =mixed(混合比較好)binlog_do_db = master(需要同步的數據庫,如果多個數據庫,重復這個配置)binlog_ignore_db = mysql(排除不需要同步的數據庫,如果多個數據庫,重復這個配置)重啟mysql服務:
sudo service mysql restart讓主服務器同步數據到從服務器
mysql> stop slave;mysql> change master to master_host='xx.xx.xx.xx', master_user='root',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=232;(保證是最新的position)mysql> start slave;查看從數據庫信息:
show slave status;此時你看到
Slave_IO_Running | Slave_SQL_Running| Yes | Yes就說明配置成功了。
這時你總算可以去操作主數據庫了。
這時你創建一個表插入數據你可以看到從數據庫也改變了。
參考文章:配置MySQL主從復制
| 
 
 | 
新聞熱點
疑難解答