cobar mysql 高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cobar mysql 高可用相关的知识,希望对你有一定的参考价值。

---------------------------------------------

server2 IP:172.25.38.2

server5 IP:172.25.38.5

---------------------------------------------

1)安装mysql  [server2][server5]

yum install -y mysql-server

rpm -qa|grep mysql##安装好,可见已安装的包

mysql-libs-5.1.71-1.el6.x86_64

mysql-5.1.71-1.el6.x86_64

mysql-server-5.1.71-1.el6.x86_64

[server2] ##编辑mysql配置文件

vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

server-id=2

binlog-do-db=dbtest

binlog-ignore-db=mysql

log-bin=mysql-bin 

master-host=172.25.38.5

master-user=ly

master-password=westos

master-port=3306

replicate-ignore-db=mysql

replicate-do-db=dbtest 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

当配置server2为主时,server5为从时,把以#开头的,把#去掉

[server5]

vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

symbolic-links=0

server-id=5

log-bin=mysql-bin

binlog-do-db=dbtest

binlog-ignore-db=mysql

#master-host=172.25.38.2

#master-user=ly

#master-password=westos

#master-port=3306

#replicate-ignore-db=mysql

#replicate-do-db=dbtest

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

log-error=/var/log/mysqld.log


/etc/init.d/mysqld start##开启数据库

mysqladmin -u root password westos##设置密码

[[email protected] ~]# mysql -p##进入数据库设置

Enter password: 

mysql> create user [email protected]‘172.25.38.5‘ identified by ‘westos‘;##创建用户并设置密码

mysql> grant all on *.* to [email protected]‘172.25.38.5‘;##授予权限

mysql> flush privleges;##刷新

mysql> show master status;##主节点状态

+------------------+----------+--------------+------------------+

| File        |Position |Binlog_Do_DB | Binlog_Ignore_DB|

+------------------+----------+--------------+------------------+

| mysql-bin.000004|   254  | dbtest    |  mysql       |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

[[email protected]  ~]# mysql -p

Enter password:

mysql> change master to master_host=‘172.25.38.2‘,master_user=‘ly‘,master_password=‘westos‘;

mysql> start slave;##开启从节点

mysql> show slave status\G;##查看从的状态

*************************** 1. row ***************************

          Slave_IO_State: Waiting for master to send event

            Master_Host: 172.25.38.5

            Master_User: ly

            Master_Port: 3306

           Connect_Retry: 60

          Master_Log_File: mysql-bin.000004

       Read_Master_Log_Pos: 254

          Relay_Log_File: mysqld-relay-bin.000005

           Relay_Log_Pos: 399

      Relay_Master_Log_File: mysql-bin.000004

         Slave_IO_Running: Yes

        Slave_SQL_Running: Yes

         Replicate_Do_DB: dbtest

       Replicate_Ignore_DB: mysql

##做同样类似操作,最后做出双向主从同步

[[email protected]  ~]# mysql -p

Enter password:

mysql> create user [email protected]‘172.25.38.2‘ identified by ‘westos‘;

mysql> grant all on *.* to [email protected]‘172.25.38.2‘;

mysql> flush privileges;

mysql> show master status;

+------------------+----------+--------------+------------------+

| File        | Position| Binlog_Do_DB| Binlog_Ignore_DB|

+------------------+----------+--------------+------------------+

| mysql-bin.000012|   106  | dbtest    | mysql       |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

[[email protected]  ~]# mysql -p

Enter password:

mysql> change master to master_host=‘172.25.38.2‘,master_user=‘ly‘,master_password=‘westos‘;

mysql> start slave;

mysql> show slave status\G;

*************************** 1. row ***************************

       Slave_IO_State: Waiting for master to send event

         Master_Host: 172.25.38.5

         Master_User: ly

         Master_Port: 3306

        Connect_Retry: 60

      Master_Log_File: mysql-bin.0000012

    Read_Master_Log_Pos: 254

       Relay_Log_File: mysqld-relay-bin.0000013

        Relay_Log_Pos: 106

   Relay_Master_Log_File: mysql-bin.0000012

      Slave_IO_Running: Yes

     Slave_SQL_Running: Yes

       Replicate_Do_DB: dbtest

     Replicate_Ignore_DB: mysql

---------------------------------------------

2)安装heartbeat [server2][server5]

得到如下安装包

heartbeat-3.0.4-2.el6.x86_64.rpm

heartbeat-devel-3.0.4-2.el6.x86_64.rpm

heartbeat-libs-3.0.4-2.el6.x86_64.rpm

