1. 程式人生 > >Linux中詳細部署mysql主從

Linux中詳細部署mysql主從

Owner channel ati tid 插入 從數據 授權 sha2 mysql主從

1.主從簡介
在現代企業中,數據顯得尤為重要,而存儲數據的數據庫選擇又五花八門,但無論是何種數據庫,均存在著一種隱患。
想幾個問題:
用一臺數據庫存放數據,若此數據庫服務器掛了導致數據丟失怎麽辦?
業務量大了,數據多了,訪問的人多了,一臺數據庫無法保證服務質量了怎麽辦?


2.主從作用
實時災備,用於故障切換
讀寫分離,提供查詢服務
備份,避免影響業務


3.主從形式
技術分享圖片
一主一從
主主復制
一主多從---擴展系統讀取的性能,因為讀是在從庫讀取的
多主一從---5.7版本開始支持
聯級復制


4.主從復制原理
技術分享圖片

1.主庫將所有的寫操作記錄到binlog日誌中並生成一個log dump線程,通過log dump線程將binlog日誌傳給從庫的I/O線程

2.從庫生成兩個線程,一個I/O線程,一個SQL線程
3.I/O線程去請求主庫的binlog,並將得到的binlog日誌寫入到relay log (中繼日誌)文件中
4.SQL線程,會讀取relay log 文件中的日誌,並解析成具體操作,來實現主從的操作一致,達到最終數據一致的目的。


*5.主從復制配置
主從復制配置步驟:
1.確保從數據庫與主數據庫裏的數據一樣
2.在主數據庫裏創建一個同步賬號授權給從數據庫使用
3.配置主數據庫(修改配置文件)
4.配置從數據庫(修改配置文件)


環境說明:

數據庫角色 IP 應用與系統版本 有無數據
主數據庫 192.168.209.12 Centos7/redhat7/ mysql-5.7 無數據
從數據庫 192.168.209.13 Centos7/redhat7/ mysql-5.7 無數據

需求:搭建兩臺mysql服務器,一臺作為主服務器192.168.209.12,一臺作為從服務器192.168.209.13,主服務器進行寫操作,從服務器進行操作。
說明://分別在主從兩臺服務器上安裝mysql-5.7版本

                  ** 主服務器和從服務器上分別都要安裝mysql**
