keepalive+mysql 主主配置

1.   環境說明:

機器名

eth0

說明

server01

192.168.100.30/24

Mysqlkeepalive

server02

192.168.100.31/24

Mysqlkeepalive

 

2.   安裝mysql

去官網下載mysql5.5的安裝包,源碼安裝比較麻煩所以忽略

MySQL-client-5.5.29-1.el6.x86_64.rpm  MySQL-server-5.5.29-1.el6.x86_64.rpm MySQL-devel-5.5.29-2.el6.x86_64.rpm

出現錯誤提示:

[root@server01 ~]# rpm -ivhMySQL-server-5.5.29-1.el6.x86_64.rpm

Preparing...               ########################################### [100%]

       file /usr/share/mysql/charsets/Index.xml from install ofMySQL-server-5.5.29-1.el6.x86_64 conflicts with file from packagemysql-libs-5.1.61-4.el6.x86_64

這個是因爲安裝了5.1的工具包的問題卸載加參數-nodeps不檢查依賴

[root@server01 ~]# rpm -e --nodepsmysql-libs

[root@server01 ~]# rpm -ivh  MySQL-server-5.5.29-1.el6.x86_64.rpm

[root@server01 ~]# rpm -ivh  MySQL-client-5.5.29-1.el6.x86_64.rpm

 

3.   配置mysql

查看mysql安裝路徑

[root@server01 mysql]# whereis mysql

mysql: /usr/bin/mysql /usr/lib64/mysql/usr/share/mysql /usr/share/man/man1/mysql.1.gz

創建配置文件夾可以直接放在/etc目錄下

 [root@server01mysql]# cp my-medium.cnf  /etc/

[root@server01 mysql]# mv my-medium.cnfmy.cnf

[root@server01 mysql]# chown  mysql:mysql /etc/my.conf

配置my.cnf

master.cnf:

[mysqld]

server-id=1

log-bin=server01.log

relay-log-index=slave-relay-bin.index #配置雙主模式所以服務器都要配置從的relay-log

relay-log=slave-relay-bin

innodb_flush_log_at_trx_commit=1

sync_binlog=1

注意:爲了使用事務的InnoDB在複製中最大的持久性和一致性,你應該指定innodb_flush_log_at_trx_commit=1,sync_binlog=1 選項。

需要在從機的slave.cnf文件的【mysqld】部分增加server-id選項。server-id的值類似主機,必須是 1到2 的32次方之間的一個正整數,而且必須和主機的ID不一樣。如果你設置多臺從機,那麼每臺必須有別於主機和其他從機的唯一的server-id值。可以把server-id值認爲是類似IP地址的東西:這些ID在複製服務器通信的時候標識了每臺唯一的服務器實例。

slave.cnf:

[mysqld]

server-id=2

log-bin=server02-bin.log

relay-log-index=slave-relay-bin.index

relay-log=slave-relay-bin

innodb_flush_log_at_trx_commit=1

sync_binlog=1

 

設置mysql自啓動

[root@server01 mysql]# chkconfig  mysql on

[root@server01 etc]# service  mysql start

[root@server01 ~]# mysqladmin -urootpassword password

4.    創建複製賬戶

[root@server01 ~]# mysql –uroot

mysql> create [email protected];

mysql> grant replication slave on *.* [email protected] identified by 'password';

 

鎖住主機,禁止用戶寫入mysql

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

不改變mysql用戶,複製test測試同步庫

mysqldump -urrot -password test >test.sql

從機啓動會解鎖

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec

拷貝test.sql到從機

從機執行

mysql -uroot -ppassword test < test.sql

回到主機查看當前日誌偏差值

mysql> show master status;

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

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB|

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

| server01-bin.000003 |     4921 |              |                  |

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

1 row in set (0.00 sec)

 

在從機上初始化複製了。在從機上執行以下命令

 

mysql> slave stop;

Query OK, 0 rows affected, 1 warning (0.00sec)

 

mysql> change master toMASTER_HOST='192.168.100.30',

   -> MASTER_USER='repl_user',

   -> MASTER_PASSWORD='password',

   -> MASTER_LOG_FILE='server01-bin.000003',

   -> MASTER_LOG_POS= 2877;

Query OK, 0 rows affected (0.03 sec)

 

mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

 

查看狀態

