CentOS服务器平台搭建mysql主从复制与读写分离的方法
本文实例讲述了centos服务器搭建mysql主从复制与读写分离的方法。分享给大家供大家参考,具体如下:
mysql 主从复制的优点:
① 如果主服务器出现问题, 可以快速切换到从服务器提供的服务,保证高可用性
② 可以在从服务器上执行查询操作, 降低主服务器的访问压力
③ 可以在从服务器上执行备份, 以避免备份期间影响主服务器的服务
注意事项:
① server-id必须唯一,一般使用ip的后三位
② 从库slave_io_running:no 可能原因:帐号无权限操作
③ can't execute the query because you have a conflicting read lock,解锁下即可 unlock tables;
④ 一般只有更新不频繁的数据或者对实时性要求不高的数据可以通过从服务器查询, 实时性要求高的数据仍然需要从主数据库获得
⑤ 修改完主从服务器的配置需要重启mysql:service mysqld restart
主机a: 192.168.10.111
从机b: 192.168.10.124
请先分别安装mysql,版本需一致,装了即可跳过
1、先登录主机 a,在主服务器上,设置一个从数据库的账户,使用replication slave(从复制)赋予权限,如:
赋予从机权限,有多台从机,就执行多次。
mysql>flush privileges;
2、 打开主机a的my.cnf,输入如下:(修改主数据库的配置文件my.cnf,开启binlog,并设置server-id的值,修改之后必须重启mysql服务)
server-id = 1 #主机标示,整数 log_bin = /var/log/mysql/mysql-bin.log #确保此文件可写,开启bin-log read-only =0 #主机,读写都可以 binlog-do-db =test #需要备份数据,多个写多行 binlog-ignore-db =mysql #不需要备份的数据库,多个写多行
可以通过mysql>show variables like 'log_%';
验证二进制日志是否已经启动。
3、现在可以停止主数据的的更新操作,并生成主数据库的备份,我们可以通过mysqldump到处数据到从数据库,当然了,你也可以直接用cp命令将数据文件复制到从数据库去,注意在导出数据之前先对主数据库进行read lock,以保证数据的一致性
mysql> flush tables with read lock; query ok, 0 rows affected (0.19 sec)
然后mysqldump
导出数据:
4、得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复。
mysql> show master status; +------------------+----------+--------------+------------------+ | file | position | binlog_do_db | binlog_ignore_db | +------------------+----------+--------------+------------------+ | mysql-bin.000007 | 517 | test | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
最好在主数据库备份完毕,恢复写操作。
mysql> unlock tables; query ok, 0 rows affected (0.28 sec)
5、将刚才主数据备份的test.sql复制到从数据库(navicat、phpmyadmin、命令行都可以),进行导入。
6、修改从数据库的my.cnf,增加server-id参数,指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置。打开从机b的my.cnf,输入(修改之后必须重启mysql服务)
server-id = 2 log_bin = /var/log/mysql/mysql-bin.log master-host =192.168.10.111 master-user =backup master-pass =123456 master-port =3306 master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒) replicate-do-db =test #只复制某个库 replicate-ignore-db=mysql #不复制某个库
7、在从服务器上,启动slave进程
mysql> start slave;
8、在从服务器进行show salve status验证
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: 192.168.10.111 master_user: root master_port: 3306 connect_retry: 5 master_log_file: mysql-bin.000007 read_master_log_pos: 263 relay_log_file: mysqld-relay-bin.000002 relay_log_pos: 408 relay_master_log_file: mysql-bin.000007 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: 263 relay_log_space: 564 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: 1 row in set (0.00 sec)
提示
slave_io_running: yes
slave_sql_running: yes
说明配置成功了
9、测试主从服务器是否能同步
插入 修改 删除 增加字段 修改字段 增加表自己测试都可以
[xjp@server22 ~]$mysql -uroot -p123456 mysql> create database test; mysql> create table user(id int); mysql> insert into user values(1),(2),(3),(4),(5),(6); query ok, 2 rows affected (0.00 sec) records: 2 duplicates: 0 warnings: 0 mysql> select * from user; +----+ | id | +----+ | 1 | | 2 | +----+ 2rows in set (0.00 sec) mysql> select * from user; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 6 rows in set (0.00 sec) mysql> update user set id=11 where id=1; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mysql> select * from user; +----+ | id | +----+ | 11 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 6 rows in set (0.00 sec) mysql> delete from user where id=2; query ok, 1 row affected (0.00 sec) mysql> select * from user; +----+ | id | +----+ | 11 | | 3 | | 4 | | 5 | | 6 | +----+ 5 rows in set (0.00 sec) mysql> alter table user add name varchar(50); query ok, 5 rows affected (0.01 sec) records: 5 duplicates: 0 warnings: 0 mysql> select * from user; +----+------+ | id | name | +----+------+ | 11 | null | | 3 | null | | 4 | null | | 5 | null | | 6 | null | +----+------+ 5 rows in set (0.00 sec) mysql> alter table user modify column name varchar(200); query ok, 5 rows affected (0.01 sec) records: 5 duplicates: 0 warnings: 0 mysql> desc user; +-------+--------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | no | | null | | | name | varchar(200) | yes | | null | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> create table user2(id int); query ok, 0 rows affected (0.01 sec) mysql> show tables; +----------------+ | tables_in_test | +----------------+ | test | | user | | user2 | +----------------+ 3 rows in set (0.00 sec) mysql>
在从服务器查看是否同步过来 如果一致说明成功
mysql> use test; database changed mysql> select * from user; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 2 rows in set (0.00 sec) mysql> select * from user; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 6 rows in set (0.00 sec) mysql> select * from user; +----+ | id | +----+ | 11 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 6 rows in set (0.00 sec) mysql> select * from user; +----+ | id | +----+ | 11 | | 3 | | 4 | | 5 | | 6 | +----+ 5 rows in set (0.00 sec) mysql> select * from user; +----+------+ | id | name | +----+------+ | 11 | null | | 3 | null | | 4 | null | | 5 | null | | 6 | null | +----+------+ 5 rows in set (0.00 sec) mysql> desc user; +-------+--------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | no | | null | | | name | varchar(200) | yes | | null | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> show tables; +----------------+ | tables_in_test | +----------------+ | test | | user | | user2 | +----------------+ 3 rows in set (0.00 sec) mysql> mysql binlog日志查看: show binlog events\g; *************************** 12. row *************************** log_name: mysql-bin.000007 pos: 985 event_type: query server_id: 1 end_log_pos: 1075 info: use `test`; delete from user where id=2 *************************** 13. row *************************** log_name: mysql-bin.000007 pos: 1075 event_type: query server_id: 1 end_log_pos: 1175 info: use `test`; alter table user add name varchar(50) *************************** 14. row *************************** log_name: mysql-bin.000007 pos: 1175 event_type: query server_id: 1 end_log_pos: 1287 info: use `test`; alter table user modify column name varchar(200) *************************** 15. row *************************** log_name: mysql-bin.000007 pos: 1287 event_type: query server_id: 1 end_log_pos: 1376 info: use `test`; create table user2(id int) 15 rows in set (0.00 sec)
很早的时候搭建的了,走不少弯路,根据当时记录的写了下
希望本文所述对大家centos服务器设置有所帮助。