MySQL主從配置

MySQL 主從配置


一、MySQL主從原理
MySQL 複製基於主服務器在二進制日誌中跟蹤所有對數據庫的更改(更新、刪除等等)。每個從服務器從主服務器接收主服務器已經記錄到其二進制日誌的保存的更新,以便從服務器可以對其數據拷貝執行相同的更新。將主服務器的數據拷貝到從服務器的一個途徑是使用LOAD DATA FROM MASTER語句。請注意LOAD DATA FROM MASTER目前只在所有表使用MyISAM存儲引擎的主服務器上工作。並且,該語句將獲得全局讀鎖定。MySQL 使用3個線程來執行復制功能,其中1個在主服務器上,另兩個在從服務器上。當發出START SLAVE時,從服務器創建一個I/O線程,以連接主服務器並讓它發送記錄在其二進制日誌中的語句。主服務器創建一個線程將二進制日誌中的內容發送到從服務器。該線程可以識別爲主服務器上SHOW PROCESSLIST的輸出中的Binlog Dump線程。從服務器I/O線程讀取主服務器Binlog Dump線程發送的內容並將該數據拷貝到從服務器數據目錄中的本地文件中,即中繼日誌。 第3個線程是SQL線程,是從服務器創建用於讀取中繼日誌並執行日誌中包含的更新。有多個從服務器的主服務器創建爲每個當前連接的從服務器創建一個線程;每個從服務器有自己的I/O和SQL線程。


二、MySQL主從的準備和實驗環境
1,操作系統CentOS release 6.5 (Final)內核版本:2.6.32-431.el6.x86_64。
2,實驗需要的軟件版本:mysql-5.5.32.tar.gz。
3.主服務器IP:192.168.0.151;從服務器IP:192.168.0.152


三、配置MySQL主從步驟
3.1.1),準備安裝前所需要的軟件及其依賴包:

[root@contrlhost ~]#yum -y install gcc gcc-c++ make ncurses ncurses-devel libaio-devel cmake
[root@contrlhost ~]#rpm -qa ncurses-devel libaio-devel cmake

3.1.2),創建用戶和組,創建安裝目錄

[root@contrlhost ~]#groupadd mysql
[root@contrlhost ~]# useradd mysql -s/sbin/nologin -M -g mysql
[root@contrlhost ~]#id mysql 
[root@contrlhost ~]#cd ~
[root@contrlhost ~]#mkdir /application

3.1.3),上傳mysql-5.5.32軟件到主機上

[root@contrlhost ~]#rz

3.1.4),解壓mysql-5.5.32

[root@contrlhost ~]#tar xvf mysql-5.5.32.tar.gz

3.1.5),編譯安裝mysql-5.5.32

[root@contrlhost ~]#cd mysql-5.5.32
[root@contrlhost mysql-5.5.32]#cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
[root@contrlhost mysql-5.5.32]#make && make install

3.1.6),設置軟鏈接並給其授權

[root@contrlhost mysql-5.5.32]#ln -s /application/mysql-5.5.32/ /application/mysql
[root@contrlhost mysql-5.5.32]#ls -l /application/
[root@contrlhost mysql-5.5.32]#ls /application/mysql/
[root@contrlhost mysql-5.5.32]#cp support-files/mysql.server /etc/init.d/mysqld
[root@contrlhost mysql-5.5.32]#chmod 700 /etc/init.d/mysqld 
[root@contrlhost mysql-5.5.32]#chown mysql.mysql /application/mysql
[root@contrlhost mysql-5.5.32]#chown mysql.mysql /data   #存放數據庫數據文件夾

3.1.7),配置環境變量

[root@contrlhost mysql-5.5.32]#ls /application/mysql/bin/mysql
[root@contrlhost mysql-5.5.32]#echo ' export PATH=/application/mysql/bin:$PATH '>>/etc/profile
[root@contrlhost mysql-5.5.32]#source /etc/profile
[root@contrlhost mysql-5.5.32]#tail -n 1 /etc/profile
[root@contrlhost mysql-5.5.32]#echo $PATH

四、MySQL主上配置my.cnf

[root@contrlhost mysql-5.5.32]#ll support-files/*.cnf
[root@contrlhost mysql]# ll support-files/*.cnf
-rw-r--r-- 1 root root  4721 Dec 11 14:33 support-files/my-huge.cnf
-rw-r--r-- 1 root root 19809 Dec 11 14:33 support-files/my-innodb-heavy-4G.cnf
-rw-r--r-- 1 root root  4695 Dec 11 14:33 support-files/my-large.cnf
-rw-r--r-- 1 root root  4706 Dec 11 14:33 support-files/my-medium.cnf
-rw-r--r-- 1 root root  2870 Dec 11 14:33 support-files/my-small.cnf
[root@contrlhost mysql-5.5.32]#mv /etc/my.cnf /etc/my.cnf.bak

