17.1 MySQL主从介绍
- MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
- MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
- 主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行 - 主上有一个log dump线程,用来和从的I/O线程传递binlog
- 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
- 使用场景:
1). 数据库备份
2).数据库备份, 客户端也要到 从数据库上读数据, 但不能到 从数据库上写数据. 减轻主库读的压力
17.2 准备工作
准备两台机器,分别安装mysql,版本最好是相同的
安装过程可以参考https://blog.51cto.com/13736286/2135537 mysql的安装
这里已经安装好了,一台hostname为master,ip为192.168.66.128 一台hostname为slave,ip为192.168.66.129
配置之前,保持两台mysql的数据是一致的,可以通过mysqldump把主上数据备份后,上传到从上,然后恢复17.3 配置主
1、在master上修改mysql配置文件/etc/my.cnf,设置server-id和打开log-bin二进制日志
[root@master ~]# vi /etc/my.cnf #增加下面两行 server-id=1 #本机标识号,唯一,数字可以随意设置,和从上要不一样 log_bin=master #打开log-bin,master为log-bin日志的前缀
2、修改完配置文件后,启动或者重启mysqld服务
[root@master ~]# /etc/init.d/mysqld restart
3、重启完成后在mysql数据目录下会生成相应的mater开头的二进制日志文件, 这些文件是实现主从的根本,
我的数据目录是在/var/lib/mysql下[root@master ~]# ll /var/lib/mysql/ 总用量 28772 -rw-rw----. 1 mysql mysql 18874368 7月 16 06:42 ibdata1 -rw-rw----. 1 mysql mysql 5242880 7月 16 06:42 ib_logfile0 -rw-rw----. 1 mysql mysql 5242880 7月 16 06:42 ib_logfile1 -rw-rw---- 1 mysql mysql 106 7月 16 06:39 master.000001 -rw-rw---- 1 mysql mysql 16 7月 16 06:39 master.index drwx------. 2 mysql mysql 4096 5月 5 02:28 mysql srwxrwxrwx 1 mysql mysql 0 7月 16 06:39 mysql.sock drwx------. 2 mysql mysql 4096 5月 5 04:23 performance_schema drwx------ 2 mysql mysql 4096 5月 20 10:12 test
注:myster.000001就是二进制日志文件
4、登陆mysql,创建用作同步数据的用户mysql> grant replication slave on *.* to 'repl'@'192.168.66.129' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
5、为了保持数据一致, 把表锁一下, 不继续写.
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)
6、查看主的状态,并记录下log-bin文件的名字和偏移量
mysql> show master status; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | master.000001 | 336 | | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
注:master.000001为log-bin文件的名字,336为偏移量,指定从这个点开始同步
17.4 配置从
1、修改从的mysql配置文件/etc/my.cnf,只用设置server-id即可,不用打开log-bin
[root@slave ~]# vi /etc/my.cnf #增加一行 server-id=2 #这个数字可以随意设置,但要区别于主设置的id
2、修改完配置文件,重启mysql服务
[root@slave ~]# /etc/init.d/mysqld restart
3、登陆从的mysql,停止slave,并更改主的信息,这一步非常关键
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='192.168.66.128', master_user='repl', master_password='123456', master_log_file='master.000001', master_log_pos=336; Query OK, 0 rows affected (0.06 sec)
注意:
这一步很关键,这里的change master的信息就是主的ip 还有创建授权的用户和密码,还有master的 log-bin文件和偏移量
4、启动slave,并查看状态mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.66.128 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master.000001 Read_Master_Log_Pos: 336 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 249 Relay_Master_Log_File: master.000001 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: 336 Relay_Log_Space: 405 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)
可以看到刚才配置的主的一些信息,关键的两个指标 Slave_IO_Running: Yes Slave_SQL_Running: Yes,看这两个状态是否为Yes,Yes表示正常,如果其中有一个为No表示不正常,如果有错误可以在 Last_Errno: 0 Last_Error: 两个指标中体现出
来
至此,从配置完成
下面这一步在主上执行
不要忘记,刚才主上为了防止写入数据锁表了,要解锁,回到主上,登陆mysql,执行mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)
17.5 测试主从同步
1、几个配置参数在主从my.cnf设置
(主和从设置一个即可)
可在mysql的配置文件my.cnf中增加以下参数,表示,要同步或忽略的库
主:可支持逗号分隔设置多个binlog-do-db= //仅同步指定的库,多个建议写多行 binlog-ignore-db= //忽略指定库
从:
replicate_do_db= //仅同步指定的库 replicate_ignore_db= //忽略指定库 replicate_do_table= //仅同步指定的表(慎用) --假设表示test.xx 带结尾的也会忽略导致库不完整,so,建议用wild下面2个支持通配。 replicate_ignore_table= //忽略指定的表(慎用) replicate_wild_do_table= //同步指定的表支持通配(如test.%) replicate_wild_ignore_table= //忽略指定的表支持通配(如test.%)
2、测试主从是否同步
在主上创建一个数据库luomysql> create database luo; Query OK, 1 row affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | luo | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
在从上直接查看是否自动创建了数据库luo
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | luo | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
可以看到从上已经自动创建了数据库luo,说明数据是同步的
扩展的一些链接:
mysql主从
https://my.oschina.net/u/3791387/blog/1837336
相关扩展
不停库不锁表在线主从配置
http://seanlook.com/2015/12/14/mysql-replicas/
主从不同步
http://www.rfyy.net/archives/2309.html
https://blog.51cto.com/storysky/259280
主主
关于 auto_increment https://blog.csdn.net/leshami/article/details/39779509
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 实现读写分离
https://blog.51cto.com/zzclinux/1980487
mysql-proxy类似的产品有:
mycat 基于阿里的开源软件cobar,官网 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370
mycat实现分库分表
https://www.cnblogs.com/joylee/p/7513038.html
atlas 出自于360,不维护不更新了 https://blog.csdn.net/AnPHPer/article/details/80566385
mysql环形主从
http://ask.apelearn.com/question/11437
mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.html
MHA架构
https://blog.51cto.com/xiaoshuaigege/2060768
比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026