mysql主從復制


由於mysql被Oracle收購了,隨時都有可能變成付費產品,所以呢這里我們使用由mysql創始人Michael Widenius在mysql基礎上重新創建的一個分支mariadb。

主要由開源社區在維護,采用GPL授權許可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。

我們的環境是:

主服務器  centos 7  192.168.10.129   mariadb-5.5.60

從服務器  centos 7  192.168.10.130   mariadb-5.5.60

1、配置yum源

[root@localhost ~]# mkdir /etc/yum.repos.d/backup

[root@localhost ~]# mv /etc/yum.repos.d/* /etc/yum.repos.d/backup/   # 備份系統自帶yum源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo   # 使用阿里的源
  
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo    # 阿里源

[root@localhost ~]# yum clean all        # 清除原來的yum緩存

[root@localhost ~]# yum makecache        # 生成新的緩存

注意:這里的阿里源mariadb的版本是mariadb-5.5.60,如果你想使用最新版的mariadb阿里源可能不適合你。你需要使用官方的源。

vi /etc/yum.repos.d/MariaDB.repo    # 添加mariadb的官方yum庫
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

使用如下命令安裝最新版的mariadb(注:本文沒有使用最新版的mariadb

[root@localhost ~]#yum install MariaDB-server MariaDB-client -y     # 安裝mariadb的服務端和客戶端

2、使用yum分別在兩台機器上安裝mariadb

[root@localhost ~]# yum install mariadb-server mariadb -y   # 安裝mariadb的服務端和客戶端

如果一切順利你會看到如下的界面

3、mariadb服務管理

 在Centos 7中使用yum安裝的程序,都可以使用systemctl來進行管理:

[root@localhost ~]# systemctl start mariadb     // 開啟mysql服務
[root@localhost ~]# systemctl stop mariadb      // 停止mysql服務
[root@localhost ~]# systemctl restart mariadb   // 重啟mysql服務
[root@localhost ~]# systemctl enable mariadb    // 設置開機啟動
[root@localhost ~]# systemctl disable mariadb   // 禁止開機啟動

4、初始化mariadb

MariaDB 安裝完畢並成功啟動后為了確保數據庫的安全性和正常運轉,需要先對數據庫程序進行初始化操作。

 1 [root@localhost ~]# systemctl start mariadb     // 啟動mysql服務
 2 [root@localhost ~]# mysql_secure_installation   // 初始化mysql數據庫
 3 
 4 // 進入mariadb的初始化配置模式
 5 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
 6       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 7 
 8 In order to log into MariaDB to secure it, we'll need the current
 9 password for the root user.  If you've just installed MariaDB, and
10 you haven't set the root password yet, the password will be blank,
11 so you should just press enter here.
12 
13 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
14 Enter current password for root (enter for none):    // 這里輸入mysql數據庫root密碼,默認沒有密碼
15 OK, successfully used password, moving on...
16 
17 Setting the root password ensures that nobody can log into the MariaDB
18 root user without the proper authorisation.
19 
20 Set root password? [Y/n] y     //  是否為mysql數據庫root用戶設置密碼,這里我們設置密碼
21 New password:    // 輸入要設置的密碼
22 Re-enter new password:   // 確認輸入的密碼
23 Password updated successfully!
24 Reloading privilege tables..
25  ... Success!
26 
27 
28 By default, a MariaDB installation has an anonymous user, allowing anyone
29 to log into MariaDB without having to have a user account created for
30 them.  This is intended only for testing, and to make the installation
31 go a bit smoother.  You should remove them before moving into a
32 production environment.
33 
34 Remove anonymous users? [Y/n] y   //  是否要刪除匿名用戶,這里刪除掉。要不然可以在不輸入用戶名和密碼的情況下登陸數據庫
35  ... Success!
36 
37 Normally, root should only be allowed to connect from 'localhost'.  This
38 ensures that someone cannot guess at the root password from the network.
39 
40 Disallow root login remotely? [Y/n] n   // 是否拒絕root用戶從遠程登陸數據庫,這里允許root用戶遠程登陸數據庫
41  ... skipping.
42 
43 By default, MariaDB comes with a database named 'test' that anyone can
44 access.  This is also intended only for testing, and should be removed
45 before moving into a production environment.
46 
47 Remove test database and access to it? [Y/n] y   // 是否刪除mysql自帶的test測試數據庫,這里沒什么用就刪除了吧
48  - Dropping test database...
49  ... Success!
50  - Removing privileges on test database...
51  ... Success!
52 
53 Reloading the privilege tables will ensure that all changes made so far
54 will take effect immediately.
55 
56 Reload privilege tables now? [Y/n] y   // 是否立即生效,這里立即生效
57  ... Success!
58 
59 Cleaning up...
60 
61 All done!  If you've completed all of the above steps, your MariaDB
62 installation should now be secure.
63 
64 Thanks for using MariaDB!
mariadb數據庫的初始化

5、配置mariadb數據庫的編碼

mariadb的默認數據庫編碼是拉丁文,如果你不信我們登陸數據庫看一下:

[root@localhost ~]# mysql -uroot -p

對於我們炎黃子孫來說拉丁文明顯不合我意,所以我們需要調整下my.cnf的配置文件

[root@localhost bin]# vim /etc/my.cnf
[mysqld]

character-set-server=utf8           // 設置服務端的字符集
collation-server=utf8_general_ci    // 設置服務端的字符集

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[client]                //  設置客戶端的字符集
default-character-set=utf8 
[mysql]                //  設置客戶端的字符集
default-character-set=utf8
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
my.cnf

設置完成以后,我們需要重啟mariadb服務,兩台服務器的配置一樣,這里就不再贅述,下面進入主題。

6、設置192.168.10.129中mysql為主服務器。

6.1在my.cnf配置文件中添加如下信息:

[mysqld]
server-id=1        # 設置主服務器的id
log-bin=mysql-bin  # 設置二進制日志文件名稱,主服務器執行sql語句的二進制文件
character-set-server=utf8        # 服務端的字符集
collation-server=utf8_general_ci # 服務端的字符編碼
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[client]   # 客戶端字符集
default-character-set=utf8
[mysql]    # 客戶端字符集
default-character-set=utf8
#
# include all files from the config directory
#
my.cnf

調整好配置文件需要重啟mariadb服務

[root@localhost ~]# systemctl restart mariadb

6.2配置mariadb主服務器

  MariaDB [(none)]> grant replication slave on *.* to 'slave_user'@'%' identified by '123.com';  // 添加同步賬號

  MariaDB [(none)]> flush table with read lock; // 為防止在導出數據庫時,有人寫入數據,這里先把數據庫加把鎖

  MariaDB [(none)]> show master status;

// 我們的主服務器信息如下,這里的兩個選項很重要,因為在配置從服務器時需要File、Position兩個字段的數據: +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 369 | | | +------------------+----------+--------------+------------------+
// mysql-bin便是我們在my.cnf文件中設置的二進制文件名
// 369是當前二進制文件所處的位置,從服務器一定要和這個位置一致。
// 此時當前shell正處於鎖表狀態,我們在開一個shell,導出所有數據庫
[root@localhost ~]# mysqldump -uroot -p --all-databases > all.sql 
// 數據導出后,解鎖表
MariaDB [(none)]> unlock tables;
// 將導出的數據庫發給從服務器192.168.10.130 
[root@localhost ~]# scp all.sql root@192.168.10.130:/opt/

7、配置192.168.10.130從服務器

7.1調整my.cnf配置文件

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=3   # 設置從服務器的id
character-set-server=utf8    # 設置服務端字符集
collation-server=utf8_general_ci  # 設置服務端字符集

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[client]   # 設置客戶端字符集
default-character-set=utf8
[mysql]    # 設置客戶端字符集
default-character-set=utf8
# 
# include all files from the config directory
#
my.cnf

7.2將主服務器192.168.10.129數據導入到本地數據庫中

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> source /opt/all.sql  // 將主服務器發來的數據庫文件導入到本地數據庫

重啟mysql服務

[root@localhost ~]# systemctl restart mariadb

登陸mysql服務開啟從服務器

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> change master to master_host='192.168.10.129', master_user='slave_user', master_password='123.com', master_log_file='mysql-bin.000001', master_log_pos=369;
master_host:主服務器IP地址
master_user:主服務器中創建的同步賬戶
master_password:同步賬戶的密碼
master_log_file:主服務器sql語句二進制文件(主服務器中show master status中的File
master_log_pos:主服務器當前sql語句在二進制文件中的位置(主服務器中show master status中的Position)。

 MariaDB [(none)]> start slave;    // 開啟從服務器

 MariaDB [(none)]> show slave status\G    // 查看從服務器信息
 *************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.10.129
 Master_User: slave_user
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000001
 Read_Master_Log_Pos: 614
 Relay_Log_File: mariadb-relay-bin.000002
 Relay_Log_Pos: 774
 Relay_Master_Log_File: mysql-bin.000001
 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: 614
 Relay_Log_Space: 1070
 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)

8、驗證我們的主從配置是否正確

如果你也做到了和下面這張圖一樣那么恭喜你,配置成功啦。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM