准备3个ubuntu虚拟机(此处用vagrant)
vagrant init envimation/ubuntu-xenial-docker
修改Vagrantfile
配置网络模式和Ip(此处是10-12)
config.vm.network "public_network", ip:"192.168.1.10"
配置内存大小,此处2G
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
在10中
下载java8 jdk
sudo mkdir /opt/java
sudo chown vagrant /opt/java
tar zxf jdk-8u191-linux-x64.tar.gz
sudo cp -r ~/jdk1.8.0_191/* /opt/java
echo 'export JAVA_HOME=/opt/java/;export PATH=${JAVA_HOME}/bin:${PATH}' >> ~/.bashrc
source ~/.bashrc
java -version
wget https://github.com/apache/incubator-shardingsphere-doc/raw/master/dist/sharding-proxy-3.0.0.M1.tar.gz
tar zxf sharding-proxy-3.0.0.M1.tar.gz
cd sharding-proxy-3.0.0.M1
编辑conf/config.yaml
dataSources:
ds_0:
url: jdbc:mysql://192.168.1.11:3306/test
username: root
password: 密码
ds_1:
url: jdbc:mysql://192.168.1.12:3306/test
username: root
password: 密码
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order${order_id % 2}
keyGeneratorColumnName: order_id
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
props:
sql.show: true
在11和12中
sudo apt-get update
sudo apt-get install mysql-server
mysql -uroot -p密码
create user root identified by '密码'
grant all privileges on *.* to 'root'@‘%‘;
flush privileges
create database test;
create table t_order0(order_id bigint not null auto_increment primary key, user_id bigint not null default 0, name varchar(100) not null default "");
create table t_order1(order_id bigint not null auto_increment primary key, user_id bigint not null default 0, name varchar(100) not null default "");
编辑/etc/mysql/mysql.conf.d/mysqld.cnf
bind = 0.0.0.0
重启
sudo service mysql restart
在10 中
./bin/start.sh
验证
mysql -uroot -p密码 -P3307
insert into t_order(order_id,user_id,name) values(1,1,"test");
按照分库分表规则,应该结果在12的t_order1表,进入验证