####mysql複製mysql
1.搭建一個master對應1個slave(只複製某個數據庫)sql
(1)規劃網絡和主從服務器數據庫
master:10.10.54.86服務器
slave:10.10.54.85網絡
(2)主機設置ide
log-bin=master-bin測試
binlog_format=mixedrest
server-id=1日誌
[root@nan86 tmp]# /etc/init.d/mysqld restartorm
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!
(3)從機設置
log-bin=slave-bin
binlog_format=mixed
server-id=10
[root@nan85 ~]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!
(4)在master上面建立一個複製用戶並授予權限
mysql> grant replication slave on *.* to 'emp'@'10.10.54.85' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(5)查看master上的二進制日誌和position位置
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000006 | 330 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(6)備份master上的數據,把備份master數據庫還原到從庫上
[root@nan86 tmp]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --flush-logs --database employees>employees.sql
[root@nan86 tmp]# mysql -uroot -p123456 -h10.10.54.85<employees.sql
備註:-p 後接的密碼爲從機受權的密碼
(7)在slave上面change master操做
mysql> change master to master_host='10.10.54.86',master_user='emp', master_password='123', master_log_file='master-bin.000006',master_log_pos=330;
Query OK, 0 rows affected (0.01 sec)
------
問題備註:
CHANGE MASTER TO MASTER_HOST='10.10.54.85',MASTER_PORT=3306,MASTER_USER='slave2',MASTER_PASSWORD='slave2',MASTER_LOG_FILE='master-bin.000009',MASTER_LOG_POS=120;
----
(8)在slave上啓動slave
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
(9)查看slave的狀態
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.54.86
Master_User: emp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000007
Read_Master_Log_Pos: 107
Relay_Log_File: nan85-relay-bin.000004
Relay_Log_Pos: 254
Relay_Master_Log_File: master-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
(10)測試
主上:
mysql> create database b;
Query OK, 1 row affected (0.02 sec)
從上:顯示有數據庫b
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| a |
| b |
| employees |
| mysql |
| performance_schema |
| test |
+--------------------+
測試在master上建立刪除添加數據,在slave上都能同步。