<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 36, comments - 30, trackbacks - 0, articles - 3

    MySQL主從復制配置

    Posted on 2016-02-23 20:41 笑看人生 閱讀(365) 評論(0)  編輯  收藏
    @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://m.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)授權,并且給從服務器創(chuàng)建訪問主服務器的賬號和密碼 admin
    mysql> grant replication slave on *.* to 'admin'@'192.168.8.131' identified by 'admin';
    創(chuàng)建數(shù)據(jù)庫contract
    mysql> create database contract;
    mysql>quit;

    復制MySQL數(shù)據(jù)庫配置模版覆蓋/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數(shù)據(jù)庫的操作日志會記錄到mysql-bin

    #原理:MySQL主從復制的原理是主服務器把對指定數(shù)據(jù)庫操作的日志寫到指定的日志文件中,從服務器
                讀取這個日志文件,寫到從服務器的指定日志文件中,然后在從服務器重新執(zhí)行日志文件。

    配置完之后,重啟MySQL
    [root@localhost Desktopps]#service mysqld restart
    Stopping mysqld:                                          [  OK  ]
    Starting mysqld:                                           [  OK  ]

    [root@localhost Desktopps]# mysql -u root -p
    查看主服務器的狀態(tài)
    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
    創(chuàng)建數(shù)據(jù)庫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;
    Database changed
    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)

    確定主從服務器的數(shù)據(jù)庫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)

    從輸出結果可以看出,主服務器上的數(shù)據(jù)被同步到從服務器上了。

    通過搭建MySQL主從復制結構,可以提高數(shù)據(jù)的安全性,同時可以實現(xiàn)讀寫分離,讓寫操作在主服務器上進行,
    讀操作在從服務器上進行,可以分擔主服務器的負擔。但是如果當主服務器宕機之后,數(shù)據(jù)庫就只能提供
    讀操作了,不能做到故障轉移,這時候,主主復制就應運而生了,有時間整理一下主主復制的配置。




    @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 成年女人永久免费观看片| 99视频精品全部免费观看| 国产成人免费爽爽爽视频| 亚洲精品一卡2卡3卡三卡四卡| 中文字幕日本人妻久久久免费| 亚洲精品国偷自产在线| WWW免费视频在线观看播放| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 亚洲国产理论片在线播放| 1a级毛片免费观看| 久久亚洲AV成人无码国产| 国产在线观看麻豆91精品免费| 亚洲一区二区影视| 精品久久免费视频| 日本激情猛烈在线看免费观看| 亚洲福利精品电影在线观看| 日本特黄特色AAA大片免费| 91麻豆国产自产在线观看亚洲 | 免费国产a理论片| 久久精品夜色国产亚洲av| 1000部拍拍拍18勿入免费视频下载 | 国产成人综合亚洲AV第一页| 一区二区三区四区免费视频 | 亚洲国产一级在线观看 | 国产AV无码专区亚洲AV麻豆丫| 日韩亚洲国产二区| 西西人体免费视频| 亚洲人成电影青青在线播放| 国产资源免费观看| a级毛片高清免费视频就| 亚洲电影唐人社一区二区| 热99re久久精品精品免费| 中文在线观看永久免费| 亚洲一区免费视频| 亚洲日韩中文字幕日韩在线| 在线看无码的免费网站| 久久无码av亚洲精品色午夜| 亚洲AV无码专区国产乱码电影| 曰批全过程免费视频在线观看| 一级中文字幕免费乱码专区| 亚洲美女在线观看播放|