假設兩台機器 ip 分別為機器一: 192.168.14.37 機器二: 192.168.14.38 ,伺服器為linux R#hel 5.9
##在兩台伺服器中執行建立使用者語句:mysql:>create user 'repl'@'%' identified by '135246'; -- 创建用户 repl 密码 135246伺服器一執行:
mysql:>grant replication client,replication slave on *.* to 'repl'@'192.168.14.38' identified by '135246'; -- 授权服务器一可以远程访问服务器二伺服器二執行:
mysql:>grant replication client,replication slave on *.* to 'repl'@'192.168.14.37' identified by '135246'; -- 授权服务器二可以远程访问服务器一驗證(根據提示輸入密碼) :
在伺服器一上連接伺服器二
mysql -h 192.168.14.38 -u repl -p在伺服器二上連接伺服器一
mysql -h 192.168.14.37 -u repl -p查看mysql :首先:
vi /etc/my.cnf
在伺服器1, 新增以下內容:
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] server_id = 1 log-bin character-set-server=utf8 #表名不区分大小写 lower_case_table_names=1 #server_id = 1 # uniquely identify 从为2 show master status可以得到伺服器一和伺服器二的 MASTER_LOG_FILE 和 MASTER_LOG_POS 訊息,
假设服务器一为 " localhost-bin.000004" 和 "120" 服务器二为 " localhost-bin.000005" 和 "667"在伺服器一上執行:
stop slave; CHANGE MASTER TO MASTER_HOST = '192.168.14.38', MASTER_USER = 'repl', MASTER_PASSWORD = '135246', MASTER_LOG_FILE = 'localhost-bin.000004', MASTER_LOG_POS = 120; start slave;在伺服器二上執行:
stop slave; CHANGE MASTER TO MASTER_HOST = '192.168.14.37', MASTER_USER = 'repl', MASTER_PASSWORD = '135246', MASTER_LOG_FILE = 'localhost-bin.000005', MASTER_LOG_POS = 667; start slave;最後驗證主主同步是否成功:在伺服器一mysql 新增表example
mysql:> create database example1 ; use example1; create table example1 (length int);最後在伺服器二查看是否有此資料庫,此表,和此條資料.查看同步狀態:
show slave status \G
mysql影片教學》