mysql replication

浏览一下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的压力。

本文出自

http://space.itpub.net/8117479/viewspace-705529

你可能感兴趣的:(mysql,mysql,数据库,职场,re,休闲)