一、建立用戶並設置遠程訪問受權mysql
1). A上添加: //ip地址爲B的ip地址,用於B訪問
# grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.2.220' identified by '123456' with grant option;
2). B上添加://ip地址爲A的ip地址,用於A訪問
# grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.2.67' identified by '123456' with grant option;
3). 執行命令更新數據庫使用戶生效。
# flush privileges;
二、確保mysql對外部ip開放sql
1). 確認一下3306是否對外開放,MySQL默認狀態下是不開放對外訪問功能的。查看的辦法以下: ~# netstat -an | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 從上面能夠看出,mysql的3306端口只是監聽本地的鏈接,這樣就阻礙了外部IP對該數據庫的訪問,修改的辦法其實很簡單,進入到mysql的配置文件所在目錄(/etc/mysql/my.cnf(默認),有的是在/etc/my.cnf)下,找到文件中的以下內容: # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 將bind-address註釋掉,或者改爲你想要使用的客戶端主機IP。 這樣mysql的遠程訪問端口就算開啓了
~# netstat -an | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
2). 關閉防火牆
# service iptables stop
3. 建立同步測試表數據庫
create database testsync; use testsync; create table t_user ( id int primary key auto_increment, username varchar(20), age int );
4. 配置A B的mysql(查看my.cnf位置 mysql --help | grep my.cnf)app
# vi /etc/my.cnf
在[mysqld]下添加以下字段:less
#兩臺機器的server-id不能相同 server-id = 1 log-bin=mysql-bin binlog_format=mixed replicate-do-db=testsync replicate-ignore-db=mysql,information_schema,performance_schema
5. 重啓並進入mysqltcp
0). 查看主機狀態
# show master status;
+-------------------+----------+--------------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------------+------------------+-------------------+ | mysqld-bin.000005 | 68113 | happy,ipaddressmgr | | | +-------------------+----------+--------------------+------------------+-------------------+ 1 row in set (0.00 sec)
1). 設置主機信息
# change master to master_host='192.168.2.220',master_user='sync_user',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=68113;
2). 啓動slave
# start slave;
3). 查看是否啓動成功
# show processlist;
+----+-------------+-------------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-------------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+ | 1 | system user | | NULL | Connect | 1264 | Waiting for master to send event | NULL | | 2 | system user | | NULL | Connect | 1262 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | | 38 | sync_user | pc.local:36102 | NULL | Binlog Dump | 1258 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | | 40 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+-------------+-------------------------+------+-------------+------+-----------------------------------------------------------------------------+------------------+
4). 查看slave上的master狀態
#show master status;
5). 查看slave狀態
# show slave status\G;
6. 以上便實現主從備份,若是想要雙主,只須要在另外一臺mysql上,執行步驟4, 5便可。ide
錯誤處理:測試
101027 16:50:58 [Note] Slave I/O thread: connected to master 'xxx@xxxxxx:3306',replication started in log 'xxx-bin.000001' at position 1440046
101027 16:50:58 [ERROR] Error reading packet from server: Could not find first log file name in binary log index file ( server_errno=1236)
101027 16:50:58 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file', Error_code: 1236
101027 16:50:58 [Note] Slave I/O thread exiting, read up to log 'xxx-bin.000001', position 1440046
Slave_IO_Running: No
Slave_SQL_Running: Yes
緣由:master_log_file或者master_log_pos配置錯誤 或者 權限錯誤
按照正常流程解決,應該是: stop slave; CHANGE MASTER TO MASTER_HOST='xxx',MASTER_USER='xx',MASTER_PASSWORD='xx',MASTER_PORT=3306,MASTER_LOG_FILE='xxx-bin.000001',MASTER_LOG_POS=1440046; start slave; 問題依然存在, 後來通過如下步驟解決,不過有點困惑: 1、重啓主庫
二、stop slave;
3、給從庫從新受權
4、執行如下命令 reset slave; start slave;