我们分别创建两台mysql 注意需要在同一网段
一台是阿里云服务器 一台是本地虚拟机 都是安装在docker 中 主库我们定位本地虚拟机为主库 从库为阿里云服务器
server-id = 1 log-bin=master-bin #添加,主服务器开启二进制日志 log-slave-updates=true #添加,允许从服务器更新二进制日志
重启 mysql
systemctl restart mysqld 如果是在docker环境 docker restart mysql
进入docker 中的mysql
docker exec -it mysql /bin/bash
mysql -u root -p
%代表所有的主机都能访问
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.0.139' IDENTIFIED BY '123456'; #给从服务器授权
查看是否创建成功
select user,host from mysql.user;
show master status;
------------------------
从库配置
vim /etc/my.cnf server-id = 2 #修改,注意id与Master的不同,两个Slave的id也要不同 relay-log=relay-log-bin #添加,开启中继日志,从主服务器上同步日志文件记录到本地 relay-log-index=slave-relay-bin.index #添加,定义中继日志文件的位置和名称
重启mysql
systemctl restart mysqld
docker 环境运行以下命令
docker restart mysql
mysql -u root -p change master to master_host='192.168.0.138' , master_user='myslave',master_password='123456',master_log_file='master-bin.000002',master_log_pos=1024; #配置同步,注意 master_log_file 和 master_log_pos 的值要与Master查询的一致,这里的是例子,每个人的都不一样 start slave; #启动同步,如有报错执行 reset slave; show slave status\G; #查看 Slave 状态 //确保 IO 和 SQL 线程都是 Yes,代表同步正常。 Slave_IO_Running: Yes #负责与主机的io通信 Slave_SQL_Running: Yes #负责自己的slave mysql进程
如果出现 Slave_IO_Running: No
如果是考培主配置文件 删除重新创建my.conf文件 会报ids 错误
https://blog.csdn.net/m820061671/article/details/52383293?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166133155416781647562716%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166133155416781647562716&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-52383293-null-null.142^v42^pc_rank_34,185^v2^control&utm_term=%20The%20slave%20I%2FO%20thread%20stops%20because%20master%20and%20slave%20have%20equal%20MySQL%20server%20ids%3B%20these%20ids%20must%20be%20different%20for%20replication%20to%20work%20%28or%20the%20--replicate-same-server-id%20option%20must%20be%20used%20on%20slave%20bu&spm=1018.2226.3001.4187
uuids 报错
删除 data 目录下的 auto.conf文件 重启mysql 会自动生成
有可能 在配置文件中没有生效
show variables like 'server_id'; set global server_id=2;
reset slave;
-------------------------------
读写分离
#数据的读写分离配置 在进行读写分离之前必须先要满足数据库的主从复制 spring.shardingsphere.datasource.names=master,slave #主数据源 master spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.master.url=jdbc:mysql://192.168.0.138:3306/rw?characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.shardingsphere.datasource.master.username=root spring.shardingsphere.datasource.master.password=root #从数据源 slave spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.slave.url=jdbc:mysql://192.168.0.139:3306/rw?characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.shardingsphere.datasource.slave.username=root spring.shardingsphere.datasource.slave.password=root #可以配置多个从库 #读写分离配置 轮询 spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin #最终数据源名称 spring.shardingsphere.masterslave.name=dataSource #主库数据源名称列表 多个逗号分隔 就是上面自定义的主库名称 spring.shardingsphere.masterslave.master-data-source-name=master #从库数据源名称列表 多个逗号分隔 就是上面自定义的从库名称 spring.shardingsphere.masterslave.slave-data-source-names=slave #显示sql spring.shardingsphere.props.sql.show=true #允许bean信息被覆盖 spring.main.allow-bean-definition-overriding=true
标签:主从复制,slave,spring,mysql,msyql,master,datasource,shardingsphere From: https://www.cnblogs.com/Lcch/p/16622766.html