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
- server-id = 1 //主從庫id不能重複
- log-bin=binlog //開啓二進制日誌文件
- binlog-do-db=bookfm //要同步的數據庫名字 若是不指定這條那麼是同步全部新建的數據庫
- character-set-server = utf8 //數據庫字符集
- replicate-ignore-db = mysql //不進行同步的數據庫
- replicate-ignore-db = test //不進行同步的數據庫
- replicate-ignore-db = information_schema //不進行同步的數據庫
- 在[mysql]部分,找到 #no-auto-rehash,去掉no,這個功能就是按table鍵自動補全功能,只能補齊表,列名
- [mysql]
- #no-auto-rehash
- auto-rehash
2).賦予從庫權限賬號,容許在主庫上讀取日誌this
- mysql>grant replication slave on *.* to 'admin'@'192.168.100.247' identified by '123456';
(在從服務器上操做)當即到從庫的機器上登陸試試,看可否登陸上:
- [root@server2 ~]# mysql -uadmin -p -h 192.168.100.248
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.1.58-log Source distribution
- Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- This software comes with ABSOLUTELY NO WARRANTY. This is free software,
- and you are welcome to modify and redistribute it under the GPL v2 license
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql>
- 成功顯示mysql>界面表示設置成功。
3).檢查用戶是否建立成功
- mysql> use mysql;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> select user,host,password from user;
- +-------+-----------------+-------------------------------------------+
- | user | host | password |
- +-------+-----------------+-------------------------------------------+
- | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
- | root | server3.com | |
- | root | 127.0.0.1 | |
- | | localhost | |
- | | server3.com | |
- | admin | 192.168.100.247 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
- +-------+-----------------+-------------------------------------------+
- 6 rows in set (0.00 sec)
- 能夠看到已經建立成功。
4)建立bookfm數據庫
- mysql> create database bookfm;
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | bookfm |
- | mysql |
- | test |
- +--------------------+
- 4 rows in set (0.00 sec)
5)鎖主庫表
- mysql> flush tables with read lock;
- Query OK, 0 rows affected (0.01 sec)
6)顯示主庫信息,記錄File和Position,從庫設置將會用到
7)將主庫數據目錄打包,發送給從庫機器,這種方式適合於數據庫剛安裝時,數據庫比較單一,若是數據庫比較大可使用mysqldump的方式把數據導出爲.sql文件,而後在從庫上建立同名數據庫,把數據導入
- [root@server3 mysql5.1.58]# tar zcf data.tar.gz data/
- [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直接解壓出來
- [root@server2 mysql5.1.58]# mv data data_bak
- [root@server2 mysql5.1.58# tar zxf data.tar.gz
2)解鎖主庫表(在主服務器上操做)
- mysql>unlock tables;
3)啓動從庫mysql服務
- 編輯從庫/usr/local/mysql5.1.58/my.cnf ,找到server-id把值修改成2
- server-id = 2
- [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)在從庫上設置同步
- 設置鏈接MASTER MASTER_LOG爲主庫的File,MASTER_LOG_POS爲主庫的Position
- mysql> slave stop;
- 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;
- mysql> slave start;
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.100.248
- Master_User: admin
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: binlog.000006
- Read_Master_Log_Pos: 278
- Relay_Log_File: server2-relay-bin.000002
- Relay_Log_Pos: 248
- Relay_Master_Log_File: binlog.000006
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes //這2項要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: 278
- Relay_Log_Space: 405
- 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:
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
5)在主庫上的bookfm數據庫上創建book表
- mysql> create table book (id int,name char(10)) engine=MYISAM;
- mysql> insert into book values(1,'a');
6)在從庫上查詢
- mysql> select * from book;
- +------+------+
- | id | name |
- +------+------+
- | 1 | a |
- +------+------+
- 1 row in set (0.00 sec)
- 能夠查詢到,說明配置成功