上一章我们讲了再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、查看有没有安装包
3、安装mysql客户端 [root@localhost ~]#yum install mysql 4、安装mysql服务端
5、开启端口 vi /etc/sysconfig/iptables-A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT 6、重启防火墙
配置主(Master)数据库 1、修改数据库配置文件 [root@localhost ~]# vi /etc/my.cnf 更改配置文件
2、重启数据库服务(mysqld) service mysqld restart 3、登陆MySQL数据库允许从库获得主库日志
进入后做如下配置: #给从库放权限mysql>GRANT FILE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY 'repl password'; #创建用户mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.101' IDENTIFIED BY 'repl password'; #修改用户权限mysql>select host,user,password from mysql.user; #查看是否修改成功mysql>FLUSH PRIVILEGES; #刷新权限 4、重启MySQL服务,登录MySQL,查看主库信息
显示大概如下内容 +------------------+----------+--------------+----------------------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+----------------------------------+-------------------+| mysql-bin.000007 | 120 | ufind_db |information_schema,performance_schema,mysql | |+------------------+----------+--------------+----------------------------------+-------------------+1 row in set (0.00 sec)
配置从(Slave)数据库 1、修改从库的数据库配置文件
将里面的内容修改为 #开启二进制日志log-bin=mysql-binserver-id=101binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysql#与主库配置保持一致replicate-do-db=testreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allslave-net-timeout=60 2、重启MySQL服务,登录MySQL
并作如下修改: #关闭Slavemysql> 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;#开启Slavemysql> start slave;
3、查看从库状态信息
如下信息: ************************* 1. row ************************* Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.100 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 120 Relay_Log_File: localhost-relay-bin.000007 Relay_Log_Pos: 520 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes //显示yes为成功 Slave_SQL_Running: Yes //显示yes为成功,如果为no,一般为没有启动master 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: 357 Relay_Log_Space: 697 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: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: //如果为no,此处会显示错误信息 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 01 row in set (0.00 sec) 至此整个过程就配置好了。现在可以在主服务器上创建一个表,然后在从服务器上查询刚创建的这个表,看是否存在就可以啦。 运行测试 1、关于增删改查,主从数据不一致问题:原因:在主库的logbin中的确有执行删除语句,但是在从库的logbin中却没有删除语句。解决:使用 use database 选取当前数据库架构中的需要操作的数据库,然后在执行删除,OK同步成功。 2、查询binlog主从日志的方法
3、手动清理master日志,最好关闭日志,在/etc/my.cnf #手动刷新日志mysql> show master status;#删除全部mysql> reset slave; #或 rest master;#删除MySQL-bin.004mysql> PURGE MASTER LOGS TO 'MySQL-bin.004'; 4、基本命令
|
|