@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
主服務器 OS:CentOS IP:192.168.8.130
從服務器 OS:CentOS IP:192.168.8.131
在主,從服務器上安裝MySQL,安裝方法如下:
[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-5.1.73-5.el6_6.x86_64
[root@localhost Desktop]# rpm -e mysql-libs-5.1.73-5.el6_6.x86_64 --nodeps
[root@localhost Desktop]# yum -y install mysql-server mysql mysql-devel
啟動MySQL
[root@localhost Desktop]# service mysqld start
#可以設置MySQL開機啟動,運行命令chkconfig mysqld on
#給root賬號設置密碼
[root@localhost Desktop]# mysqladmin -u root password 'root'
[root@localhost Desktopps]# mysql -u root -p
給從服務器(192.168.8.131)授權,并且給從服務器創建訪問主服務器的賬號和密碼 admin
mysql> grant replication slave on *.* to 'admin'@'192.168.8.131' identified by 'admin';
創建數據庫contract
mysql> create database contract;
mysql>quit;
復制MySQL數據庫配置模版覆蓋/etc/my.cnf
[root@localhost Desktopps]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
[root@localhost Desktopps]#vi /etc/my.cnf
設置以下三個值
log-bin=mysql-bin #指定從服務器讀取的日志文件
server-id = 1 #主服務器必須設定為1,從服務器的值>1
binlog-do-db=contract #對contract數據庫的操作日志會記錄到mysql-bin
#原理:MySQL主從復制的原理是主服務器把對指定數據庫操作的日志寫到指定的日志文件中,從服務器
讀取這個日志文件,寫到從服務器的指定日志文件中,然后在從服務器重新執行日志文件。
配置完之后,重啟MySQL
[root@localhost Desktopps]#service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@localhost Desktopps]# mysql -u root -p
查看主服務器的狀態
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 106
Binlog_Do_DB: contract
Binlog_Ignore_DB:
1 row in set (0.00 sec)
這里記好File和Position的值,配置從服務器的時候需要用到。File就是從服務器需要讀取的日志文件,Position表示從日志文件的什么位置開始讀起。
下面開始配置從服務器
[root@localhost Desktop]# mysqladmin -u root password 'root'
[root@localhost Desktopps]# mysql -u root -p
創建數據庫contract
mysql> create database contract;
mysql>quit;
[root@localhost Desktopps]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
[root@localhost Desktopps]#vi /etc/my.cnf
設置以下兩個值
log-bin=mysql-bin #指定主服務器讀取的日志文件
server-id = 2 #主服務器必須設定為1,從服務器的值>1
[root@localhost Desktopps]# mysql -u root -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.130', MASTER_PORT=3306,
MASTER_USER='admin', MASTER_PASSWORD='admin',
MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=106;
啟動從服務器同步
mysql>start slave;
mysql>show slave status\G;
Slave_IO_Running: YES
Slave_SQL_Running: YES
如果輸出以上內容,則表示MySQL主從復制配置成功。
驗證
在主服務器上運行
[root@localhost Desktopps]# mysql -u root -p
mysql> use contract;
mysql> show tables;
Empty set (0.04 sec)
在從服務器上運行
[root@localhost Desktopps]# mysql -u root -p
mysql> use contract;
Database changed
mysql> show tables;
Empty set (0.04 sec)
確定主從服務器的數據庫contract的下面都沒有表。
在主服務器上運行建表命令,并往表里插入一條記錄:
mysql> create table `user` (`id` int not null auto_increment,`name` varchar (60),`password` varchar (20),`role` int not null,`email` varchar (30),`alertday` int,primary key (`id`));
Query OK, 0 rows affected (0.36 sec)
mysql> insert into `user` (`name`,`password`,`role`,`email`,`alertday`) values('admin','admin',0,'xxxx@xxx.com',30);
Query OK, 1 row affected (0.08 sec)
在從服務器上運行查詢語句。
mysql> select * from user;
+----+-------+----------+------+--------------+----------+
| id | name | password | role | email | alertday |
+----+-------+----------+------+--------------+----------+
| 1 | admin | admin | 0 | xxxx@xxx.com | 30 |
+----+-------+----------+------+--------------+----------+
1 row in set (0.01 sec)
從輸出結果可以看出,主服務器上的數據被同步到從服務器上了。
通過搭建MySQL主從復制結構,可以提高數據的安全性,同時可以實現讀寫分離,讓寫操作在主服務器上進行,
讀操作在從服務器上進行,可以分擔主服務器的負擔。但是如果當主服務器宕機之后,數據庫就只能提供
讀操作了,不能做到故障轉移,這時候,主主復制就應運而生了,有時間整理一下主主復制的配置。
@import url(http://www.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);