Cobar的安装和配置步骤

来源:互联网 发布:linux系统有哪些版本 编辑:程序博客网 时间:2024/04/30 15:13
一:环境准备
1、软件准备
操作系统: Centos6.3
数据库:MySQL5.5
JDK版本:java7
Cobar软件:cobar1.2.7
2、数据准备
MySQL所在服务器IP为192.168.0.1,端口为3306,用户名为test,密码为test。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解决:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('root') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>
mysql>
GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost'  IDENTIFIED BY 'test' WITH GRANT OPTION; 
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'  IDENTIFIED BY 'test' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'test'@'127.0.0.1'  IDENTIFIED BY 'test' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'test'@'192.168.0.1'  IDENTIFIED BY 'test' WITH GRANT OPTION;

我们需要创建schema:dbtest1、dbtest2、dbtest3,table:tb1、tb2,脚本如下:
#创建dbtest1
drop database if exists dbtest1;
create database dbtest1;
use dbtest1;
#在dbtest1上创建tb1
create table tb1(
id    int not null,
gmt   datetime);
 
#创建dbtest2
drop database if exists dbtest2;
create database dbtest2;
use dbtest2;
#在dbtest2上创建tb2
create table tb2(
id    int not null,
val   varchar(256));
 
#创建dbtest3
drop database if exists dbtest3;
create database dbtest3;
use dbtest3;
#在dbtest3上创建tb2
create table tb2(
id    int not null,
val   varchar(256));

二:部署和配置Cobar
请确保机器上设置了JAVA环境变量JAVA_HOME
1、下载Cobar压缩文件并解压,进入conf目录可以看到schema.xml, rule.xml, server.xml等相关的配置文件
wget http://code.alibabatech.com/mvn/releases/com/alibaba/cobar/cobar-server/1.2.7/cobar-server-1.2.7.tar.gz
tar zxf cobar-server-1.2.7.tar.gz
cd cobar-server-1.2.7 

相关说明
a、基本目录
如果您还没有下载Cobar,请先进入Cobar Release下载最新Cobar压缩包。解压后,进入cobar-server-1.2.4目录,可以看到Cobar的主要目录如下:
bin    #包含Cobar的启动、重启、停止等脚本文件
conf   #包含Cobar所有配置文件
lib    #包含Cobar及其依赖的jar文件
logs   #包含Cobar所有日志文件
b、启动脚本
Cobar的所有启动停止脚本全部放在bin目录中,进入bin目录,可以看到:
startup.sh   #Linux环境启动脚本
startup.bat  #Windows环境启动脚本
restart.sh   #Linux环境重启脚本
shutdown.sh  #Linux环境停止脚本
c、配置文件
Cobar的所有配置文件全部放在conf目录中,进入conf目录,可以看到:
server.xml   #Cobar系统、用户、集群等相关配置
schema.xml   #schema,dataNode,dataSource相关配置
rule.xml     #分布式规则定义
log4j.xml    #日志相关配置

schema.xml配置如下(注意:schema.xml包含MySQL的IP、端口、用户名、密码等配置,您需要按照注释替换为您的MySQL信息。)
2、schema.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cobar:schema SYSTEM "schema.dtd">
<cobar:schema xmlns:cobar="http://cobar.alibaba.com/">
 
  <!-- schema定义 -->
  <schema name="dbtest" dataNode="dnTest1">
    <table name="tb2" dataNode="dnTest2,dnTest3" rule="rule1" />
  </schema>
 
  <!-- 数据节点定义,数据节点由数据源和其他一些参数组织而成。-->
  <dataNode name="dnTest1">
    <property name="dataSource">
      <dataSourceRef>dsTest[0]</dataSourceRef>
    </property>
  </dataNode>
  <dataNode name="dnTest2">
    <property name="dataSource">
      <dataSourceRef>dsTest[1]</dataSourceRef>
    </property>
  </dataNode>
  <dataNode name="dnTest3">
    <property name="dataSource">
      <dataSourceRef>dsTest[2]</dataSourceRef>
    </property>
  </dataNode>
 
  <!-- 数据源定义,数据源是一个具体的后端数据连接的表示。-->
  <dataSource name="dsTest" type="mysql">
    <property name="location">
      <location>192.168.0.1:3306/dbtest1</location> <!--注意:替换为您的MySQL IP和Port-->
      <location>192.168.0.1:3306/dbtest2</location> <!--注意:替换为您的MySQL IP和Port-->
      <location>192.168.0.1:3306/dbtest3</location> <!--注意:替换为您的MySQL IP和Port-->
    </property>
    <property name="user">test</property> <!--注意:替换为您的MySQL用户名-->
    <property name="password">test</property> <!--注意:替换为您的MySQL密码-->
    <property name="sqlMode">STRICT_TRANS_TABLES</property>
  </dataSource>
