MySQL 主從服務器的配置步驟

jopen 12年前發布 | 11K 次閱讀 MySQL 數據庫服務器

         環境:

         Master:
               ip 192.168.1.110
               os rhel-5.8
               mysql 5.5.28
         Slave:
               ip 192.168.1.113
               os rhel-5.8
               mysql 5.5.28


        
          
             
             ① 主my.cnf的配置

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin     #打開mysql二進制日志
binlog-do-db=test     #設置二進制日志記錄的庫
server-id       = 1   #設置mysql_id,主從不能相同
binlog-ignore-db=mysql  #設置二進制日志不記錄的庫
sync_binlog=1         #設置binlog有更新的時候刷新到磁盤


 

             ② 登入主庫

use mysql;
grant replication slave,file on *.* to 'Think'@'192.168.1.113' identified by 'mysql';
flush privileges;


                查看主庫狀態,并記錄File和Position

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000002
        Position: 107
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)


 

             ③ 從my.cnf的配置

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id       = 2
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
slave-net-timeout=10


 

             ④ 從庫change master

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='192.168.1.110',master_user='Think',master_password='mysql',
    -> master_log_file='mysql-bin.000002',master_log_pos=107;
Query OK, 0 rows affected (0.10 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.110
                  Master_User: Think
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 107
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 413
              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
1 row in set (0.00 sec)


 

             ⑤ 驗證同步
                主庫:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| bin            |
| servers        |
| water          |
+----------------+
3 rows in set (0.00 sec)

mysql> insert into bin values(1);
Query OK, 1 row affected (0.12 sec)


                從庫:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| bin            |
| sales          |
| t              |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from bin;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)


來自:http://blog.csdn.net/linwaterbin/article/details/8270921

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!