在此可以自定義配置my.cnf

[root@contrlhost mysql]# cat /etc/my.cnf
[mysqld]
port            = 3306
socket          = /application/mysql-5.5.32/tmp/mysql.sock
datadir         =/data/mysql
user            =mysql
#Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id = 1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all

初始化MySQL,此步驟關鍵一步。

[root@contrlhost scripts]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/mysql --user=mysql
WARNING: The host 'contrlhost' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql//bin/mysqladmin -u root password 'new-password'
/application/mysql//bin/mysqladmin -u root -h contrlhost password 'new-password'

Alternatively you can run:
/application/mysql//bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /application/mysql/ ; /application/mysql//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql//mysql-test ; perl mysql-test-run.pl
Please report any problems with the /application/mysql//scripts/mysqlbug script!

添加開機啓動

[root@contrlhost mysql-5.5.32]#chkconfig --add mysqld
[root@contrlhost mysql-5.5.32]#chkconfig --list mysqld

啓動服務登錄MySQL:

[root@contrlhost scripts]# /etc/init.d/mysqld start
Starting MySQL....... SUCCESS! 
[root@contrlhost scripts]#mysql

修改登錄MySQL密碼:

[root@contrlhost scripts]# mysqladmin -uroot password 'hwg123'

登錄數據庫:

[root@contrlhost scripts]# mysql -uroot -phwg123
mysql> select user,host,password from mysql.user;
+------+------------+-------------------------------------------+
| user | host       | password                                  |
+------+------------+-------------------------------------------+
| root | localhost  | *306D1D5B0F3CE16BD454D359127EE7292A69A8E7 |
| root | contrlhost |                                           |
| root | 127.0.0.1  |                                           |
| root | ::1        |                                           |
|      | localhost  |                                           |
|      | contrlhost |                                           |
+------+------------+-------------------------------------------+
6 rows in set (0.04 sec)

刪除空用戶名;contrlhost; ::1 等等;

mysql> delete from mysql.user where user='';
mysql> delete from mysql.user where host='::1';

如果有其他的IP不是你要授權的IP就把它刪除;

mysql> delete from mysql.user where host='192.168.0.151';
mysql> delete from mysql.user where host='contrlhost';
mysql> select user,host from mysql.user;  

在Master上設置授權給slave用戶

mysql> grant all privileges on *.* to 'rep'@'192.168.0.%'identified by 'hwg123' with grant option;
mysql> flush privileges;

查詢授權用戶信息

mysql> select user,host,password from mysql.user;
+------+-------------+-------------------------------------------+
| user | host        | password                                  |
+------+-------------+-------------------------------------------+
| root | localhost   | *306D1D5B0F3CE16BD454D359127EE7292A69A8E7 |
| root | 127.0.0.1   |                                           |
| rep  | 192.168.0.% | *306D1D5B0F3CE16BD454D359127EE7292A69A8E7 |
+------+-------------+-------------------------------------------+
3 rows in set (0.00 sec)

接着確定Master上的bin-log文件和Post點位:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      250 |              |           
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

五、在Slave上執行操作
上述第三大步驟後此處略過!
修改登錄MySQL密碼:

[root@glpi ~]# mysqladmin -uroot password 'hwg123'
mysql> change master to master_host='192.168.0.151',master_user='rep',master_password='hwg123',master_port=3306,master_log_file='mysqlbin.000003',master_log_pos=2164;

查看mysql slave正常與否;

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.151
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 972
               Relay_Log_File: glpi-relay-bin.000002
                Relay_Log_Pos: 1118
        Relay_Master_Log_File: mysql-bin.000004
             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: 972
              Relay_Log_Space: 1273
              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.07 sec)

注意:關鍵要看看IO和mysql兩個進程是否正常;一般是兩個OK即可;如果不正常需要看看具體的日誌文件:
錯誤的情況:
1,Slave_IO_Running: No 顯示爲NO
Slave_IO_Running: No
Slave_SQL_Running: Yes
先查看日誌:

[root@glpi ~]# tail -fn 100 /var/log/mysqld.log

解決辦法:
1,重啓master庫:

[root@glpi ~]#/etc/init.d/mysqld restart

2,查看主庫的狀態:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      972 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)

3,停止slave同步:

mysql> slave stop;

4,根據mysql-bin和Master_Log_Pos重新設定;

mysql> change master to Master_Log_File='mysql-bin.000004',Master_Log_Pos=972;

5,最後啓動從庫同步,然後查詢狀態

mysql> slave start;
mysql> show slave status\G
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章