mysql主從複製 之 一主一從

Linux下配置Mysql主從複製
系統環境:CentOS 5.6
Mysql版本:5.1.58
拓撲圖:mysql

這裏是一主一從,要配置一主多從,其餘從服務器把servier -id 修改成不一樣的數字,其餘的按照從服務器的配置就 OK。
1、安裝mysqllinux

在主從服務器上安裝mysql,最好是一樣版本,若是主服務器版本高,從服務器版本低可能會出問題,若是主服務器版本低,從服務器版本高那卻是沒有問題,這裏用的是5.1.58redis

看Mysql主從複製安裝篇sql

http://linux5588.blog.51cto.com/65280/800139數據庫

若是主從服務器型號同樣,配置同樣,主服務器安裝完mysql以後,直接打包mysql安裝目錄,而後傳到從服務器上,而後添加mysql用戶,修改目錄的權限,就能夠啓動從服務器的mysql服務。vim

2、配置主從複製服務器

1.設置主庫(在主服務器上操做)
如下操做,若是沒有指定在從服務器上操做的,都是在主服務器上操做ide

1)修改主庫my.cnf,vim /usr/local/mysql5.1.58/my.cnf
在[mysqld]部分,添加以下語句ui

  
  
           
  
  
  1. server-id = 1      //主從庫id不能重複  
  2. log-bin=binlog   //開啓二進制日誌文件  
  3. binlog-do-db=bookfm   //要同步的數據庫名字 若是不指定這條那麼是同步全部新建的數據庫  
  4. character-set-server = utf8     //數據庫字符集  
  5. replicate-ignore-db = mysql  //不進行同步的數據庫  
  6. replicate-ignore-db = test     //不進行同步的數據庫  
  7. replicate-ignore-db = information_schema     //不進行同步的數據庫 
  
  
           
  
  
  1. 在[mysql]部分,找到 #no-auto-rehash,去掉no,這個功能就是按table鍵自動補全功能,只能補齊表,列名  
  2. [mysql]  
  3. #no-auto-rehash  
  4. auto-rehash  

2).賦予從庫權限賬號,容許在主庫上讀取日誌this

  
  
           
  
  
  1. mysql>grant replication slave on *.* to 'admin'@'192.168.100.247' identified by '123456'; 

(在從服務器上操做)當即到從庫的機器上登陸試試,看可否登陸上:

  
  
           
  
  
  1. [root@server2 ~]# mysql -uadmin -p -h 192.168.100.248      
  2. Enter password:  
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 2  
  5. Server version: 5.1.58-log Source distribution  
  6.  
  7. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  8. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  9. and you are welcome to modify and redistribute it under the GPL v2 license  
  10.  
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  12.  
  13. mysql>   
  14. 成功顯示mysql>界面表示設置成功。  

3).檢查用戶是否建立成功

  
  
           
  
  
  1. mysql> use mysql;  
  2. Reading table information for completion of table and column names  
  3. You can turn off this feature to get a quicker startup with -A  
  4.  
  5. Database changed  
  6. mysql> select user,host,password from user;  
  7. +-------+-----------------+-------------------------------------------+  
  8. | user  | host            | password                                  |  
  9. +-------+-----------------+-------------------------------------------+  
  10. | root  | localhost       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  11. | root  | server3.com     |                                           |  
  12. | root  | 127.0.0.1       |                                           |  
  13. |       | localhost       |                                           |  
  14. |       | server3.com     |                                           |  
  15. | admin | 192.168.100.247 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  16. +-------+-----------------+-------------------------------------------+  
  17. 6 rows in set (0.00 sec)  
  18. 能夠看到已經建立成功。 

4)建立bookfm數據庫

  
  
           
  
  
  1. mysql> create database bookfm;  
  2. Query OK, 1 row affected (0.00 sec)  
  3.  
  4. mysql> show databases;  
  5. +--------------------+  
  6. | Database           |  
  7. +--------------------+  
  8. | information_schema |  
  9. | bookfm             |  
  10. | mysql              |  
  11. | test               |  
  12. +--------------------+  
  13. 4 rows in set (0.00 sec) 

5)鎖主庫表

  
  
           
  
  
  1. mysql> flush tables with read lock;  
  2. Query OK, 0 rows affected (0.01 sec) 

6)顯示主庫信息,記錄File和Position,從庫設置將會用到

