標籤:mysql 主從複製
mysql複製1.搭建一個master對應1個slave(只複製某個資料庫)(1)規劃網路和主從伺服器master:10.10.54.86slave:10.10.54.85(2)主機設定log-bin=master-binbinlog_format=mixedserver-id=1[[email protected] tmp]# /etc/init.d/mysqld restart ERROR! MySQL server PID file could not be found!Starting MySQL.. SUCCESS! (3)從機設定log-bin=slave-binbinlog_format=mixedserver-id=10[[email protected] ~]# /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資料庫還原到從庫上[[email protected] tmp]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --flush-logs --database employees>employees.sql[[email protected] 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)(8)在slave上啟動slavemysql> 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)從上:顯示有資料庫bmysql> show databases;+--------------------+| Database |+--------------------+| information_schema || a || b || employees || mysql || performance_schema || test |+--------------------+測試在master上建立刪除添加資料,在slave上都能同步。
本文出自 “筆記” 部落格,請務必保留此出處http://sunflower2.blog.51cto.com/8837503/1651704
Mysql---主從複製