mysql 同步 master-slave

linux OS: ubuntu server 8.04.1
software: mysql5.0.51
mysql1  : 192.168.6.4    //master
mysql2  : 192.168.6.5    //slave

安裝步驟 
1. 分別在兩臺機器上安裝mysql-server
shell > apt-get install mysql-server
2.修改 mysql1 master 的配置文件
vim /etc/mysql/my.cnf
找到 bind-address = 127.0.0.1
改成 bind-address = 0.0.0.0
找到 
#server-id               = 1
#log_bin                 = /var/log/mysql/mysql-bin.log
去掉 註釋符號
server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
default-character-set   = utf8     #新加上的爲了保持編碼一至防止出錯

3.改好後保存退出,而後創建一個slave服務器的用戶帳號
root@msyql1:/# mysql -uroot -p

mysql>  grant replication slave,replication client on *.* to ludy@'192.168.6.5' identified by 'ypmwbg';
mysql >  grant replication slave on *.* to ludy@192.168.6.5 identified by 'ypmwbg';     //給予權限
到這裏要注意了,個人兩臺數據庫都是空的.

重啓mysql服務

4.修改 mysql2 服務器slave的 my.cnf配置文件
找到 bind-address            = 127.0.0.1
替換 bind-address            = 0.0.0.0
找到 
#server-id               = 1
#log_bin                 = /var/log/mysql/mysql-bin.log
把 註釋符號去掉 改成以下

server-id               = 2
master-host             = 192.168.6.4
master-user             = ludy
master-password         = ypmwbg
master-port             = 3306
log_bin                 = /var/log/mysql/mysql-bin.log
log-slave-updates
skip-slave-start
配置完後 從新啓動mysql

而後進入 mysql1 master 服務器
root@msyql:~# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      98  |              |                  | 
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

記錄下來之後 進入 mysql2 slave mysql

root@msyql2:~# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>  change master to master_log_file='mysql-bin.000001', master_log_pos=98;

【參考: mysql

change master to master_host='192.168.0.100', master_user='slave', master_password='******', master_log_file='mysql-bin.000010', master_log_pos=16860;
slave start;
執行:show  slave status\G;時看到的以下狀態;
mysql> show slave status\G; linux

sql

//這個地方就是記錄下來的 mysql1 master 的數據

mysql > start slave;  //啓動slave 服務

mysql > show slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.6.4
                Master_User: ludy
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 181
             Relay_Log_File: mysqld-relay-bin.000003
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB: 
        Replicate_Ignore_DB: 
         Replicate_Do_Table: 
     Replicate_Ignore_Table: 
    Replicate_Wild_Do_Table: 
Replicate_Wild_Ignore_Table: 
                 Last_Errno: 0
                 Last_Error: 
               Skip_Counter: 0
        Exec_Master_Log_Pos: 181
            Relay_Log_Space: 235
            Until_Condition: None
             Until_Log_File: 
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File: 
         Master_SSL_CA_Path: 
            Master_SSL_Cert: 
          Master_SSL_Cipher: 
             Master_SSL_Key: 
      Seconds_Behind_Master: 0
1 row in set (0.01 sec)

哈哈      Slave_IO_Running: Yes
          Slave_SQL_Running: Yes

說明啓動成功
而後在 master 新建 一個數據庫看看

root@msyq1l:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database chenggong;
Query OK, 1 row affected (0.00 sec)

在 salve 查看看是否同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| chenggong          |   //同步了哈哈 ~
| mysql              | 
| test               | 
+--------------------+
4 rows in set (0.01 sec)


好了就寫這麼多,若是你做 master-slave 的時候 你的master 數據裏有數據那麼
你必須 在  我寫的 第三步與第四步中加入一下步驟:

接 上文 第三步
進入master數據庫的Mysql控制檯執行 
mysql >FLUSH TABLES WITH READ LOCK;   //鎖表
而後重新打開一個 終端 拷貝 master 的全部的數據到 slave 服務器覆蓋
讀取  master 二進制文件與偏移量
mysql > show master status;
一樣 要記錄下 file 與 position 的值
而後解鎖

mysql > unlock tables; shell


本文出自 「linuxer」 博客,請務必保留此出處http://deidara.blog.51cto.com/400447/122039
數據庫


補充說明:可能新版本或更高的版本中沒直接支持master-slave.因此簡單配置便可: ubuntu

master機器中: vim

#cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
#vi /etc/my.cnf

在[msysqld]中加入 服務器

replicate-ignore-db = mysql
replicate-do-db = db //要備份的數據庫
replicate-ignore-db = test
replicate-ignore-db = information_schema
binlog-do-db = db //要備份的數據庫
#mysql -u root -p
mysql> flush tables with read lock;
【完成數據同步後再回到master機器中解鎖:mysql> unlock tables;】
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |      107 | db           |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
受權:
grant replication slave,replication client on *.* to slave@'192.168.0.6' identified by '*****';

回到slave機器中: ide

#cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
#vi /etc/my.cnf

在[msysqld]中加入 編碼


replicate-ignore-db = mysql
replicate-do-db = db //要備份的數據庫
replicate-ignore-db = test
replicate-ignore-db = information_schema
binlog-do-db = db //要備份的數據庫
bind-address = 0.0.0.0
log-slave-updates
read-only=1
註釋掉server-id = 1打開 server-id = 2。


重啓Mysql後進入Mysql:

#mysql -u root -p
change master to master_host='192.168.0.17', master_user='slave', master_password='******', master_log_file='mysql-bin.000007', master_log_pos=107;
【注意這裏的值應該和master機器的值相同】
mysql> start slave;
mysql> show slave status\G;

若是顯示:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.17
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 107
               Relay_Log_File: localhost-relay-bin.000012
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: PlatformWeb
          Replicate_Ignore_DB: mysql,test,information_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 559
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1

 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
說明配置成功,若是出現異常,大多數狀況都是沒法鏈接master機器形成的,檢查訪問權限,有必要的時候重啓兩臺機器,通常先起動master。

接下來作數據同步:

從master機器上獲取數據。

# /usr/bin/mysqldump  -h 192.168.0.17 -u root -p db > db.sql
mysql -u root -p db < db.sql
數據同步完成後回到master機器中解鎖。
相關文章
相關標籤/搜索