7)將主庫數據目錄打包,發送給從庫機器,這種方式適合於數據庫剛安裝時,數據庫比較單一,若是數據庫比較大可使用mysqldump的方式把數據導出爲.sql文件,而後在從庫上建立同名數據庫,把數據導入

  
  
           
  
  
  1. [root@server3 mysql5.1.58]# tar zcf data.tar.gz data/  
  2. [root@server3 mysql5.1.58]# scp data.tar.gz root@192.168.100.247:/usr/local/mysql5.1.58/ 

2.設置從庫(在從服務器上操做)
如下操做,若是沒有指定在主服務器上操做的,都是在從服務器上操做

1)備份從庫data目錄,把從主庫複製過來的data.tar.gz直接解壓出來

  
  
           
  
  
  1. [root@server2 mysql5.1.58]# mv data data_bak  
  2. [root@server2 mysql5.1.58# tar zxf data.tar.gz   

2)解鎖主庫表(在主服務器上操做)

  
  
           
  
  
  1. mysql>unlock tables; 

3)啓動從庫mysql服務

  
  
           
  
  
  1. 編輯從庫/usr/local/mysql5.1.58/my.cnf ,找到server-id把值修改成2   
  2. server-id = 2   
  3.  
  4. [root@server2 mysql5.1.58]# /usr/local/mysql5.1.58/bin/mysqld_safe --defaults-file=/usr/local/mysql5.1.58/my.cnf --user=mysql &  

4)在從庫上設置同步

  
  
           
  
  
  1. 設置鏈接MASTER MASTER_LOG爲主庫的File,MASTER_LOG_POS爲主庫的Position  
  2. mysql> slave stop;  
  3. mysql> change master to master_host='192.168.100.248',master_user='admin',master_password='123456',master_log_file='binlog.000006',master_log_pos=278;  
  4. mysql> slave start;  
  5. mysql> show slave status\G;  
  6. *************************** 1. row ***************************  
  7.                Slave_IO_State: Waiting for master to send event  
  8.                   Master_Host: 192.168.100.248  
  9.                   Master_User: admin  
  10.                   Master_Port: 3306  
  11.                 Connect_Retry: 60  
  12.               Master_Log_File: binlog.000006  
  13.           Read_Master_Log_Pos: 278  
  14.                Relay_Log_File: server2-relay-bin.000002  
  15.                 Relay_Log_Pos: 248  
  16.         Relay_Master_Log_File: binlog.000006  
  17.              Slave_IO_Running: Yes  
  18.             Slave_SQL_Running: Yes        //這2項要Yes才行  
  19.               Replicate_Do_DB:  
  20.           Replicate_Ignore_DB:  
  21.            Replicate_Do_Table:  
  22.        Replicate_Ignore_Table:  
  23.       Replicate_Wild_Do_Table:  
  24.   Replicate_Wild_Ignore_Table:  
  25.                    Last_Errno: 0  
  26.                    Last_Error:  
  27.                  Skip_Counter: 0  
  28.           Exec_Master_Log_Pos: 278  
  29.               Relay_Log_Space: 405  
  30.               Until_Condition: None  
  31.                Until_Log_File:  
  32.                 Until_Log_Pos: 0  
  33.            Master_SSL_Allowed: No  
  34.            Master_SSL_CA_File:  
  35.            Master_SSL_CA_Path:  
  36.               Master_SSL_Cert:  
  37.             Master_SSL_Cipher:  
  38.                Master_SSL_Key:  
  39.         Seconds_Behind_Master: 0  
  40. Master_SSL_Verify_Server_Cert: No  
  41.                 Last_IO_Errno: 0  
  42.                 Last_IO_Error:  
  43.                Last_SQL_Errno: 0  
  44.                Last_SQL_Error:  
  45. 1 row in set (0.00 sec)  
  46.  
  47. ERROR:  
  48. No query specified 

5)在主庫上的bookfm數據庫上創建book表

  
  
           
  
  
  1. mysql> create table book (id int,name char(10)) engine=MYISAM;  
  2. mysql> insert into book values(1,'a');  

6)在從庫上查詢

  
  
           
  
  
  1. mysql> select * from book;  
  2. +------+------+  
  3. | id   | name |  
  4. +------+------+  
  5. |    1 | a    |  
  6. +------+------+  
  7. 1 row in set (0.00 sec)  
  8. 能夠查詢到,說明配置成功  
相關文章
相關標籤/搜索