Use a C-JDBC to give a MySQL Cluster

Source: Internet
Author: User

I. Preface
Cjdbc (http://c-jdbc.objectweb.org/) is an open source database cluster middleware, any JDBC-based applications can access the database cluster transparently through it, it can replicate data between nodes, in addition, query load balancing can be implemented for each node. With such software, we can easily implement raidb-Redundant Array of inexpensive database Redundant Arrays for low-cost databases.
As the number of users accessing large applications increases, increasing database storage and database redundancy can increase system reliability and performance.

The following uses cjdbc to perform raidb for two peer-to-peer MySQL instances,This article assumes that you have set up two peer-to-peer MySQL environments and created a database clusterdb that requires redundant cluster.

2. Configure the environment
MYSQL: 5.0.19, use InnoDB as the MySQL engine

C-jdbc: 2.0.2

JDK: 1.5

3. select an appropriate C-JDBC raid B mechanism

cjdbc has several raid B mechanisms to choose from, such as RAIDb-0, RAIDb-1, etc., you can choose a different raidb mechanism according to different situations. For details about various raid B mechanisms, refer to the cjdbc documentation and demo.

RAIDb-1 has the following functions:
full image processing mechanism, each node has a complete database structure, This method provides the best fault tolerance Processing, by setting a reasonable loading balance policy, you can improve query performance. However, since any write operation (Create/update/delete) requires propagation replication on each node, the write operation will be slower than the original one, for example:

here we choose RAIDb-1 as the cjdbc raidb mechanism.

4. Create a table for two peer-to-peer MySQL instances. Assume that the IP addresses of the two MySQL instances are 192.168.0.2 and 192.168.0.3 respectively.
bash> mysql-h192.168.0.2-uroot
bash> Use clusterdb
bash> Create Table user (ID int (3) not null auto_increment primary key, name char (50) not null) engine InnoDB ;< br> bash> exit;

bash> mysql-h192.168.0.3-uroot
bash> Use clusterdb
bash> Create Table user (ID int (3) not null auto_increment primary key, name char (50) not null) engine InnoDB ;
bash> exit;

5. Install C-JDBC controller in Linux
bash> mkdir-P/usr/local/C-jdbc
bash> Cd/usr/local/C-jdbc
bash> tar xvfz c-jdbc-2.0.2-bin.tar.gz
bash> export cjdbc_home =/usr/local/C-jdbc
6. Put the MySQL JDBC driver in the C-JDBC controller.

Here we use mysql-connector-java-3.1.12-bin.jar driverProgramPut it in
/Usr/local/C-jdbc/Drivers

7. Configure C-JDBC Controller
1. Create a virtual database configuration file in the/usr/local/C-jdbc/config/virtualdatabase directory and name it a mysql-raidb1-distribution.xml with the following content:

<? XML version = "1.0" encoding = "utf8"?>
<! Doctype C-JDBC public "-// objectweb // DTD C-JDBC 2.0.2 // en" "http://c-jdbc.objectweb.org/dtds/c-jdbc-2.0.2.dtd">

<C-JDBC>

<Virtualdatabase name = "mydb">

<Distribution>
</Distribution>

<Authenticationmanager>
<Admin>
<User Username = "admin" Password = "C-jdbc"/>
</Admin>
<Virtualusers>
<Virtuallogin vlogin = "boss" vpassword = "boss"/>
</Virtualusers>
</Authenticationmanager>

<Databasebackend name = "mysqlnode211" driver = "org. gjt. Mm. MySQL. Driver" url = "JDBC: mysql: // 192.168.0.2/clusterdb" connectionteststatement = "select 1">
<Connectionmanager vlogin = "boss" rlogin = "boss_user" rpassword = "123456">
<Variablepoolconnectionmanager initpoolsize = "10" minpoolsize = "10" maxpoolsize = "50" idletimeout = "30" waittimeout = "10"/>
</Connectionmanager>
</Databasebackend>

<Databasebackend name = "mysqlnode213" driver = "org. gjt. Mm. MySQL. Driver" url = "JDBC: mysql: // 192.168.0.3/clusterdb" connectionteststatement = "select 1">
<Connectionmanager vlogin = "boss" rlogin = "boss_user" rpassword = "123456">
<Variablepoolconnectionmanager initpoolsize = "10" minpoolsize = "10" maxpoolsize = "50" idletimeout = "30" waittimeout = "10"/>
</Connectionmanager>
</Databasebackend>

<Requestmanager>
<Requestscheduler>
RAIDb-1Scheduler level = "passthrough"/>
</Requestscheduler>

<Loadbalancer>
<RAIDb-1>
<Waitforcompletion policy = "first"/>
<RAIDb-1-LeastPendingRequestsFirst/>
RAIDb-1>
</Loadbalancer>
</Requestmanager>

</Virtualdatabase>

C-JDBC>

2. Create the C-JDBC controller configuration file in the/usr/local/C-jdbc/config/controller directory, and name it uud-controller-distributed.xml, the content is as follows:










3. Create the startup C-JDBC controller SH in the/usr/local/C-jdbc/config/demo directory and name it a uud-distributed-raidb1-controller.sh with the following content:

#! /Bin/sh

Export cjdbc_home =/usr/local/C-JDBC
Export java_home =/opt/jdk1.5

CD $ cjdbc_home/bin

Echo "waiting for MySQL servers to finish start up"

Echo "Starting Controller"
./Controller. Sh-f ../config/controller/uud-controller-distributed.xml &

8. Start C-JDBC Controller
Bash> Cd/usr/local/C-jdbc/demo
Bash> chmod U + rwx uud-distributed-raidb1-controller.sh
Bash>./uud-distributed-raidb1-controller.sh &

If the startup is normal, the following information is displayed:

Waiting for MySQL servers to finish start up
Starting Controller
10:32:21, 126 info controller. Core. Controller C-JDBC controller (2.0.2)
10:32:21, 189 info controller. Core. Controller loading configuration file: ../config/controller/uud-controller-distributed.xml
2006-04-20 10:32:21, 278 info controller. Core. Controller JMX is enabled
2006-04-20 10:32:21, 308 info controller. Core. Controller starting JMX server on Host: 127.0.0.1
2006-04-20 10:32:21, 674 info backend. databasebackend. mysqlnode211 adding Connection Manager for virtual user "boss"
2006-04-20 10:32:21, 749 info backend. databasebackend. mysqlnode213 adding Connection Manager for virtual user "boss"
2006-04-20 10:32:21, 809 info controller. requestmanager. mydb request manager will parse requests with the following granularity: no_parsing
10:32:21, 814 info controller. virtualdatabase. mydb grouping ing jgroups using: file:/usr/local/C-jdbc/config/jgroups. xml

-------------------------------------------------------
GMS: Address is 127.0.0.1: 32773
-------------------------------------------------------
10:32:26, 476 info controller. virtualdatabase. mydb group mydb connected to/127.0.0.1: 32773 [/127.0.0.1: 32773]
2006-04-20 10:32:26, 476 info controller. virtualdatabase. mydb first controller in group mydb
10:32:26, 477 warn controller. virtualdatabase. mydb no recovery log has been configured, enabling backend without checkpoint.

[1] + done./uud-distributed-raidb1-controller.sh

8. Compile the C-JDBC client program
1. Put the C-JDBC drivers (/usr/local/C-jdbc/Drivers/c-jdbc-driver.jar) into classpath
2. Write and insert 10 data entries to MySQL. The program is as follows:

/**
* @ Author Hu Ronghua
* @ Company Century dragon 21cn
*/
Package com. cjdbc. test;

Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. drivermanager;

/**
*
*/
Public class generatesampledata {

Public void generate () {
connection conn = NULL;
preparedstatement pstmt = NULL;
try {
// This is the drivers class of C-JDBC drivers, note that it is not mysql drivers class
class. forname ("org. objectweb. cjdb C. driver. driver "). newinstance ();
// 192.168.0.1 is the IP address of the cjdbc controller
// mydb is defined in the file mysql-raidb1-distribution.xml
// user = boss & Password = boss is defined in the file mysql-raidb1-distribution.xml
//
//
//
string url = "JDBC: cjdbc: // 192.168.0.1: 25323/mydb? User = boss & Password = boss ";

Conn = drivermanager. getconnection (URL );

Try {
Conn. setautocommit (false );
Pstmt = conn. preparestatement ("insert into user values ('',?) ");

Int numoftestrecords = 10;
System. Out. println ("Update record start .");
For (INT I = 0; I <numoftestrecords; I ++ ){
String newkey = I + "-" + I;
Pstmt. setstring (1, "Hua _" + newkey );
Pstmt.exe cuteupdate ();

}
Conn. Commit ();
System. Out. println ("Update record success .");
}
Catch (exception ex ){
Conn. rollback ();
Ex. printstacktrace ();
}
Finally {
Try {
If (pstmt! = NULL)
Pstmt. Close ();
If (Conn! = NULL)
Conn. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Generatesampledata G = new generatesampledata ();
G. Generate ();

}

}

3, after the program execution, respectively to MySQL node 192.168.0.2 and 192.168.0.3 query, to see if the data has been synchronized, if the two MySQL node has the same data, it means that the C-JDBC environment is successfully set up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.