Mysql数据库学习(八)数据分片概述 、 部署MyCAT服务 、 测试配置

数据库系统学习

链接:https://pan.baidu.com/s/1gllp16VvxQ7ruTTNbtAqqQ
提取码:tty6

使用mycat软件提供的服务 实现对数据 的 分布式存储
环境准备
重新搭建3台数据库服务器。
检查3台服务器的防火墙服务 和selinux 是否关闭

把56主机 做mycat 服务 (如果有mysqld服务 停止服务并设置为开机不运行 检查停止防火墙服务 和selinux 是否关闭)

50做客户端 只要有mysql命令即可  检查防火墙服务 和selinux 是否关闭

把软件Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 拷贝给56主机

一、相关概念:
什么是分布式存储:把数据存储在不同地点的服务器里,或把数据存储在
同一地点的不同服务器里。

二、配置分片服务
2.1 配置分片服务器 192.168.4.56
1 安装mycat软件
yum -y install java-1.8.0-openjdk.x86_64
[root@mycat56 ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mycat56 ~]# mv mycat/ /usr/local/
[root@mycat56 ~]# ls /usr/local/mycat/
bin catlet conf lib logs version.txt
[root@mycat56 ~]#

	2 安装文件介绍

[root@mycat56 ~]# /usr/local/mycat/bin/mycat --help
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
[root@mycat56 ~]#

[root@mycat56 ~]# ls /usr/local/mycat/conf/.xml
[root@mycat56 ~]# ls /usr/local/mycat/conf/
.properties
[root@mycat56 ~]# ls /usr/local/mycat/conf/.txt
[root@mycat56 ~]# ls /usr/local/mycat/lib/
.jar
[root@mycat56 ~]# ls /usr/local/mycat/logs/

	3 修改配置文件

xml 扩展标记语言 注释
1 server.xml (默认即可) //设置连接账号及逻辑库
2 schema.xml 定义分布式存储数据的表( 根据存储数据的需要修改)

[root@mycat56 ~]# wc -l /usr/local/mycat/conf/schema.xml
77 /usr/local/mycat/conf/schema.xml

[root@mycat56 ~]# sed -i ‘56,77d’ /usr/local/mycat/conf/schema.xml
(删除的是读写分离的配置代码,mycat也可以做读写分离,这里不需要就删除掉)
[root@mycat56 ~]# wc -l /usr/local/mycat/conf/schema.xml
55 /usr/local/mycat/conf/schema.xml
[root@mycat56 ~]#

schema.xml 文件的格式

<?xml version="1.0"?>

<mycat:schema xmlns:mycat="http://io.mycat/"> #定义拆分存储数据的表 <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> #定义表名 <table ..... /> 单标签 <table .....> 双标签 </table> </schema> #定义数据库服务器的主机名 <dataNode ...... /> #定义数据库服务器的ip地址 <dataHost ......> </dataHost> </mycat:schema>

例子: vim schema.xml

`<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

<table name="employee" primaryKey="ID" dataNode="dn1,dn2,dn3"  rule="sharding-by-intfile" />
                            
<table name="customer" primaryKey="ID" dataNode="dn1,dn2,dn3"  rule="sharding-by-intfile">

(dn1就是第一台节点,以此类推)

    <dataNode name="dn1" dataHost="mysqla" database="db1" />
    <dataNode name="dn2" dataHost="mysqlb" database="db2" />
    <dataNode name="dn3" dataHost="mysqlc" database="db3" />

(mysqla仅是配置文件里数据库的名字和真实结点的名字没有关系可以自己随意取,db1是第一台数据库节点的库名,其他的以此类推)

  <dataHost name="mysqla" maxCon="1000" minCon="10" balance="0"
                      writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">

      <heartbeat>select user()</heartbeat>
      <writeHost host="hostM1" url="192.168.4.53:3306" user="pljadmin" password="123qqq...A">
      </writeHost>

(hostM1是指第一台节点,后面包括ip 用户名及密码)
< /dataHost>
< dataHost name=“mysqlb” maxCon=“1000” minCon=“10” balance=“0”
writeType=“0” dbType=“mysql” dbDriver=“native” switchType=“1” slaveThreshold=“100”>
< heartbeat>select user()
< /heartbeat>
< writeHost host=“hostM2” url=“192.168.4.54:3306” user=“pljadmin” password=“123qqq…A”>
< /writeHost>
< /dataHost>

  <dataHost name="mysqlc" maxCon="1000" minCon="10" balance="0"
                      writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
      <heartbeat>select user()</heartbeat>
      <writeHost host="hostM3" url="192.168.4.55:3306" user="pljadmin" password="123qqq...A">
      </writeHost>
   </dataHost>

3 rule.xml 表使用的分片规则(有10种) (默认即可)
2.2 配置数据库服务器 192.168.4.53 、 192.168.4.54 、192.168.4.55

1 创建存储数据的库 (库名要根据schema.xml创建)
[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘create database db1’
[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘create database db2’
[root@host55 ~]# mysql -uroot -p123qqq…A -e ‘create database db3’

	2 用户授权 pljadmin

[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘grant all on . to pljadmin@"%" identified by “123qqq…A”’
[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘grant all on . to pljadmin@"%" identified by “123qqq…A”’
[root@host55 ~]# mysql -uroot -p123qqq…A -e ‘grant all on . to pljadmin@"%" identified by “123qqq…A”’

三、启动服务
3.1 检查所有主机的防火墙服务是否关闭
3.2 测试数据库服务的授权用户
[root@mycat56 ~]# mysql -h192.168.4.53 -upljadmin -p123qqq…A
[root@mycat56 ~]# mysql -h192.168.4.54 -upljadmin -p123qqq…A
[root@mycat56 ~]# mysql -h192.168.4.55 -upljadmin -p123qqq…A

     3.3 启动服务

[root@mycat56 ~]# ss -utnlp | grep 8066

[root@mycat56 ~]# ls /usr/local/mycat/logs/
[root@mycat56 ~]#
[root@mycat56 ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server…
[root@mycat56 ~]#

[root@mycat56 ~]# ls /usr/local/mycat/logs/
mycat.log mycat.pid wrapper.log
[root@mycat56 ~]#

[root@mycat56 ~]# /usr/local/mycat/bin/mycat status
Mycat-server is running (24344).
[root@mycat56 ~]# cat /usr/local/mycat/logs/mycat.pid
24344
[root@mycat56 ~]#

 3.4 查看服务状态
	[root@mycat56 ~]#ss -utnlp  | grep 8066  #查看端口号

            [root@mycat56 ~]# ps -C java    #查看进程

PID TTY TIME CMD
24358 ? 00:00:09 java
[root@mycat56 ~]#

四、统一排错
查看日志目录下日志文件wrapper.log 的报错信息,根据报错 排错,重启mycat服务

日志报错看不懂的同学,按照如下步骤排错:
	把56 主机的内存调到1.5G
            echo  mycat56 > /etc/hosts
            检查schema.xml的配置
	检查数据库服务器的配置
            重启56主机的操作系统
            再次执行启动mycat服务的命令

五、分片规则的使用 (水平分割 分片规则)
5.1 枚举分片规则sharding-by-intfile
(给字段赋值时,值只能在规定的范围内选择)

  5.2 求模分片规则 mod-long
               (把字段值与指定的数字做取余计算 根据计算结果存储数据)

     ****要想实现数据的分布式存储,表中必须创建 分片规则要求的字段名

 查看分片规则要求创建的字段名
 
 vim rule.xml  

        查看了枚举分片表中要创建的字段名

26 < tableRule name=“sharding-by-intfile”> 分片规则名
27 < rule>
28 < columns>sharding_id</ columns> 字段名
29 < algorithm>hash-int< /algorithm> 算法
30 < /rule>
31 </ tableRule>

97 <function name=“hash-int” 算法名
98 class=“io.mycat.route.function.PartitionByFileMap”>
99 < property name=“mapFile”>partition-hash-int.txt</ property> 分片规则对应的配置文件
100 < /function>

        ls  /usr/local/mycat/conf/partition-hash-int.txt 
        vim  /usr/local/mycat/conf/partition-hash-int.txt (定义字段的值列表)

            10000=0  
10010=1
10020=2
        :wq

(字段值如果是写10000 根据配置文件里的0可以判断这行数据会分配到第一台节点,1是第二台,2是第三胎)

  查看求模分片规则表看要创建的字段名

38 < tableRule name=“mod-long”> 分片规则名
39 < rule>
40 < columns>id< /columns> 字段名
41 < algorithm>mod-long< /algorithm> 算法
42 < /rule>
43 < /tableRule>

105 < function name=“mod-long” class=“io.mycat.route.function.PartitionByMod”>
106 < !-- how many data nodes -->
107 < property name=“count”>3< /property> 指定与数字3做取余运算
108 < /function>

[root@mycat56 ~]# /usr/local/mycat/bin/mycat restart

设置源码服务开机运行
[root@mycat56 ~]#echo “/usr/local/mycat/bin/mycat start” >> /etc/rc.local
[root@mycat56 ~]#chmod +x /etc/rc.local

注意: 表中必须要创建使用的分片规则要求的字段名

查看使用枚举分片规则的表名
vim schema.xml

创建表
[root@host50 ~]# mysql -h192.168.4.56 -P8066 -uroot -p123456

show databases;
use TESTDB;
show tables;

mysql> create table employee( ID int primary key auto_increment , sharding_id int , name char(10) , sex enum(“boy”,“girl”) );

desc employee;

在数据库服务器本机查看表结构
[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘desc db1.employee’
[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘desc db2.employee’
[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘desc db3.employee’

存储数据 (向表里添加多行行时,能不能把多行分别存储在不同数据库服务器里)
[root@host50 ~]# mysql -h192.168.4.56 -P8066 -uroot -p123456

show databases;
use TESTDB;

mysql> insert into employee(sharding_id,name,sex) values (10010,“a”,“boy”),(10010,“a”,“boy”),(10010,“b”,“girl”);
Query OK, 3 rows affected (0.17 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> insert into employee(sharding_id,name,sex) values (10020,“a”,“boy”),(10020,“a”,“boy”),(10020,“b”,“girl”);
Query OK, 3 rows affected (0.46 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> insert into employee(sharding_id,name,sex) values (10030,“a”,“boy”);
ERROR 1064 (HY000): can’t find any valid datanode :EMPLOYEE -> SHARDING_ID -> 10030
mysql>

mysql> select * from employee;
±—±------------±-----±-----+
| ID | sharding_id | name | sex |
±—±------------±-----±-----+
| 1 | 10020 | a | boy |
| 2 | 10020 | a | boy |
| 3 | 10020 | b | girl |
| 1 | 10000 | a | boy |
| 2 | 10000 | a | boy |
| 3 | 10000 | b | girl |
| 1 | 10010 | a | boy |
| 2 | 10010 | a | boy |
| 3 | 10010 | b | girl |
±—±------------±-----±-----+
9 rows in set (0.09 sec)

mysql>

 查看是拆分存储

[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘select * from db1.employee’
mysql: [Warning] Using a password on the command line interface can be insecure.
±—±------------±-----±-----+
| ID | sharding_id | name | sex |
±—±------------±-----±-----+
| 1 | 10000 | a | boy |
| 2 | 10000 | a | boy |
| 3 | 10000 | b | girl |
±—±------------±-----±-----+
[root@host53 ~]#

[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘select * from db1.employee’
mysql: [Warning] Using a password on the command line interface can be insecure.
±—±------------±-----±-----+
| ID | sharding_id | name | sex |
±—±------------±-----±-----+
| 1 | 10000 | a | boy |
| 2 | 10000 | a | boy |
| 3 | 10000 | b | girl |
±—±------------±-----±-----+
[root@host53 ~]#

[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘select * from db1.employee’
mysql: [Warning] Using a password on the command line interface can be insecure.
±—±------------±-----±-----+
| ID | sharding_id | name | sex |
±—±------------±-----±-----+
| 1 | 10000 | a | boy |
| 2 | 10000 | a | boy |
| 3 | 10000 | b | girl |
±—±------------±-----±-----+
[root@host53 ~]#

查看使用求模分片规则的表名
]# vim schema.xml
table name=“hotnews” dataNode=“dn1,dn2,dn3” rule=“mod-long” />
:wq

[root@mycat56 ~]# /usr/local/mycat/bin/mycat restart

创建表
[root@host50 ~]# mysql -h192.168.4.56 -P8066 -uroot -p123456

use TESTDB;
create table hotnews(id int , title char(30) , woker char(10) , comment char(100));

desc hotnews;

在3台数据库服务器查看表结构
[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘desc db1.hotnews’
[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘desc db2.hotnews’
[root@host55 ~]# mysql -uroot -p123qqq…A -e ‘desc db3.hotnews’

存储数据(验证是否实现数据的拆分存储)

[root@host50 ~]# mysql -h192.168.4.56 -P8066 -uroot -p123456

use TESTDB;

mysql> insert into hotnews values(7,“a”,“a”,“aaa”);
ERROR 1064 (HY000): partition table, insert must provide ColumnList
mysql>
mysql> insert into hotnews(id,title,woker,comment) values(7,“a”,“a”,“aaa”);
Query OK, 1 row affected (0.07 sec)

mysql> insert into hotnews(id,title,woker,comment) values(11,“a”,“a”,“aaa”);
Query OK, 1 row affected (0.06 sec)

mysql> insert into hotnews(id,title,woker,comment) values(9,“a”,“a”,“aaa”);
Query OK, 1 row affected (0.01 sec)

mysql> select * from hotnews;
±-----±------±------±--------+
| id | title | woker | comment |
±-----±------±------±--------+
| 7 | a | a | aaa |
| 11 | a | a | aaa |
| 9 | a | a | aaa |
±-----±------±------±--------+
3 rows in set (0.91 sec)

mysql>

在3台数据库服务本机查看数据

[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘select * from db1.hotnews’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±------±--------+
| id | title | woker | comment |
±-----±------±------±--------+
| 9 | a | a | aaa |
±-----±------±------±--------+
[root@host53 ~]#

[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘select * from db2.hotnews’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±------±--------+
| id | title | woker | comment |
±-----±------±------±--------+
| 7 | a | a | aaa |
±-----±------±------±--------+
[root@host54 ~]#

[root@host55 ~]# mysql -uroot -p123qqq…A -e ‘select * from db3.hotnews’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±------±--------+
| id | title | woker | comment |
±-----±------±------±--------+
| 11 | a | a | aaa |
±-----±------±------±--------+
[root@host55 ~]#

#################################3
六、添加新库新表,在56主机做如下配置
6.1 添加新库
vim server.xml
< user name=“root”>
< property name=“password”>123456< /property>
< property name=“schemas”>TESTDB,GAMEDB< /property>
< /user>
< user name=“user”>
< property name=“password”>user< /property>
< property name=“schemas”>TESTDB,GAMEDB< /property>
< property name=“readOnly”>true< /property>
< /user>

:wq
6.2 添加新表
vim schema.xml
< ?xml version=“1.0”?>
< !DOCTYPE mycat:schema SYSTEM “schema.dtd”>
< mycat:schema xmlns:mycat=“http://io.mycat/”>

  <schema name="GAMEDB" checkSQLschema="false" sqlMaxLimit="100">
	<table name="user" rule="mod-long" dataNode="dn1,dn2,dn3" />
            <table name="employee2" dataNode="dn1,dn2,dn3" type="global" />
  </schema>
            

6.3 重启mycat服务

[root@mycat56 ~]# /usr/local/mycat/bin/mycat stop
[root@mycat56 ~]# /usr/local/mycat/bin/mycat start

6.4 测试配置

[root@host50 ~]# mysql -h192.168.4.56 -P8066 -uroot -p123456
mysql> show databases;
±---------+
| DATABASE |
±---------+
| GAMEDB |
| TESTDB |
±---------+
2 rows in set (0.01 sec)

mysql>
mysql> use GAMEDB;
mysql> show tables;
±-----------------+
| Tables in GAMEDB |
±-----------------+
| employee2 |
| user |
±-----------------+
2 rows in set (0.00 sec)

mysql>

mysql> create table employee2(name char(10) , age int);
Query OK, 0 rows affected (0.70 sec)

mysql> insert into employee2(name,age)values(“aa”,12);
Query OK, 1 row affected (0.33 sec)

mysql> insert into employee2(name,age)values(“aa”,12);
Query OK, 1 row affected (0.20 sec)

mysql> insert into employee2(name,age)values(“bb”,12);
Query OK, 1 row affected (0.13 sec)

mysql>
mysql> select * from employee2;
±-----±-----+
| name | age |
±-----±-----+
| aa | 12 |
| aa | 12 |
| bb | 12 |
±-----±-----+
3 rows in set (0.23 sec)

mysql>

mysql> insert into employee2(name,age)values(“aa”,12);
mysql> create table user(id int , class char(7) , name char(5));
Query OK, 0 rows affected (0.60 sec)

mysql> insert into user(id , class , name ) values(8,“aa”,“aa”),(9,“bb”,“bb”),(10,“cc”,“cc”);
Query OK, 3 rows affected (0.13 sec)

mysql> select * from user;
±-----±------±-----+
| id | class | name |
±-----±------±-----+
| 9 | bb | bb |
| 10 | cc | cc |
| 8 | aa | aa |
±-----±------±-----+
3 rows in set (0.09 sec)

mysql>

 在数据库服务器本机查看数据

[root@host53 ~]# mysql -uroot -p123qqq…A -e 'select * from db1.user’mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±-----+
| id | class | name |
±-----±------±-----+
| 9 | bb | bb |
±-----±------±-----+
[root@host53 ~]#

[root@host54 ~]# mysql -uroot -p123qqq…A -e 'select * from db2.user’mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±-----+
| id | class | name |
±-----±------±-----+
| 10 | cc | cc |
±-----±------±-----+
[root@host54 ~]#

[root@host55 ~]# mysql -uroot -p123qqq…A -e 'select * from db3.user’mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±------±-----+
| id | class | name |
±-----±------±-----+
| 8 | aa | aa |
±-----±------±-----+
[root@host55 ~]#

[root@host55 ~]# mysql -uroot -p123qqq…A -e ‘select * from db3.employee2’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±-----+
| name | age |
±-----±-----+
| aa | 12 |
| aa | 12 |
| bb | 12 |
±-----±-----+
[root@host55 ~]#

[root@host54 ~]# mysql -uroot -p123qqq…A -e ‘select * from db2.employee2’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±-----+
| name | age |
±-----±-----+
| aa | 12 |
| aa | 12 |
| bb | 12 |
±-----±-----+
[root@host54 ~]#
[root@host53 ~]# mysql -uroot -p123qqq…A -e ‘select * from db1.employee2’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±-----+
| name | age |
±-----±-----+
| aa | 12 |
| aa | 12 |
| bb | 12 |
±-----±-----+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值