歡迎您光臨本站 註冊首頁

mysql 主從複製讀寫分離實現

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

mysql主從複製 (一)安裝mysql(主從伺服器皆相同) 先創建用戶 useradd mysql -s /sbin/nologin tar zxvf mysql-5.0.45.tar.gz cd mysql-5.0.45 ./configure --prefix=/usr/local/mysql --localstatedir=/opt/data --with-extra-charsets=utf8,gb2312,gbk --with-pthread --enable-thread-safe-client 注:配置過程指定數據文件的位置及另加的字符集. make make install cp support-files/my-large.cnf /etc/my.cnf cd /usr/local/mysql chgrp -R mysql . 生成基本的資料庫和表: /usr/local/mysql/bin/mysql_install_db --user=mysql 成功執行后察看數據目錄/opt/data,看是否有文件或目錄生成. chown -R mysql:mysql /opt/data 記得給每個資料庫設置root密碼. (二)修改配置文件 不同的地方就是server-id,主伺服器配置文件不用修改,從伺服器的配置文件server-id=10.其他的內容基本相同. (三)啟動服務 /usr/local/mysql/bin/mysqld_safe --user=mysql& 這個過程主輔伺服器都相同. (四)授權(在主伺服器上進行) GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.8.100 identified by 'mylqs'; (五)查詢主資料庫狀態(主伺服器上進行) mysql> show master status; ------------------ ---------- -------------- ------------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |


------------------ ---------- -------------- ------------------
| mysql-bin.000003 | 235 | | |
------------------ ---------- -------------- ------------------
記下file及position的值,後面做從伺服器操作的時候需要用. (六)配置從伺服器 mysql> change master to master_host='192.168.8.101', master_user='rep1', master_password='mysql', master_log_file='mysql-bin.000003', master_log_pos=235; 正確執行后再執行: mysql> start slave; 就啟用了複製功能.這裡我們運行一下 mysql> show slave statusG 來檢查一下,一個正常的輸出結果應該如下面的形式:
mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.8.101 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 235 Relay_Log_File: -relay-bin.000009 Relay_Log_Pos: 235 Relay_Master_Log_File: mysql-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: 235 Relay_Log_Space: 235 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 1 row in set (0.00 sec)
mysql代理安裝配置 一、安裝mysql-proxy.需要按下列順序安裝其所依賴的包: (一)安裝LUA tar zxvf lua-5.1.tar.gz cd lua-5.1 用vi修改Makefile,使"INSTALL_TOP=/usr/local/lua

",這樣做的目的是為了是lua的所有文件都安裝在目錄/usr/local/lua/
make posix make install (二)安裝 libevent tar zxvf libevent-1.1a.tar.gz cd libevent-1.1a ./configure --prefix=/usr/local/libevent make make install (三)安裝check tar zxvf check-0.8.4.tar.gz cd check-0.8.4 ./configure make make install (四)設置安裝mysql-proxy所需的環境變數.把下面的內容追加到/etc/profile中 export LUA_CFLAGS="-I/usr/local/lua/include" LUA_LIBS="-L/usr/local/lua/lib -llua -ldl" LDFLAGS="-L/usr/local/libevent/lib -lm" export CPPFLAGS="-I/usr/local/libevent/include" export CFLAGS="-I/usr/local/libevent/include" 然後執行 source /etc/profile (安裝完mysql-proxy不再需要這些變數,可以刪除之) (五)安裝mysql(只安裝mysql客戶端即可) tar zxvf mysql-5.0.45.tar.gz cd mysql-5.0.45 ./configure --prefix=/usr/local/mysql --without-server

make make install (六)安裝mysql-proxy tar zxvf mysql-proxy-0.5.0.tar.gz cd mysql-proxy-0.5.0 ./configure –prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql --with-lua Make Make install 二、主要的命令行選項 --help-all顯示所有的幫助選項 --admin-address=host:port 管理主機及埠,默認是4041 --proxy-address=host:port 代理伺服器的監聽地址及埠,默認4040 --proxy-read-only-address=host:port 只讀連接時,代理伺服器的監聽地址及埠.默認4042 --proxy-backend-addresses=host:port連接真實伺服器的地址及監聽埠,默認是3306,這是mysql代理最重要的選項,多個主機之間用空格隔開.使用rr演算法. --proxy-lua-script=file指定lua腳本的名稱 三、使用方法

2mysql伺服器的情形
--proxy-backend-addresses=mysql_ip1:3306 --proxy-backend-addresses=mysql_ip2:3306
3個伺服器的情形:一個主伺服器,負責寫入;2個從伺服器,負責查詢.
mysql-proxy --proxy-read-only-address=<slave_ip1>:3306 --proxy-read-only-address=<slave_ip2>:3306
配置驗證 1、主從複製測試:在主資料庫伺服器上創建庫和表,然後再插入記錄,再登陸到從伺服器,看是否也建立相一致的庫和表以及記錄.
mysql> create database first_db; Query OK, 1 row affected (0.01 sec) mysql> create table first_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into first_tb values (001,'myself'); Query OK, 1 row affected (0.00 sec) 在主數據伺服器的表first_db中插入記錄
現在轉到從資料庫伺服器,看是否同步了上面主資料庫的數據

mysql> show databases; -------------------- | Database | -------------------- | information_schema | | first_db | | mysql | | test | -------------------- 4 rows in set (0.01 sec) 資料庫自動生成了 mysql> use first_db;
Database changed mysql> show tables;
--------------------
| Tables_in_first_db |
--------------------
| first_tb |
--------------------
1 row in set (0.00 sec) 表也自動生成了 mysql> select * from first_tb;
------ --------
| id | name |
------ --------
| 1 | myself |
------ --------
1 row in set (0.00 sec) 記錄也按照我們的意願存在了
2、讀寫分離:用mysql客戶端程序如mysql登陸到mysql代理,然後執行讀寫操作,以測試讀寫分離的正確性.

本文出自 「愛你linux」 博客,謝絕轉載!


[火星人 ] mysql 主從複製讀寫分離實現已經有427次圍觀

http://coctec.com/docs/linux/show-post-49582.html