
Mysql主从复制:


网课这里使用两个虚拟机,一个做主库,一个做从库
修改ip:(8条消息) VMWare克隆多台虚拟机并修改ip_虚拟机克隆修改ip_头顶一点云的博客-优快云博客
主库配置:

服务器id不固定,只要保证唯一即可


ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
修改密码等级和长度:
set global validate_password_policy=LOW;
set global validate_password_length=密码长度;
然后重试即可

从库配置:

重启mysql服务:systemctl restart mysqld

停止slave:stop slave;

show slave status\G;纵表查看,更整齐

Slave_SQL_Running为No看这个:(8条消息) 配置数据库主从复制时Slave_IO_Running为No的问题与解决_家里有只mini猫的博客-优快云博客
测试成功:

Sharding-JDBC:

导入依赖:
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
配置读写规则:
server:
port: 8080
spring:
shardingsphere:
datasource:
names:
master,slave
# 主数据源
master: # 这里的master和上面的names.master对应
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://主库IP:3306/库名?characterEncoding=utf-8
username: root
password: 主库密码
# 从数据源
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://从库IP:3306/库名?characterEncoding=utf-8
username: root
password: 从库密码
masterslave:
# 读写分离配置
load-balance-algorithm-type: round_robin # 负载均衡策略: 轮询
# 最终的数据源名称(Bean的名字)
name: dataSource
# 主库数据源名称
master-data-source-name: master
# 从库数据源名称列表,多个逗号分隔
slave-data-source-names: slave
props:
sql:
show: true #开启SQL显示,默认false
配置允许Bean定义覆盖:
spring:
main:
allow-bean-definition-overriding: true
JDBC会通过不同数据源(主和从)来操作不同数据库
项目集成报错:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
解决:(8条消息) Java启动异常:The driver has not received any packets from the server_DNYDYS的博客-优快云博客