安装kingshard
1. 安装Go语言环境(请使用最新版),具体步骤请Google。
2. git clone https://github.com/flike/kingshard.git $GOPATH/src/github.com/flike/kingshard
3. cd src/github.com/flike/kingshard
4. source ./dev.sh
5. make
6. 设置配置文件
7. 运行kingshard。./bin/kingshard -config=etc/ks.yaml
修改kingshard/etc/unshard.yaml配置,修改master和slave和log_path
# node is an agenda for real remote mysql server.
nodes :
-
name : node1
# default max conns for mysql server
max_conns_limit : 32
# all mysql in a node must have the same user and password
user : root
password : kingshard
# master represents a real mysql master server
master : 127.0.0.1:3307 # 主
# slave represents a real mysql salve server,and the number after '@' is
# read load weight of this slave.
slave : 127.0.0.1:3308 # 主
#down_after_noalive : 32
运行kingshard
mysql -h127.0.0.1 -ukingshard -pkingshard -P9696;
执行sql,查看log,已实现读写分离
➜ log git:(master) ✗ tail -f sql.log
2019/03/04 17:50:42 - OK - 26.2ms - 127.0.0.1:56193->127.0.0.1:3308:show databases
2019/03/04 17:50:42 - OK - 5.5ms - 127.0.0.1:56193->127.0.0.1:3308:show tables
2019/03/04 17:51:03 - OK - 3.9ms - 127.0.0.1:56193->127.0.0.1:3308:select * from user
2019/03/04 17:52:08 - OK - 6.7ms - 127.0.0.1:56193->127.0.0.1:3307:insert into user(id,age,name) values(2,10,"zhangsan")
2019/03/04 17:52:47 - OK - 4.7ms - 127.0.0.1:56193->127.0.0.1:3308:select * from user where id = 2
2019/03/04 17:59:27 - OK - 6.4ms - 127.0.0.1:56193->127.0.0.1:3307:update user set age = 18 where id = 2