MySQL Replication Configuration

byfree發表於2010-07-19
在一臺linux伺服器上實現MySQL Replication
配置說明:
master:
version  5.1.47
/etc/my47.cnf
server-id = 1
port = 3307
socket = /tmp/mysql47.sock
datadir = /mysql47/data
binlog-do-db = test(複製庫名)
slave:
version  5.1.48
/etc/my48.cnf
server-id = 2
port = 3306
socket = /tmp/mysql48.sock
datadir = /usr/local/mysql/var
replicate-do-db = test(與master的binlog-do-db二選一即可)
1.在master上建立複製使用者
$ mysqld_safe --defaults-file=/etc/my47.cnf &
$ mysql -S /tmp/mysql47.sock -p
mysql> create user rep@localhost identified by 'rep';
mysql> GRANT replication slave ON *.* to 'rep'@'localhost';
2.分別配置master和slave的my.cnf引數
master:
# vi /etc/my47.cnf
[mysqld]
server-id = 1
socket = /tmp/mysql47.sock
port = 3307
datadir = /mysql47/data
log-bin=mysql-bin
#binlog-do-db = test
slave:
# vi /etc/my48.cnf
[mysqld]
server-id = 2
port = 3306
socket = /tmp/mysql48.sock
#log-bin=mysql-bin
#log-slave-updates
#read-only=1
replicate-do-db=test
3.slave上配置master資訊
execute the following statement on the slave:
$ mysqld_safe --defaults-file=/etc/my48.cnf &
$ mysql -S /tmp/mysql48.sock -p
mysql> CHANGE MASTER TO
    -> MASTER_HOST='localhost',
    -> MASTER_PORT=3307,
    -> MASTER_USER='rep',(前面建立具有複製許可權的那個使用者)
    -> MASTER_PASSWORD='rep',
    -> MASTER_LOG_FILE='mysql-bin.000014',
    -> MASTER_LOG_POS=106;
   
其中MASTER_LOG_FILE='mysql-bin.000014',MASTER_LOG_POS=106引數出自master
master執行:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000014 |      106 |              |                  |
+------------------+----------+--------------+------------------+
檢視slave狀態:
mysql> slave start;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: localhost
                  Master_User: rep
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000017
          Read_Master_Log_Pos: 193
               Relay_Log_File: oel5-relay-bin.000013
                Relay_Log_Pos: 338
        Relay_Master_Log_File: mysql-bin.000017
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 193
              Relay_Log_Space: 637
              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:
              
Slave_IO_Running、Slave_SQL_Running狀態為YES
mysql> show processlist\G;
檢視複製程式
4.Replication驗證
master:
mysql> use test;
mysql> create table t (i int);
mysql> insert into t values(10);
mysql> insert into t values(20);
mysql> select * from t;
+------+
| i    |
+------+
|   10 |
|   20 |
+------+
slave:
mysql> use test;
mysql> select * from t;
+------+
| i    |
+------+
|   10 |
|   20 |
+------+

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17997/viewspace-668384/,如需轉載,請註明出處,否則將追究法律責任。

相關文章