數據庫的整體架構
架構說明:
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上
配置雙主多從複製
先配置master1和master2之間的互相複製
在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; |
然後修改master1和master2上的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 |
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
單向複製的資料網上很多,在此不再贅述
主備都完成的情況是:
在master1,master2上隨便在一臺上修改數據,在任何一臺機器上都可以看到變化。