ldirectord-3.9.5-3.1.x86_64.rpm

yum install openssl perl-Net-SSLeay perl-TimeDate perl-libwww-perl perl-Compress-Zlib perl-html-Parser perl-HTML-Tagset ipvsadm -y##解决依赖性

rpm -ivh heartbeat-libs-3.0.4-2.el6.x86_64.rpm ldirectord-3.9.5-3.1.x86_64.rpm heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-devel-3.0.4-2.el6.x86_64.rpm

vim /etc/ha.d/ha.cf##编辑配置

 34 logfacility     local0

 48 keepalive 2

 56 deadtime 30

 61 warntime 10

 71 initdead 60

 76 udpport 694##端口

 91 bcast   eth0            # Linux

157 auto_failback on

211 node    server5.example.com

212 node    server2.example.com

222 ping 172.25.38.250##一个同一网段可ping的IP,最好是网关

255 respawn hacluster /usr/lib64/heartbeat/ipfail

261 apiauth ipfail gid=haclient uid=hacluster

vim /etc/ha.d/haresources##资源配置

  server5.example.com IPaddr::172.25.38.100/24/eth0 drbddisk::example Filesystem::/dev/drbd1::/var/lib/mysql::ext4 mysqld

vim /etc/ha.d/authkeys

   auth 1

   1 crc

  #2 sha1 HI!

  #3 md5 Hello!

两台配置一样,直接scp过去就好。            

---------------------------------------------

3)配置drbd

在两台主机上添加硬盘,最好一样大小,方便操作

////////////////////

得到如下包

drbd-8.4.2.tar.gz

或者8个如下安装包

drbd-8.4.2-2.el6.x86_64.rpm

drbd-xen-8.4.2-2.el6.x86_64.rpm

drbd-udev-8.4.2-2.el6.x86_64.rpm

drbd-utils-8.4.2-2.el6.x86_64.rpm

drbd-pacemaker-8.4.2-2.el6.x86_64.rpm

drbd-heartbeat-8.4.2-2.el6.x86_64.rpm

drbd-bash-completion-8.4.2-2.el6.x86_64.rpm

drbd-km-2.6.32_431.el6.x86_64-8.4.2-2.el6.x86_64.rpm

tar包方式进行安装如下:

rpm -ivh drbd-*

yum install gcc flex rpm-build kernel-devel -y

cp /root/drbd-8.4.0.tar.gz /root/rpmbuild/SOURCES/

tar zxf drbd-8.4.0.tar.gz

cd drbd-8.4.0

./configure --enable-spec

./configure --enable-spec --with-km 

在当前目录下得到两个文件

drbd.spec drbd-km.spec

rpmbuild -bb drbd.spec

rpmbuild -bb drbd-km.spec

cd /root/rpmbuild/RPMS/x86_64

rpm -ivh * 

