上一章我们讲了再Laravel中配置读写分离和负载均衡,那么有点小小的问题还需要在本节中完成,就是mysql的主从复制,如果没有主从复制的话,数据会有些问题,所以本节也把最重要的内容分享出来,大家学习。
配置环境
操作系统:两台CentOS 7.6的Linux系统(虚拟机)
数据库版本:MySQL 5.7
主服务器IP:192.168.1.100
从服务器IP:192.168.1.101
安装Mysql数据库(后面单独出一篇mysql的安装教程)
我们现在就简单安装一下mysql,新手按步骤进行操作:
1、先检查系统是否安装有mysql
[root@localhost~]#yum list installed mysql*
[root@localhost~]#rpm –qa|grep mysql*
2、查看有没有安装包
[root@localhost~]#yum list mysql*
3、安装mysql客户端
[root@localhost~]#yum install mysql
4、安装mysql服务端
[root@localhost~]#yum install mysql-server
[root@localhost~]#yum install mysql-devel
5、开启端口
vi/etc/sysconfig/iptables
-AINPUT-p tcp-m tcp –dport3306-jACCEPT
6、重启防火墙
systemctl restart iptables.service--重启防火墙
systemctl stop iptables.service--关闭防火墙
配置主(Master)数据库
1、修改数据库配置文件
[root@localhost~]# vi/etc/my.cnf
更改配置文件
[mysqld]#开启二进制日志
log-bin=mysql-bin
#标识唯一id(必须),一般使用ip最后位
server-id=100#不同步的数据库,可设置多个
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#指定需要同步的数据库(和slave是相互匹配的),可以设置多个
binlog-do-db=test
注:日志的存储容量设置的大小是根据你的服务器资源实际情况修改的。
2、重启数据库服务(mysqld)
service mysqld restart
3、登陆MySQL数据库允许从库获得主库日志
[root@localhost~]# mysql-u root-p"你的密码"
注:第一次登陆是不需要输入root的密码的,有密码就要输入哦。
进入后做如下配置:
#给从库放权限
mysql>GRANTFILEON*.*TO'repl'@'192.168.1.101'IDENTIFIEDBY'repl password';#创建用户
mysql>GRANTREPLICATIONSLAVEON*.*TO'repl'@'192.168.0.101'IDENTIFIEDBY'repl password';#修改用户权限
mysql>select host,user,passwordfrommysql.user;#查看是否修改成功
mysql>FLUSHPRIVILEGES;#刷新权限
4、重启MySQL服务,登录MySQL,查看主库信息
[root@localhost~]# service mysqld restart #重启mysql服务[root@localhost~]# mysql-u root-p #登陆mysql
mysql>show master status;#查看master状态
显示大概如下内容
+------------------+----------+--------------+----------------------------------+-------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
+------------------+----------+--------------+----------------------------------+-------------------+
|mysql-bin.000007|120|ufind_db|information_schema,performance_schema,mysql||
+------------------+----------+--------------+----------------------------------+-------------------+
1rowinset(0.00sec)
注:如果执行这个步骤始终为Empty set(0.00 sec),那说明前面的my.cnf没配置对,请回去重新检查配置步骤。
配置从(Slave)数据库
1、修改从库的数据库配置文件
[root@localhost~]# vi/etc/my.cnf
将里面的内容修改为
#开启二进制日志
log-bin=mysql-bin
server-id=101binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#与主库配置保持一致
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2、重启MySQL服务,登录MySQL
[root@localhost~]# service mysqld restart
[root@localhost~]# mysql-u root-p"你的密码"
并作如下修改:
#关闭Slave
mysql>stop slave;#设置连接主库信息
mysql>change master to master_host='192.168.1.100',master_user='repl',master_password='repl password',master_log_file='mysql-bin.000007',master_log_pos=120;#开启Slave
mysql>start slave;
注:上面的master_log_file是在配置Master的时候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一对应
3、查看从库状态信息
mysql>show slave status \G;
如下信息:
*************************1.row*************************Slave_IO_State:Waitingformaster to send event
Master_Host:192.168.1.100Master_User:root
Master_Port:3306Connect_Retry:60Master_Log_File:mysql-bin.000007Read_Master_Log_Pos:120Relay_Log_File:localhost-relay-bin.000007Relay_Log_Pos:520Relay_Master_Log_File:mysql-bin.000007Slave_IO_Running:Yes//显示yes为成功Slave_SQL_Running:Yes//显示yes为成功,如果为no,一般为没有启动masterReplicate_Do_DB:test
Replicate_Ignore_DB:mysql//上面的都是配置文件中的信息Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:0Last_Error:Skip_Counter:0Exec_Master_Log_Pos:357Relay_Log_Space:697Until_Condition:None
Until_Log_File:Until_Log_Pos:0Master_SSL_Allowed:No
Master_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert:No
Last_IO_Errno:0Last_IO_Error://如果为no,此处会显示错误信息Last_SQL_Errno:0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:2Master_UUID:be0a41c0-2b40-11e8-b791-000c29267b6a
Master_Info_File:/usr/local/mysql/data/master.info
SQL_Delay:0SQL_Remaining_Delay:NULLSlave_SQL_Running_State:Slave has read all relay log;waitingforthe slaveI/Othread to update it
Master_Retry_Count:86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position:01
rowinset(0.00sec)
至此整个过程就配置好了。现在
可以在主服务器上创建一个表,然后在从服务器上查询刚创建的这个表,看是否存在就可以啦。
运行测试
1、关于增删改查,主从数据不一致问题:
原因:
在主库的logbin中的确有执行删除语句,但是在从库的logbin中却没有删除语句。
解决:
使用 use database 选取当前数据库架构中的需要操作的数据库,然后在执行删除,OK同步成功。
2、查询binlog主从日志的方法
查看binlog全部文件
mysql>show binary logs;#查看binlog是否开启NO为开启
mysql>show variables like'log_bin%';#详细信息
mysql>show variables like'binlog%';#查看binlog日志
mysql>show binlog eventsin'mysql-bin.000007';#或者使用mysqlbinlog,如果报错使用--no-defaults(使用全路径)[root@localhost~]#/usr/local/mysql/bin/mysqlbinlog--no-defaults/usr/local/mysql/data/mysql-bin.000019
3、手动清理master日志,最好关闭日志,在/etc/my.cnf
#手动刷新日志
mysql>show master status;#删除全部
mysql>reset slave; #或 rest master;
#删除MySQL-bin.004
mysql>PURGEMASTERLOGSTO'MySQL-bin.004';
4、基本命令