********************************mysql安裝****************************
//關閉防火墻及selinux
//安裝依賴 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//創建用戶和組
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下載二進制格式的mysql軟件包
[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//解壓軟件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/

//創建軟連接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

//修改目錄/usr/local/mysql的屬主屬組
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql

//添加環境變量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH 

//建立數據存放目錄
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  19 13:20 data

//初始化數據庫 註意這個命令後會生成臨時密碼 要記住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//配置mysql
//軟連接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v

//生成配置文件
[root@lanzhiyong ~]# cat > /etc/my.cnf << EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

//配置服務啟動腳本
[root@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld

//啟動mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]#  ss -antl
LISTEN      0      80                  :::3306                            :::*     

//修改密碼 使用臨時密碼登入
[root@lanzhiyong ~]# mysql -uroot -p
jd?ajfrKY4pQ 這是以上步驟的臨時密碼
mysql> set password = password(‘lanzhiyong‘);

#############查看主庫有哪些庫###################3
[root@lanzhiyong ~]# mysql -uroot -p
Enter password:   //上面設置的密碼

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

#####################查看從庫有哪些庫#############
[root@lan ~]# mysql -uroot -p
Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)

###############現在在主服務上添加四個數據庫########
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lan                |
| lanzhiyong         |
| mysql              |
| performance_schema |
| sys                |
| zhi                |
+--------------------+
7 rows in set (0.00 sec)

//進入lanzhiyong數據庫裏添加一個lanzhi表
mysql> use lanzhiyong;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_lanzhiyong |
+----------------------+
| lanzhi               |
+----------------------+
1 row in set (0.00 sec)

//查看表的內容
mysql> select * from lanzhi;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | lan  |   10 |
|  2 | cs   |   90 |
|  3 | lol  |   66 |
|  4 | pp   |   33 |
+----+------+------+
4 rows in set (0.00 sec)

//全備主庫
//先退出之前的數據庫,全備主庫時需要另外打開一個終端,再開一臺192.168.209.12 的服務器,給數據庫加上讀鎖,避免在備份期間與其他人在寫入導致數據不一致

   //加上讀鎖之後只能讀不能寫
 mysql> flush tables with read lock;
 Query OK, 0 rows affected (0.02 sec)

 //備份主庫將備份文件傳送到從庫
[root@lanzhiyong ~]# mysqldump -uroot -p --all-databases > all.sql

[root@lanzhiyong ~]# scp all.sql [email protected]:/root/
The authenticity of host ‘192.168.209.13 (192.168.209.13)‘ can‘t be established.
ECDSA key fingerprint is                SHA256:lOImReX4QGLGm5Qibnn4osotw9PoMtSRGLRaK1JAs4w.
ECDSA key fingerprint is    MD5:e4:1a:5f:28:d1:e0:2a:28:50:1a:1e:9c:cd:23:03:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.209.13‘ (ECDSA) to the   list of known hosts.
[email protected]‘s password:
all.sql                                                        100%  784KB      24.4MB/s   00:00

//解除主庫的鎖表狀態 
  mysql> quit
Bye 

//從庫 登入先 再讀取主庫傳來的備份
[root@lan ~]# mysql -uroot -p
Enter password:
mysql> source all.sql;
#讀取了之後 主庫的所有數據在從庫上確保一致

//在主數據庫裏創建一個同步賬號授權給從數據庫使用
mysql> create user ‘repl‘@‘192.168.209.13‘ identified by  ‘repl123‘;
Query OK, 0 rows affected (0.01 sec)

//在主數據庫授權復制給從數據庫
mysql> grant replication slave on *.* to ‘repl‘@‘192.168.209.13‘;
Query OK, 0 rows affected (0.00 sec)

//刷新權限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

 ##############配置主數據庫###################
[root@lanzhiyong ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
log-error=/opt/data/mysql.log    //添加錯誤日誌目錄
user = mysql
skip-name-resolve

 #replication
log-bin=mysql_bin     //添加啟用binlog日誌
server-id=3                //添加數據庫服務器唯一標識符,主庫的server-id值必須比從庫的大
[root@lanzhiyong ~]# service mysqld restart
[root@lanzhiyong ~]# ss -antl
  State      Recv-Q Send-Q   Local Address:Port                  Peer     Address:Port              
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128                 :::22                              :::*                  
LISTEN     0      100                ::1:25                              :::*                  
LISTEN     0      80                  :::3306                            :::*  

//查看主庫的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+---- ---------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

##################配置從數據庫##################
[root@lan ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
log-error=/opt/data/mysql.log      //添加錯誤日誌目錄
user = mysql
skip-name-resolve

#replication
server-id=5                //添加從庫的唯一標識符,從庫的server-id值必須小於主庫的值
relay-log=mysql_relay_log       //添加中繼日誌文件

[root@lan ~]# service mysqld restart
[root@lan ~]# ss -antl
    State      Recv-Q Send-Q   Local Address:Port                  Peer     Address:Port              
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128                 :::22                              :::*                  
LISTEN     0      100                ::1:25                              :::*                  
LISTEN     0      80                  :::3306                            :::*     

//從庫配置並啟動從復制

mysql> change master to master_host=‘192.168.209.12‘,
    master_user=‘repl‘,
    master_password=‘repl123‘,
    master_log_file=‘mysql_bin.000001‘,
    master_log_pos=154;   
  Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

//查看從服務器狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.209.12
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay_log.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_bin.000002
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: 154
Relay_Log_Space: 740
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: 3
Master_UUID: 3bcb9f5a-b269-11e8-aeca-000c2947a37d
Master_Info_File: /opt/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log;   waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

#######主庫插入記錄驗證#########

//在主庫裏的lanzhiyong庫裏lanzhi表裏添加幾條記錄,驗證是否能同步
mysql> select from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
+----+------+------+
4 rows in set (0.01 sec)
mysql> insert into lanzhi (id,name,age) values (5,‘AA‘,23),(6,‘BB‘,45),(7,‘CC‘,100);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings:
mysql> select
from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
| 5 | AA | 23 |
| 6 | BB | 45 |
| 7 | CC | 100 |
+----+------+------+
7 rows in set (0.00 sec)

############從庫看是否同步###########3
mysql> select * from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
| 5 | AA | 23 |
| 6 | BB | 45 |
| 7 | CC | 100 |
+----+------+------+
7 rows in set (0.01 sec)

Linux中詳細部署mysql主從