1、主库创建账号:
show master status;
GRANT REPLICATION SLAVE ON . to 'reader'@'%' identified by 'readerpwd';
flush privilegs
2、从库配置
开启binlog
log-bin=/var/lib/MysqL/MysqL-bin
server-id=3 //参照原从库配置+1
3、备份主库
MysqLdump -uroot -p123 --routines --single_transaction --master-data=2 --databases testdb > testdb.sql
参数说明:
- --routines:导出存储过程和函数
- --single_transaction:导出开始时设置事务隔离状态,并使用一致性快照开始事务,然后unlock tables;而lock-tables是锁住一张表不能写操作,直到dump完毕。
- --master-data:默认等于1,将dump起始(change master to)binlog点和pos值写到结果中,等于2是将change master to写到结果中并注释。
4、从库创建数据库,并导入数据
将dump的数据拷贝到从库后开始导数据
MysqL> grant all pricileges on *.* to testdb.* identified by 'testdb';
MysqL> create database testdb;
MysqL> source /tmp/testdb.sql
# head -25 testdb.sql
root@MysqL20151:/tmp# head -25 /tmp/0907.sql
-- MysqL dump 10.13 distrib 5.5.46, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: vphotos
-- ------------------------------------------------------
-- Server version 5.5.46-0ubuntu0.14.04.2-log
/*!40101 SET @OLD_CHaraCTER_SET_CLIENT=@@CHaraCTER_SET_CLIENT */;
/*!40101 SET @OLD_CHaraCTER_SET_RESULTS=@@CHaraCTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_sql_mode=@@sql_mode, sql_mode='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_sql_NOTES=@@sql_NOTES, sql_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE='MysqL-bin.003789', MASTER_LOG_POS=49778941;
可以看到 MASTER_LOG_FILE='MysqL-bin.003789', MASTER_LOG_POS=49778941;
6、启动从库
MysqL> change master to master_host='10.*.*.*',master_user='reader',master_password='readerpwd',master_log_file='MysqL-bin.003789',master_log_pos=49778941;
// 验证从库状态
MysqL> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.3.16.7
Master_User: slave02
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MysqL-bin.003791
Read_Master_Log_Pos: 99002276
Relay_Log_File: MysqLd-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: MysqL-bin.003789
Slave_IO_Running: Yes
Slave_sql_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
..................
注:看到IO和sql线程均为YES,说明主从配置成功。
参考:
https://yq.aliyun.com/articles/38826
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。