Preparing the environment
At least two servers are required, which must be two independent computers. Of course, virtual machines can also be used.
1) Construct the master and slave databases separately and output logs (for easy location of problems)
configure the log path
2) Installation and startup:
modify the my.ini path in the master to the master path
mysqld --install MySQLXY --defaults-file=" C:\Program Files\MySQL\MySQL Server XY\my.ini"
(install/remove of the service denied permission is not enough to run as an administrator)
net start MySQLXY
Sc delete master/slave If you make a mistake, you can delete the service and restart the operation to
open the log ( not necessary)
(Not required) Turn on the log of sql statement, it is not recommended to turn on in production environment:
Check the log directory and open the log of the sql statement:
mysql>show variables like'%general_log%';
mysql>set global general_log=on;
After opening, restart Mysql, the above-mentioned opening log configuration will be invalid.
master server configuration
1) Modify the mysql.ini of the master
log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_ignore_db=mysql
binlog_checksum=none
2) Restart the master service and log in.
3) Authorize the account and permissions of the savle server.
Scenario:
master master server: 192.168.1.101
slave server: 192.168.1.102
1. Right to slave database server 192.168.10.131 (master user, only The slave server is open) The
syntax is: GRANT REPLICATION SLAVE ON . to'User Name'@'192.168.0.102 ' identified by'Password';
Mysql> GRANT REPLICATION SLAVE ON *.* to 'lvtest'@'192.168.77.128'identified by 'admin';
Parameter description:
lvtest: account
IDENTIFIED BY'admin ' used by slave to connect to master: password used by slave to connect to master
192.168.77.128 : slave IP
2 Inquire the status of the master database
Mysql> show master status;
±-------------------±---------±-------- ------±-----------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±--- ----------------±---------±--------------±-------- ---------------------+
| mysql-bin.000002 | 226 | | mysql |
±--------------- ----±---------±--------------±-------------------- ---------+
Record the value of File and Position, use it on the slave side
slave configuration
1. Modify the configuration file my.ini of the slave server to change server-id = 1 to server-id = 10, and make sure that this
ID is not used by other MySQL services.
2. Start the slave server and log in.
3. On the slave side, configure the master link information (execution statement)
1) Configure
Mysql> change master to
master_host='192.168.77.1', #master IP
master_user='lvtest', #master database passed GRANT authorized account
master_password='admin', #master database password authorized by GRANT
master_port=3307, #master database password
master_log_file='mysql-bin.000001',
#master database file name displayed by show master status
master_log_pos =296
#master database Position value displayed by show master status
2) (restart) connect
Mysql> start slave;
3)主从同步检查
show slave status;
其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
4. Test
1) On the master, build a database, create tables, and add data.
2) Refresh the slave database, and the records also exist.
From this, the entire MySQL master-slave replication process is completed. Next, we perform MySQL read-write separation Installation and configuration.
Read and write separation-sharding-jdbc
Configuration file
Basic configuration-->application.yml
server:
port: 8018
spring:
application:
name: bj-sharding-jdbc
main:
allow-bean-definition-overriding: true
profiles:
# rw-读写分离配置 table-数据分表+读写分离 dt-分库分表+读写分离
active: dt
mybatis:
mapper-locations: classpath:/top/qrainly/**/dao/**/*.xml
Read-write separation configuration -->application-rw.yml
sharding:
jdbc:
dataSource:
names: db-test0,db-test1
db-test0:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
db-test1:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
config:
# 仅配置读写分离时打开此配置
masterslave:
# 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询
load-balance-algorithm-type: round_robin
name: db1s2
master-data-source-name: db-test0
slave-data-source-names: db-test1
props:
sql:
# 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
show: true
Data table + read-write separation configuration --> application-table.yml
sharding:
jdbc:
dataSource:
names: db-test0,db-test1
db-test0:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
db-test1:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
config:
# 配置数据分表
sharding:
tables:
user:
table-strategy:
standard:
sharding-column: id
precise-algorithm-class-name: top.qrainly.sharding.jdbc.config.MyPreciseShardingAlgorithm
# 读取ds_0数据源的user_0、user_1、user_2、user_3
actual-data-nodes: ds_0.user_$->{
0..3}
master-slave-rules:
ds_0:
master-data-source-name: db-test0
slave-data-source-names: db-test1
props:
sql:
# 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
show: true
Sub-database sub-table + read-write separation configuration --> application-dt.yml
---
sharding:
jdbc:
datasource:
names: ds-master-0,ds-master-1,ds-master-0-slave-0,ds-master-1-slave-0
# 主库0
ds-master-0:
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
# 主库0-从库0
ds-master-0-slave-0:
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
# 主库1
ds-master-1:
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bj_sharding1?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
# 主库1-从库0
ds-master-1-slave-0:
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/bj_sharding1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
config:
sharding:
tables:
user:
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_$->{
id % 5}
key-generator-column-name: id
actual-data-nodes: ds_$->{
0..1}.user_$->{
0..4}
default-database-strategy:
inline:
# 置的分库的字段,本案例是根据id进行分
sharding-column: id
# 置的分库的逻辑,根据id%2进行分
algorithm-expression: ds_$->{
id % 2}
master-slave-rules:
ds_1:
slave-data-source-names: ds-master-1-slave-0
master-data-source-name: ds-master-1
ds_0:
slave-data-source-names: ds-master-0-slave-0
master-data-source-name: ds-master-0
Note: The parameter exclude={DataSourceAutoConfiguration.class} needs to be added to @SpringBootApplication under the sub-database sub-table configuration
ok, switch spring.profiles.active to play in different configuration modes!
Reference article: https://my.oschina.net/qrainly/blog/3072946