mysql 主從複製

主從服務
主服務器爲192.168.1.1 從服務器爲192.168.1.2
備份主服務器test數據庫
/user/local/mysql/bin/mysqldump -uroot -p test -l -F >/root/test.sql
把備份文件拷貝到從服務器
scp /root/test.sql [email protected]:/root/test.sql
然後再從服務器上恢復數據庫
/usr/local/mysql/bin/mysql -uroot -p test < /root/test.sql


主服務器

修改/etc/my.cnf
server-id必須是唯一值
開啓log-bin

創建授權用戶
grant all slave on *.* to [email protected] identified by "pass"
grant replication slave on *.* [email protected] identified by "pass"
select user,host,password from mysql.user  查看數據庫裏的用戶名

修改從服務器/etc/my.cnf
server-id 不能和主相同
master-host = 192.168.1.1
master-user = user1
master-password = abc123,
master-port = 3306


pkill 3306 關閉3306端口
再次啓動mysql服務

show status\G 列和行倒轉  查看 slave_SQL_Running 和 Slave_IO_Running是否爲yes
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.1.1
                Master_User: user1
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000002
        Read_Master_Log_Pos: 371
             Relay_Log_File: localhost-relay-bin.000006
              Relay_Log_Pos: 508
      Relay_Master_Log_File: mysql-bin.000002
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes



在主服務器上查看
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      371 |              |                  | 
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

主從服務器的數據庫test表test的數據相同
mysql> select * from test;
+------+
| id   |
+------+
| 1    | 
| 2    | 
| 3    | 
| 4    | 
| 5    | 
| 6    | 
| 7    | 
| 8    | 
+------+

再在主服務器上更新數據


mysql> insert into test values('9');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values('10');
Query OK, 1 row affected (0.00 sec)


直接在從服務器上可以同步了

mysql> select * from test;
+------+
| id   |
+------+
| 1    | 
| 2    | 
| 3    | 
| 4    | 
| 5    | 
| 6    | 
| 7    | 
| 8    | 
| 9    | 
| 10   | 
+------+
10 rows in set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章