mysql> show slave status\G

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.100.30

                  Master_User: repl_user

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File:server01-bin.000003

         Read_Master_Log_Pos: 4921

               Relay_Log_File:slave-relay-bin.000002

                Relay_Log_Pos: 256

       Relay_Master_Log_File: server01-bin.000003

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

              Replicate_Do_DB:

         Replicate_Ignore_DB:

          Replicate_Do_Table:

      Replicate_Ignore_Table:

     Replicate_Wild_Do_Table:

 Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

         Exec_Master_Log_Pos: 4921

              Relay_Log_Space: 412

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

          Master_SSL_Allowed: No

          Master_SSL_CA_File:

          Master_SSL_CA_Path:

              Master_SSL_Cert:

           Master_SSL_Cipher:

               Master_SSL_Key:

       Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

 Replicate_Ignore_Server_Ids:

            Master_Server_Id: 1

1 row in set (0.00 sec)

5.    驗證從機複製特性

主機數據庫內容如下:

mysql> use test

Database changed

mysql> select * from new

   -> ;

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

| id | col1 | col2 |

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

|  1| hah  | test |

|  2| s    | b    |

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

2 rows in set (0.00 sec)

從機數據庫內容:

Database changed

mysql> select * from new;

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

| id | col1 | col2 |

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

|  1| hah  | test |

|  2| s    | b    |

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

2 rows in set (0.02 sec)

 

回到主機修改數據:

mysql> update new setcol1='haha',col2='change' where id='1';

Query OK, 1 row affected (0.04 sec)

Rows matched: 1  Changed: 1 Warnings: 0

 

mysql> select * from new

   -> ;

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

| id | col1 | col2   |

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

|  1| haha | change |

|  2| s    | b      |

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

2 rows in set (0.00 sec)

查看從機是否修改:

mysql> select * from new;

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

| id | col1 | col2   |

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

|  1| haha | change |

|  2| s    | b      |

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

2 rows in set (0.00 sec)

觀察得出從機已經從主機同步到了數據。

 

6.   主主模式配置

現在的設置爲192.168.100.30爲主機,192.168.100.31爲從機,

按照上述設置再將192.168.100.31設爲主機,192.168.100.30設爲從機。

設置完成後兩臺機器既相互爲主機也相互爲備機,都可以寫入數據並同步到對方機器。

 

7.    安裝keepalived(兩臺均安裝)

安裝依賴包

[root@server01 ~]# yum install -y gcc gcc+gcc-c++ openssl openssl-devel popt-devel

下載

[root@server01 ~]# wgethttp://www.keepalived.org/software/keepalived-1.2.7.tar.gz

[root@server01 ~]# tar -xzf  keepalived-1.2.7.tar.gz

[root@server01 ~]#./configure  --prefix=/usr/local/keeplived

[root@server01 ~]# make

[root@server01 ~]# make install

[root@server01 ~]# cp/usr/local/keeplived/sbin/keepalived /usr/bin/

[root@server01 ~]# cp /usr/local/keeplived/etc/sysconfig/keepalived  /etc/sysconfig/

[root@server01 ~]# cp/usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/init.d/

[root@server01 ~]# mkdir /etc/keepalived

創建 vi keepalived.conf如下內容

! Configuration File for keepalived

global_defs {

   notification_email {

       [email protected]

    }

   

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   virtual_router_id 50

   priority 100

   advert_int 1

   nopreempt

   authentication {

       auth_type PASS

       auth_pass 1111

    }

 

virtual_ipaddress {

   192.168.100.40

    }

}

192.168.100.31上的配置文件爲

global_defs {

   notification_email {

       [email protected]

    }

   

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   virtual_router_id 50

   priority 90

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

 

virtual_ipaddress {

   192.168.100.40

    }

}

[root@server01 ~]# ping 192.168.100.40

PING 192.168.100.40 (192.168.100.40) 56(84)bytes of data.

64 bytes from 192.168.100.40: icmp_seq=1ttl=64 time=0.262 ms

[root@server02 ~]# ping 192.168.100.40

PING 192.168.100.40 (192.168.100.40) 56(84)bytes of data.

64 bytes from 192.168.100.40: icmp_seq=1ttl=64 time=0.064 ms

兩臺服務器均可以ping通服務器。

Ip addr可以看到虛擬IP已經綁定到網卡上了

發佈了41 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章