• MySQL主从又叫作Replication、AB复制。简单讲就是A和B两台机器作主从后,在A上写数据,另一台B也会跟着写数据,二者数据实时同步的mysql
• MySQL主从是基于binlog的,主上须开启binlog才能进行主从。linux
• 主从过程大体有3个步骤web
• 1)主将更改操做记录到binlog里sql
• 2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里数据库
• 3)从根据relaylog里面的sql语句按顺序执行vim
• 主上有一个log dump线程,用来和从的I/O线程传递binlog服务器
• 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另一个SQL线程用来把relaylog里面的sql语句落地socket
MySQL主从的原理图ide
其中Msater是主,Slave是从测试
MySQL主从的做用
注:mysql主从,是有方向性的,写数据,必须从主机器开始;若是不依照原理会致使数据紊乱
准备两台机器,每台机器安装msyql服务,并启动mysql服务
1.下载安装包(二进制免编译)-->2.解压压缩包-->3.初始化-->4.修改配置文件-->5.复制启动脚本-->6.修改启动脚本-->7.启动mysql-->8.开机启动mysql
首先下载二进制免编译的包,下载到/usr/local/src/目录下
解压完以后,把解压出来的目录放到 /usr/local/mysql/ 目录下
注:首先检查 /usr/local/mysql/ 目录是否存在,如果这个目录存在,首先把这个目录改个名字,或者把目录下的内容删除,而后把解压出来的目录放到 /usr/local/mysql/ 目录下面
cd /usr/local/mysql/ ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
注:其中的--user=mysql 须要提早建立mysql用户;初始化成功的标志就是两个OK,或者用 echo $? 检查是否初始化成功
vim /etc/my.cnf 定义 datadir=/data/mysql 定义 socket=/tmp/mysql.sock
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld //对如下两行进行指定路径 指定basedir的路径 /usr/local/mysql 指定datadir的路径 /data/mysql
/etc/init.d/mysql start
若是启动失败,能够去查看错误日志;
查看 /data/mysql 目录下的文件,默认属主、属组,若是不是mysql的,启动时会因没法写入数据而不能启动mysql,须要改变属主和属组
chomd mysql:mysql /data/mysql
chkconfig mysqld on
vim /etc/my.cnf 增长server-id=5和log_bin=lemlinux1
[root@linux-5 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
[root@linux-5 ~]# cd /data/mysql/ [root@linux-5 mysql]# ls -lt 总用量 110676 -rw-rw----. 1 mysql mysql 50331648 7月 5 23:59 ib_logfile0 -rw-rw----. 1 mysql mysql 12582912 7月 5 23:59 ibdata1 -rw-rw----. 1 mysql mysql 54024 7月 5 23:59 linux-5.err -rw-rw----. 1 mysql mysql 5 7月 5 23:59 linux-5.pid -rw-rw----. 1 mysql mysql 19 7月 5 23:59 lemlinux1.index -rw-rw----. 1 mysql mysql 120 7月 5 23:59 lemlinux1.000001 drwx------. 2 mysql mysql 4096 7月 4 23:34 zrlog -rw-rw----. 1 mysql mysql 56 5月 24 19:58 auto.cnf drwx------. 2 mysql mysql 4096 5月 24 18:35 mysql drwx------. 2 mysql mysql 4096 5月 24 18:35 performance_schema -rw-rw----. 1 mysql mysql 50331648 5月 24 18:35 ib_logfile1 drwx------. 2 mysql mysql 6 5月 24 18:35 test
其中.index;.000001这些文件是实现MySQL主从的根本
.index 索引页,这个文件是必需要有的
.000001 这个是二进制日志文件,会持续生成二、三、4等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法完成主从)
把zrlog库备份并恢复成lem库,做为测试数据
mysqldump -uroot zrlog > /tmp/lem.sql //zrlog数据库备份 mysql -uroot -e "create database lem" //建立新库lem mysql -uroot lem < /tmp/lem.sql //将zrlog数据库备份数据导入lem库中
[root@linux-5 mysql]# ls -lt 总用量 110688 -rw-rw----. 1 mysql mysql 50331648 7月 6 00:09 ib_logfile0 -rw-rw----. 1 mysql mysql 12582912 7月 6 00:09 ibdata1 -rw-rw----. 1 mysql mysql 10152 7月 6 00:08 lemlinux1.000001 drwx------. 2 mysql mysql 4096 7月 6 00:08 lem -rw-rw----. 1 mysql mysql 54024 7月 5 23:59 linux-5.err -rw-rw----. 1 mysql mysql 5 7月 5 23:59 linux-5.pid -rw-rw----. 1 mysql mysql 19 7月 5 23:59 lemlinux1.index drwx------. 2 mysql mysql 4096 7月 4 23:34 zrlog -rw-rw----. 1 mysql mysql 56 5月 24 19:58 auto.cnf drwx------. 2 mysql mysql 4096 5月 24 18:35 mysql drwx------. 2 mysql mysql 4096 5月 24 18:35 performance_schema -rw-rw----. 1 mysql mysql 50331648 5月 24 18:35 ib_logfile1 drwx------. 2 mysql mysql 6 5月 24 18:35 test
能够看到lemlinux1.000001二进制文件是有增长的,lemlinux1.000001增加的大小是和zrlog这个数据库备份的大小保持一致的,lemlinux1.000001文件里完整的记录了数据库的建立的库,建立的表,以及表里的内容,经过完整的lemlinux1.000001文件也能够恢复数据库的数据
在MySQL主从中,从数据库须要到主数据库上同步日志,这个过程须要认证,所以咱们须要在主库上建立一个用户用于从库登陆同步日志。
grant replication slave on *.* to 'repl'@192.168.88.10(从库IP) identified by '123456';
锁定表,目的是不让表继续写,由于一会须要作从机器配置,须要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错
flush tables with read lock;
查看一下binlog的文件和大小,并记住binlog的filename
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | lemlinux1.000001 | 10362 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
查看/data/mysql/下有哪些库,主上有哪些库,从上也得有哪些库,同步这些库,就意味着这些数据都要备份过去
[root@linux-5 mysql]# ls /data/mysql/ auto.cnf ib_logfile0 lem lemlinux1.index linux-5.pid performance_schema zrlog ibdata1 ib_logfile1 lemlinux1.000001 linux-5.err mysql test
备份数据库,除了mysql库,由于mysql库里面有帐号密码,从上备份的时候不可能把全部权限复制过去,因此mysql不须要备份
mysqldump -uroot test > /tmp/lem2.sql
在主从同步时把主上/tmp/目录下 .sql文件都拷贝到从上去
vim /etc/server 增长server-id = 10
从库不须要生成二进制日志文件,只有主库须要
[root@linux-10 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
[root@linux-10 ~]# scp 192.168.88.5:/tmp/*.sql /tmp/ Warning: Permanently added '192.168.88.5' (ECDSA) to the list of known hosts. root@192.168.88.5's password: lem2.sql 100% 1258 1.0MB/s 00:00 lem.sql 100% 9867 4.3MB/s 00:00
mysql> create database lem; Query OK, 1 row affected (0.00 sec) mysql> create database zrlog; Query OK, 1 row affected (0.00 sec) mysql> create database test; Query OK, 1 row affected (0.00 sec)
root@linux-10 ~]# mysql -uroot -p12345678 zrlog < /tmp/lem.sql Warning: Using a password on the command line interface can be insecure. [root@linux-10 ~]# mysql -uroot -p12345678 lem < /tmp/lem.sql Warning: Using a password on the command line interface can be insecure. [root@linux-10 ~]# mysql -uroot -p12345678 test < /tmp/lem2.sql Warning: Using a password on the command line interface can be insecure.
stop slave; //中止同步
change master to master_host='192.168.88.5', master_user='repl', master_password='123456', master_log_file='lemlinux1.000001', master_log_pos=10362;
master_host='192.168.88.5',指定主机器host
master_user='repl',指定主机器用户
master_password='123456',指定主机器密码
master_log_file='lemlinux1.000001',指定binlog文件名
master_log_pos=10362,指定binlog文件大小
master_log_file和master_log_pos是由主库show master status
也能够指定主机器的port,由于在生产环境中,也会有人更改mysql的默认端口 master_port=端口号,若是是默认端口则无需添加
start slave;
mysql> show slave status\G //\G后不用加分号,自己就是结束符 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.88.5 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: lemlinux1.000003 Read_Master_Log_Pos: 120 Relay_Log_File: linux-10-relay-bin.000005 Relay_Log_Pos: 283 Relay_Master_Log_File: lemlinux1.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error:
成功的标志是 Slave_IO_Running和Slave_SQL_Running两项是否均为yes
还须要关注
Seconds_Behind_Master: 0 //为主从延迟的时间
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
unlock tables;
17.5 测试主从同步
主从中有一些配置参数可使数据库同步时更加精确,具备目的性。
binlog-do-db= //仅同步指定的库 binlog-ignore-db= //忽略指定库
replicate_do_db= //仅同步指定的库 replicate_ignore_db= //忽略指定库 replicate_do_table= //仅同步指定的表 replicate_ignore_table= //忽略指定表, replicate_wild_do_table= //如aming.%, 支持通配符% 指定同步靠谱的匹配 同步表 replicate_wild_ignore_table= //如aming.%, 支持通配符% 指定同步靠谱的匹配 忽略表
使用前4条参数时会有必定的局限性,例如:有一个临时表,写的数据很是快,数据也大,天天都须要删除或者更新,那么就不须要天天去作同步,而且在面对联合查找时,可能会形成数据不完整的状况,所以在设置主从配置参数时,尽可能使用后两条。
mysql> use lem Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------+ | Tables_in_lem | +---------------+ | comment | | link | | log | | lognav | | plugin | | tag | | type | | user | | website | +---------------+ 9 rows in set (0.00 sec) mysql> select count(*) from website; +----------+ | count(*) | +----------+ | 9 | +----------+ 1 row in set (0.00 sec) mysql> select * from website; +--------+---------------------+----------------------------------------------------------------------+--------+ | siteId | name | value | remark | +--------+---------------------+----------------------------------------------------------------------+--------+ | 1 | rows | 10 | NULL | | 2 | template | /include/templates/default | NULL | | 3 | autoUpgradeVersion | 86400 | NULL | | 4 | pseudo_staticStatus | false | NULL | | 5 | title | lemzrlog | NULL | | 6 | second_title | lem | NULL | | 7 | home | http://192.168.88.5/zrlog/ | NULL | | 8 | zrlogSqlVersion | 4 | NULL | | 9 | plugin_core_db_key | {"pluginInfoMap":{},"setting":{"disableAutoDownloadLostFile":false}} | NULL | +--------+---------------------+----------------------------------------------------------------------+--------+ 9 rows in set (0.00 sec) mysql> truncate table website; Query OK, 0 rows affected (0.01 sec) mysql> select count(*) from website; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
mysql> use lem Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(*) from website; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
从库的数据也被删除,说明主从配置成功
如果误操做了,好比在从机器误删除了,再去主上删除相同的数据,就会有可能致使主从失败
这时在从机器上 start slave;
而后在start slave;
再来查看show slave status\G
如果仍是失败,则只能从新作主从了
在主机器的数据库上 show mater status; 查看文件大小
而后在从机器上先stop slave;
而后直接change master to master_host='192.168.88.5', master_user='repl', master_password='123456', master_log_file='lemlinux1.000001', master_log_pos=10362;
由于基本还没作什么操做的,数据仍是一致的,直接改下数据大小就行
而后在从机器上 start slave;
再来查看 show slave status\G 看是否为两个Yes,若仍是报错只能从新从头作主从了