mysql主从分离
1、工具:
两台机器
master:192.168.0.1
slave:192.168.0.2
2、master的配置
找到mysql的配置文件,一般centos的是/etc/my.cnf,ubuntu的是/etc/mysql/mysql.conf.d/mysqld.cnf
跳到到[mysqld]下配置
bind-address = 192.168.0.1 #可注释 server-id = 1 #架构中唯一节点id log_bin=mysql-bin log_bin = /var/log/mysql/mysql-bin.log #开启binlog binlog-ignore-db=mysql #不需要复制的数据库 binlog-do-db=ufind_db #要复制的数据库
重启master
service mysql restart
进入mysql命令行,加用户
CREATE USER 'zhylioooo'@'%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE ON *.* TO 'zhylioooo'@'%';
导出master数据库
给数据库加锁使其只可读
FLUSH TABLES WITH READ LOCK;
到处数据库文件
mysqldump -u root -p --all-databases --master-data > dbdump.sql
给数据库解锁
UNLOCK TABLES;
查看master的二进制记录文件及文件当前记录的位置
SHOW MASTER STATUS;
+
------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+
------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1050 | | | |
+
------------------+----------+--------------+------------------+-------------------+
1 row
in
set
(0.00 sec)
3、slave配置
slave配置重点
server-id = 2 #架构中唯一节点id,比master大 log_bin=mysql-bin log_bin = /var/log/mysql/mysql-bin.log #开启binlog
重启mysql
导入master导出的数据库文件,使数据库一致
mysql -u root -p < dbdump.sql
进入mysql使slave与master连接
STOP SLAVE; CHANGE MASTER TO -> MASTER_HOST='192.168.0.1', -> MASTER_USER='zhylioooo', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=1050; START SLAVE;
4、集群策略:https://www.cnblogs.com/KTblog/p/5122825.html