bitsCN.com
《高可用MySQL》P10
MySQL的复制在以下环境中经常使用:
1 通过热备份来避免灾难
2 产生报表
使用从服务器来产生报表,可大大降低主服务器的负载。
3 调试和审计
例如,查看某些查询是否有性能问题,以及服务器是否由于某个槽糕的查询而不同步。
1 配置一个服务器作为 Master。
2 配置一个服务器作为 Slave。
3 将 Slave 连接到 Master。
除非你从一开始就计划复制且 my.conf 文件中配置正确,否则步骤1和步骤2要求必须重启每个服务器。
将配置选项添加到my.conf以配置Master
[mysqld]user = mysqlpid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockport = 3306basedir = /usrdatadir = /var/lib/mysqltmpdir = /tmp<strong>log</strong><strong>-bin = master-binlog-bin-index = master-bin.indexserver-id = 1</strong>
在Master上创建一个复制用户
master> CREATE USER repl_user;Query OK, 0 rows affected (0.00 sec)master> GRANT REPLICATION SLAVE on *.* to repl_user IDENTIFIED by 'xyzzy';Query OK, 0 rows affected (0.00 sec)
添加选项到my.conf文件来配置Slave
[mysqld]user = mysqlpid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockport = 3306basedir = /usrdatadir = /var/lib/mysqltmpdir = /tmp<strong>server</strong><strong>-id = 2relay-log-index = slave-relay-bin.indexrelay-log = slave-relay-bin</strong>
slave> CHANGE MASTER TO -> MASTER_HOST ="master-1", -> MASTER_PORT = 3306, -> MCSTER_USER = 'repl_user', -> MAST_PASSWORD = 'xyzzy'; Query OK, 0 rows affected(0.00 sec) slave> START SLAVE;
bitsCN.com