一、mysql 之 master
1、关闭防火墙,SELinux 【或者写一条防火墙入栈规则也行】
systemctl stop firewalld systemctl disable firewalld vim /etc/selinux/config 第七行enforcing 改为 disabled
或者写入防火墙规则:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=80/udp --permanent
firewall-cmd --reload <------ 重载firewalld
2、修改配置文件my.cnf :vim /etc/my.cnf
#配置mysql 的server ,client 的默认字符集为utf8
[mysqld] character-set-server=utf8 collation-server=utf8_general_ci log-error=/var/log/mysqld.log
#设置master
server-id=1
#开启二进制日志
log-bin=mysql_binlog
[client] default-character-set=utf8 [mysql] default-character-set=utf8
3、进入数据库,创建一个用于主从同步的账号
mysql>create user 'slave'@'%' identified by '666666';
4、给创建账号授权,允许同步
mysql>grant replication slave on *.* to 'slave'@'%' ;
5、锁定数据库,防止数据写入
mysql> flush tables with read lock;
6、配置完从库后,主库解锁
mysql> unlock tables;
7、主库执行:show master status; 记下 Position 和 File 的值,等下进行从库配置要用到
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-binlog.000023 | 2720 | test1,test2 | mysql
+------------------+----------+--------------+------------------+
二、mysql 之slave
1、修改配置文件my.cnf
#配置mysql 的server ,client 的默认字符集为utf8 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci log-error=/var/log/mysqld.log #设置slave server-id=3 #开启只允许读 read-only=true [client] default-character-set=utf8 [mysql] default-character-set=utf8
2、设置从库同步的master信息
mysql> change master to master_host='主库ip' , master_user='主库设置的用户slave', master_password='666666',master_log_file='mysql_binlog.000023',mysql_log_pos=470;
3、开启主从同步
mysql> start slave;
4、查看信息
mysql> show slave statusG;
5、Slave_IO_Running、Slave_SQL_Running 都是yes 则表示配置成功
mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.119.10 Master_User: tiger Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1039 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 537 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
【如果不是两个yes,请先检查防火墙、selinux有没有关闭……】