</cobar:schema>

rule.xml配置如下(本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。)
3、rule.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cobar:rule SYSTEM "rule.dtd">
<cobar:rule xmlns:cobar="http://cobar.alibaba.com/">
  <!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法。-->
  <tableRule name="rule1">
    <rule>
      <columns>id</columns>
      <algorithm><![CDATA[ func1(${id})]]></algorithm>
    </rule>
  </tableRule>
 
  <!-- 路由函数定义,应用在路由规则的算法定义中,路由函数可以自定义扩展。-->
  <function name="func1" class="com.alibaba.cobar.route.function.PartitionByLong">
    <property name="partitionCount">2</property>
    <property name="partitionLength">512</property>
  </function>
</cobar:rule>
server.xml配置如下
server.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cobar:server SYSTEM "server.dtd">
<cobar:server xmlns:cobar="http://cobar.alibaba.com/">
 
  <!--定义Cobar用户名,密码-->
  <user name="test">
    <property name="password">test</property>
    <property name="schemas">dbtest</property>
  </user>
</cobar:server>

三:启动和使用Cobar
1、启动Cobar,进入bin目录可以看到Cobar的启动、停止与重启脚本
./startup.sh #Cobar进程名为CobarStartup
2、查看logs目录下stdout.log, 启动成功日志如下
10:54:19,264 INFO  ===============================================
10:54:19,265 INFO  Cobar is ready to startup ...
10:54:19,265 INFO  Startup processors ...
10:54:19,443 INFO  Startup connector ...
10:54:19,446 INFO  Initialize dataNodes ...
10:54:19,470 INFO  dnTest1:0 init success
10:54:19,472 INFO  dnTest3:0 init success
10:54:19,473 INFO  dnTest2:0 init success
10:54:19,481 INFO  CobarManager is started and listening on 9066
10:54:19,483 INFO  CobarServer is started and listening on 8066
10:54:19,484 INFO  ===============================================
3、访问Cobar同访问MySQL的方式完全相同, 常用访问方式如下(注意:本文将Cobar部署在192.168.0.1这台机器上,否则请替换为您的Cobar所在IP,其他信息不变)
#命令行
mysql -h192.168.0.1 -utest -ptest -P8066 -Ddbtest
 
#JDBC(建议5.1以上的mysql driver版本)
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.1:8066/dbtest", "test", "test");
......
4、SQL执行示例,执行语句时与使用传统单一数据库无区别
mysql>show databases;                                        #dbtest1、dbtest2、dbtest3对用户透明
----------
| DATABASE |
----------
| dbtest   |
----------
 
mysql>show tables;                                                   #dbtest中有两张表tb1和tb2
-------------------
| Tables_in_dbtest1 |
-------------------
| tb1               |
| tb2               |
-------------------
 
mysql>insert into tb1 (id, gmt) values (1, now());                   #向表tb1插入一条数据
mysql>insert into tb2 (id, val) values (1, "part1");                 #向表tb2插入一条数据
mysql>insert into tb2 (id, val) values (2, "part1"), (513, "part2"); #向表tb2同时插入多条数据
mysql>select * from tb1;                                             #查询表tb1,验证数据被成功插入
---- ---------------------
| id | gmt                 |
---- ---------------------
|  1 | 2012-06-12 15:00:42 |
---- ---------------------
 
mysql>select * from tb2;                                             #查询tb2,验证数据被成功插入
----- -------
| id  | val   |
----- -------
|   1 | part1 |
|   2 | part1 |
| 513 | part2 |
----- -------
 
mysql>select * from tb2 where id in (1, 513);                        #根据id查询
----- -------
| id  | val   |
----- -------
|   1 | part1 |
| 513 | part2 |
----- -------
5、查看后端MySQL数据库dbtest1,dbtest2和dbtest3,验证数据分布在不同的库中

四、创造错误
1、关闭MYSQL,查看前端查询和stdout.log日志。
2、删除test用户权限,查看前端查询和stdout.log日志。

原创粉丝点击