数据库的整体架构

架构说明:

1, 数据库之间做主从复制,实现双主多备

2, 在数据库之间利用mysql-mmm套件实现HA,并用monitor进行监听

3, 利用mysql-proxy进行读写分离

4, 利用keepalived实现mysql-proxy的高可用

优势:

1, 数据库得到高可用的保护,当主库其中一个节点宕机,备用的主库进行切换,保证应用的正常进行。

2, 利用mysql-proxy进行读写分离,可以减轻主库的负载,并且能对从库进行负载均衡。

3, mysql-proxy采用keepalived的方法,实现对应用层的透明,并且保证mysql-proxy的高可用性。

4, 利用mysql-proxy可为日后的数据库改造,切分提供了有利的条件。

软件:

Mysql数据库   mysql-5.0.87.tar.gz  

Mysql-mmm套件(做数据库的高可用) mysql-mmm-2.2.1.tar.gz

Keepalived套件(做mysqlproxy代理的高可用)

Mysql-proxy软件(实现读写分离)

IP与服务器hostname分配

192.168.21.5 monitor

192.168.21.11 master1

192.168.21.12 master2

192.168.21.13 slave1

192.168.21.14 slave2

192.168.21.15 mysqlproxy1

192.168.21.16 mysqlproxy2

数据库软件的安装:

#移动数据库软件包到/usr/src目录下

[root@test ~]# mv mysql-5.0.87.tar.gz /usr/src/

#解压数据库软件包

[root@test src]# tar –xzvf  mysql-5.0.87.tar.gz

#建立目录

[root@test src]# mkdir /var/run/mysqld

#建立日志目录

[root@test src]# mkdir /var/log/mysqld

#添加mysql

[root@test src]# groupadd mysql

#添加mysql用户

[root@test src]# useradd g mysql mysql

[root@test src]# cd /usr/src/mysql-5.0.87

#配置mysql

--prefix=/usr/local/mysql  #安装目录

--with-charset=gb2312 #设置字符集

--with-extra-charsets=gb2312,utf8,gbk #设置额外支持字符集

--with-server-suffix="-yzmyt" #添加数据库名称的后缀

--with-pthread 支持多线程

--with-unix-socket-path=/var/run/mysqld/mysql5.socket #设置unix socket路径

--with-tcp-port=3306 #设置通信端口

--with-mysqld-user=mysql #设置mysql用户

--with-big-tables #设置支持大表

--with-debug #设置为debug状态

--with-***-storage-engine #设置示例支持的存储引擎

[root@test mysql-5.0.87]# ./configure --prefix=/usr/local/mysql --enable-local-infile --with-charset=gb2312 --with-extra-charsets=gb2312,utf8,gbk --with-server-suffix="-yzmyt" --with-pthread --with-unix-socket-path=/var/run/mysqld/mysql5.socket --with-tcp-port=3306 --with-mysqld-user=mysql --with-zlib-dir=/usr --with-libwrap=/usr --with-low-memory --with-mysqlmanager --with-openssl --with-big-tables --with-debug  --with-example-storage-engine  --with-archive-storage-engine  --with-csv-storage-engine  --with-blackhole-storage-engine  --with-ndbcluster  --with-ndb-docs  --with-ndb-test  --with-federated-storage-engine

#编译

[root@test mysql-5.0.87]# make

#安装

[root@test mysql-5.0.87]# make install

#创建my.cnf所在的文件文件目录

[root@test mysql-5.0.87]# mkdir /usr/local/mysql/etc

#创建my.cnf

[root@test mysql-5.0.87]# vi /usr/local/mysql/etc/my.cnf

#添加:

[mysqld]

#数据文件存储路径

datadir=/data-source

#socket文件路径

socket=/var/run/mysqld/mysql5.socket

#pid文件路径

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

#log日志文件路径

log=/var/log/mysqld/mysql5.log

#错误日志文件路径

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

#端口

port=3306

#所属用户

user=mysql

#默认存储引擎为innodb

default-storage-engine=INNODB

#初始化连接,设置为关闭自动提交

init_connect='set autocommit=0'

#日志形式

log-bin=mysql-bin

#服务器编号

server-id=1

#修改文件夹的所属组

[root@test mysql-5.0.87]# chown mysql:mysql /var/log/mysqld /var/run/mysqld  /usr/src/local/mysql-5.0.87 -R

#创建数据文件所在文件夹

[root@test mysql-5.0.87]# mkdir /data-source

#修改数据文件所在文件夹的属组

[root@test mysql-5.0.87]# chown mysql:mysql /data-source -R

#安装mysql数据库

[root@test mysql-5.0.87]# /usr/local/mysql/bin/mysql_install_db --datadir=/data-source/ --user=mysql

#拷贝服务启动文件

[root@test mysql-5.0.87]# cp /usr/src/mysql-5.0.87/support-files/mysql.server /etc/rc.d/init.d/mysql5

#改变mysql5的权限

[root@test mysql-5.0.87]# chmod 755 /etc/rc.d/init.d/mysql5

#启动数据库

[root@test mysql-5.0.87]# service mysql5 start

Starting MySQL SUCCESS!

#进入数据库

[root@test mysql-5.0.87]# /usr/local/mysql/bin/mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 147

Server version: 5.0.87-yzmyt-debug-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| test               |

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

6 rows in set (0.00 sec)

同理安装数据库到master2,slave1,slave2

配置双主多从复制

先配置master1master2之间的互相复制

master1上执行:

[root@master1 ~]# /usr/local/mysql/bin/mysql -uroot –p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.0.87-terry-debug-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

创建用户允许远程访问本库

mysql> grant replication slave,file on *.* to 'replication'@'192.168.1.12' identified by '123456';

#刷新权限

mysql> flush privileges;

master2上执行:

[root@master2 ~]# /usr/local/mysql/bin/mysql -uroot –p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.0.87-terry-debug-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave,file on *.* to 'replication'@'192.168.1.11' identified by '123456';

mysql> flush privileges;

然后修改master1master2上的my.cnf

master1上添加

log-bin=mysql-bin

server-id=1

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

master2上添加

master1上执行

[root@master1 ~]# /usr/local/mysql/bin/mysql -uroot –p

log-bin=mysql-bin
server-id = 2
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.0.87-terry-debug-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#刷新表为只读状态

mysql> flush tables with read lock;

#查看主的状态

mysql> show master status\G

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

            File: mysql-bin.000008

        Position: 98

    Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)

#锁表

mysql> unlock tables;

#关闭slave应用

mysql> stop slave;

#修改主上的log应用

mysql> change master to master_host='192.168.1.12',master_user='replication',master_password='123456',master_log_file='mysql-bin.000008', master_log_pos=98;

mysql> start slave;

master1上执行

[root@master2 ~]# /usr/local/mysql/bin/mysql -uroot –p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.0.87-terry-debug-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#刷新表为只读模式

mysql> flush tables with read lock;

#查看master状态

mysql> show master status\G

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

            File: mysql-bin.000008

        Position: 98

    Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)

#将表解锁

mysql> unlock tables;

#停止slave应用

mysql> stop slave;

#master上进行应用

mysql> change master to master_host='192.168.1.11',master_user='replication',master_password='123456',master_log_file='mysql-bin.000008', master_log_pos=98;

#启动日志应用

mysql> start slave;

查看slave状态

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

进行检测:

master1中新建一个表,在master2中同时进行生成。

双主互为热备完成。

对两台slave主机进行单向复制

只要修改my.cnf

单向复制的资料网上很多,在此不再赘述

主备都完成的情况是:

master1master2上随便在一台上修改数据,在任何一台机器上都可以看到变化。