redhat 5.5 配置 mysql AB複製

wangxiangtao發表於2011-08-22

瀏覽一下5.5官檔上關於replication 的章節:

http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html

總結了一下關於AB複製的步驟:

(1)設定主伺服器的my.cnf  開啟二進位制日誌記錄 設定serverid

[mysqld]

log-bin=mysql-bin

server-id=1

innodb_flush_log_at_trx_commit=1

sync_binlog=1

 

(2) 設定從伺服器的serverid

[mysqld]

server-id=2

 

(3)設定複製帳戶, 並授權

create  user 'gabriel'@'%.richinfo.cn' identified by 'gabriel0814';

grant replication slave on *.* to 'gabriel'@'%.richinfo.cn ';

 

(4) 鎖定資料庫狀態,讀取主伺服器二進位制日誌的狀態

flush tables with read lock;

show master status

 

(5)使用mysqldump 建立資料庫快照, 釋放鎖

mysqldump --all-database --lock-all-tables > dbdump.db

unlock tables;

 

(6)在從伺服器上準備複製:

stop slave;

mysql < fulldb.dump

change master to

master_host='applinux002.richinfo.cn',

master_user='gabriel',

master_password='gabriel0814',

master_log_file='',

master_log_pos= ;

 

start slave;

以上步驟在自己的虛擬機器上測試是透過的,在複製搭建完畢後,由於許可權的問題,去掉萬用字元% 改為實際的主機名, AB複製成功。

在生產線上具體實施步驟如下:

(1)     更改master資料庫 my.cnf引數:

[root@applinux002 data]# cat /etc/mysql/my.cnf | grep -v ^# | grep -v ^$

[client]

port            = 3306

socket          = /etc/mysql/mysqld.sock

[mysqld]

datadir  = /md3200i/data1/mysql/data

port            = 8808

socket          = /etc/mysql/mysqld.sock

skip-external-locking

key_buffer_size = 384M

max_allowed_packet = 1M

table_open_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 8M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size = 32M

thread_concurrency = 24

log-bin=mysql-bin

server-id       = 1

innodb_data_home_dir = /md3200i/data1/mysql/data

innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend

innodb_log_group_home_dir = /md3200i/data1/mysql/data

innodb_buffer_pool_size = 2048M

innodb_additional_mem_pool_size = 20M

innodb_log_file_size = 512M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

(2)     更改slave資料庫my.cnf 引數:

 

[root@applinux003 mysql]#  cat /etc/mysql/my.cnf | grep -v ^# | grep -v ^$

[client]

port            = 3306

socket          = /etc/mysql/mysqld.sock

[mysqld]

port            = 8808

socket          = /etc/mysql/mysqld.sock

skip-external-locking

key_buffer_size = 384M

max_allowed_packet = 1M

table_open_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 8M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size = 32M

thread_concurrency = 8

server-id       = 2

innodb_data_home_dir = /md3200i/data1/mysql/data

innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend

innodb_log_group_home_dir = /md3200i/data1/mysql/data

innodb_buffer_pool_size = 2048M

innodb_additional_mem_pool_size = 20M

innodb_log_file_size = 512M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

(3)     建立複製使用者, 並授權 由於在測試環境中運用萬用字元%發生了錯誤,在生產環境

中直接對真實主機名進行授權。

 

mysql> create  user 'replica'@'applinux003.richinfo.cn' identified by 'replica';

Query OK, 0 rows affected (0.00 sec)

 

mysql> grant replication slave on *.* to 'replica'@'applinux003.richinfo.cn';

Query OK, 0 rows affected (0.00 sec)

 

mysql>  grant super on *.* to 'replica'@'applinux00linux003.richinfo.cn';

Query OK, 0 rows affected (0.00 sec)

 

mysql> create  user 'replica'@'applinux002.richinfo.cn' identified by 'replica';

Query OK, 0 rows affected (0.00 sec)

 

mysql> grant replication slave on *.* to 'replica'@'applinux002.richinfo.cn';

Query OK, 0 rows affected (0.00 sec)

 

mysql>  grant super on *.* to 'replica'@'applinux00linux002.richinfo.cn';

Query OK, 0 rows affected (0.00 sec)

(4) 鎖定資料庫狀態,讀取主伺服器二進位制日誌的狀態

 

mysql> flush  tables with read lock;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000025 |     1213 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

(5)使用mysqldump 建立資料庫快照, 釋放鎖

mysql> quit

Bye

[root@applinux002 data]# mysqldump -u root -p --all-database --lock-all-tables > dbdump.db

Enter password:

 

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

(6)在從伺服器上恢復資料:

[root@applinux003 data]# service  mysql  status

MySQL running (25059)                                      [  OK  ]

[root@applinux003 data]# pwd

/md3200i/data1/mysql/data

 [root@applinux003 data]# /usr/local/mysql/bin/mysqladmin -u root -p123456 password 'T4GRUkwMtqIpVzgXA3o6'

[root@applinux003 data]# mysql  -u root –p < dbdump.db

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

………….

(7) replica 使用者做互連測試:

Slave 庫上

[root@applinux003 data]# mysql  -h  applinux002.richinfo.cn -ureplica -preplica -P8808

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 134

Server version: 5.5.15-log Source distribution

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

(8) slave 配置

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

 

mysql> change master to

    -> master_host='applinux002.richinfo.cn',

    -> master_user='replica',

    -> master_password='replica',

    -> master_port=8808,

    -> master_log_file='mysql-bin.000025',  

    -> master_log_pos=589 ;

Query OK, 0 rows affected (0.02 sec)

--在配置過程中此處多了一個空格 導致無法找到 log-bin 索引檔案, 因此一定要仔細,認真…….

mysql> start slave;

mysql> use itao;

Database changed

mysql> select *  from  test;

+------+

| id   |

+------+

|    1 |

|    2 |

|  810 |

+------+

3 rows in set (0.00 sec)

 

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: applinux002.richinfo.cn

                  Master_User: replica

                  Master_Port: 8808

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000025

          Read_Master_Log_Pos: 1403

               Relay_Log_File: applinux003-relay-bin.000002

                Relay_Log_Pos: 443

        Relay_Master_Log_File: mysql-bin.000025

             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: 1403

              Relay_Log_Space: 605

              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)

 

ERROR:

No query specified

---整個複製過程搭建完畢,

(9) 進行mysql  AB 複製測試

Master

mysql> use itao;

Database changed

mysql> insert into test(id) values (100);

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into test(id) values (200);

Query OK, 1 row affected (0.00 sec)

 

mysql>

 

 

Slave

mysql> use itao;

Database changed

mysql> select *  from  test;

+------+

| id   |

+------+

|    1 |

|    2 |

|  810 |

|  100 |

+------+

4 rows in set (0.00 sec)

 

mysql> select *  from  test;

+------+

| id   |

+------+

|    1 |

|    2 |

|  810 |

|  100 |

|  200 |

+------+

5 rows in set (0.00 sec)

保持同步,真正實現了讀寫分離, master 庫上 可以進行 讀寫操作,slave 庫上用於 讀操作,可以用於報表,備份,減少了master的壓力。

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

相關文章