scp /root/rpmbuild/RPMS/x86_64/* 172.25.38.5:/root

并且执行 rpm -ivh drbd-*

vim /etc/drbd.d/example.res

  1 resource example {

  2 meta-disk internal;

  3 device /dev/drbd1;

  4 syncer {

  5 verify-alg sha1;

  6 }

  7 on server5.example.com {

  8 disk /dev/vdb;

  9 address 172.25.38.5:7789;

 10 }

 11 on server2.example.com {

 12 disk /dev/vdb;

 13 address 172.25.38.2:7789;

 14 }

 15 }

drbdadm create-md example

/etc/init.d/drbd start

drbdadm primary example

cat /proc/drbd#在两台主机上查看同步状态

mkfs.ext4 /dev/drbd1#数据同步结束后创建文件系统

mount /dev/drbd1 /var/www/html#挂载文件系统

---------------------------------------------

4)安装jdk

得到

jdk-6u32-linux-x64.bin

sh jdk-6u32-linux-x64.bin

mv jdk-6u32-linux-x64 java

mv java /usr/local

vim /etc/profile

export JAVA_HOME=/usr/local/java

export CLASSPATH=.:$JAVA_HOME/bin:$JAVA_HOME/jre/lib

export PATH=$PATH:$JAVA_HOME/bin

source /etc/profile

cd /home/

测试:

vim test.java

class test {

        public static void main(String[] args)

        {

 

                 System.out.println("Hello world!");

        }

 

}

java test

---------------------------------------------

5) 安装cobar

cobar-server-1.2.6.zip

unzip cobar-server-1.2.6.zip

cd /root/cobar-server-1.2.6/conf

vim schema.xml

  <!-- schema定义 -->

  <schema name="dbtest" >

    <table name="usertb" dataNode="server5,server2"  />

  </schema>

 

 <dataSource name="dbtest" type="mysql">

    <property name="location">

      <location>172.25.38.5:3306/dbtest</location>

      <location>172.25.38.5:3306/dbtest</location>

    </property>

    <property name="user">root</property>

    <property name="password">westos</property>

    <property name="sqlMode">STRICT_TRANS_TABLES</property>

  </dataSource>

 

vim server.xml

  <user name="root">

    <property name="password">westos</property>

    <property name="schemas">dbtest</property>

  </user>

  <!--

  <user name="root">

    <property name="password"></property>

  </user>

  <cluster>

    <node name="cobar1">

      <property name="host">172.25.38.5</property>

      <property name="weight">1</property>

    </node>

  </cluster>

 

  <cluster>

    <node name="cobar2">

      <property name="host">172.25.38.2</property>

      <property name="weight">2</property>

    </node>

  </cluster>

 

vim rule.xml

<!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>

 

cd /root/cobar-server-1.2.6/bin

[[email protected] bin]# ./startup.sh 

"/usr/local/java/bin/java" -Dcobar.home="/root/cobar-server-1.2.6" -classpath "/root/cobar-server-1.2.6/conf:/root/cobar-server-1.2.6/lib/classes:/root/cobar-server-1.2.6/lib/cobar-common-1.2.6.jar:/root/cobar-server-1.2.6/lib/cobar-config-1.2.6.jar:/root/cobar-server-1.2.6/lib/cobar-net-1.2.6.jar:/root/cobar-server-1.2.6/lib/cobar-parser-1.2.6.jar:/root/cobar-server-1.2.6/lib/cobar-route-1.2.6.jar:/root/cobar-server-1.2.6/lib/cobar-server-1.2.6.jar:/root/cobar-server-1.2.6/lib/log4j-1.2.16.jar:/root/cobar-server-1.2.6/lib/slf4j-api-1.6.4.jar:/root/cobar-server-1.2.6/lib/slf4j-log4j12-1.6.4.jar" -server -Xms1024m -Xmx1024m -Xmn256m -Xss128k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+UseFastAccessorMethods -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 com.alibaba.cobar.CobarStartup >> "/root/cobar-server-1.2.6/logs/console.log" 2>&1 &

[[email protected] bin]# /etc/init.d/drbd start

Starting DRBD resources: [

]

.

[[email protected] bin]# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 52:54:00:f6:11:cd brd ff:ff:ff:ff:ff:ff

    inet 172.25.38.2/24 brd 172.25.38.255 scope global eth0

    inet 172.25.38.100/24 brd 172.25.38.255 scope global secondary eth0

    inet6 fe80::5054:ff:fef6:11cd/64 scope link 

       valid_lft forever preferred_lft forever

[[email protected] bin]# df

Filesystem               1K-blocks    Used Available   Use% Mounted on

/dev/mapper/VolGroup-lv_root  15006084     1723528  12520292  13% /

tmpfs                   203008       0   203008   0% /dev/shm

/dev/vda1                495844       33452   436792   8% /boot

/dev/drbd1               4128284       95208  3823372   3% /var/lib/mysql

[[email protected]server2 bin]#./shutdown.sh 

[[email protected] conf]# df

Filesystem               1K-blocks    Used Available  Use% Mounted on

/dev/mapper/VolGroup-lv_root  17069948     1602508 14600320   10%   /

tmpfs                  251136       0   251136    0% /dev/shm

/dev/vda1                495844      33467   436777   8% /boot

/dev/drbd1               4128284      95232  3823348   3% /var/lib/mysql

[[email protected] conf]# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 52:54:00:6e:00:88 brd ff:ff:ff:ff:ff:ff

    inet 172.25.38.5/24 brd 172.25.38.255 scope global eth0

    inet 172.25.38.100/24 brd 172.25.38.255 scope global secondary eth0

    inet6 fe80::5054:ff:fe6e:88/64 scope link 

       valid_lft forever preferred_lft forever

 

-------------------------------------


 




本文出自 “12098166” 博客,请务必保留此出处http://12108166.blog.51cto.com/12098166/1912264

以上是关于cobar mysql 高可用的主要内容,如果未能解决你的问题,请参考以下文章

MySQL-高可用架构MHA

MySQL-高可用架构MHA

MySQL高可用群集

MySQL高可用群集--MMM高可用架构

mysql集群高可用架构

[转]MYSQL高可用方案探究(总结)