Linux網站架構系列之Mysql----部署篇

weixin_34115824發表於2017-11-15

MySQL是一個關係型資料庫管理系統,由瑞典MySQL AB公司開發,目前屬於Oracle公司。MySQL所使用的SQL語言是用於訪問資料庫的最常用標準化語言。由於其體積小、速度快、總體擁有成本低,尤其是開放原始碼這一特點,一般中小型網站的開發都選擇MySQL作為網站資料庫。由於其社群版的效能卓越,搭配PHP和Apache可組成良好的開發環境。本篇將為大家講解mysql的簡單部署。

mysql的分為四個版本:

1
2
3
4
Alpha版:一般只在軟體開發公司內部執行,不對外公開。
Beta版:完成功能的開發和所有的測試工作之後的產品,不會存在較大的漏洞和BUG,並且邀請和提供給使用者體驗與測試,以便更全面地測試軟體的不足之處或存在的問題。
RC版:屬於生產環境釋出之前的一個小版本或稱候選版本,是測試Beta版本二收集到的BUG或不足之處,根據手機到的資訊而進行修復和完善之後的產品。
GA版本:軟體產品正式釋出的版本,也成生產版本的產品。

並且mysql為了更好發展將版本路線分為了三條。

1
2
3
第一條產品線:從5.0版本升級到5.1的系列版本,繼續完善與改進其使用者體驗和效能,同時增加新功能。
第二條產品線:為了更好的整合MySQL AB公司,社群和新功能。版本編號從5.4開始,目前發展到5.6。
第三條產品線:為了更好推廣MySQL Cluster 版本,從6.0版本開始,目前發展到7.3版本。

由於mysql不同產品線之間的編譯安裝方法還有差別,因此本篇將分別為大家講解mysql的5.1.7版本和5.6.13版本的簡單部署。

5.1.7版本

1
2
3
4
5
6
7
8
環境
CentOS6.4 x86_64位 採用最小化安裝,系統經過了基本優化
selinux為關閉狀態,iptables為無限制模式
ip:192.168.1.113/24
mysql版本:mysql-5.1.70
原始碼包存放位置:/server/tools
原始碼包編譯安裝位置:/etc/local/軟體名稱
資料庫存放位置:/mydata


一、準備工作

1、開發環境部署

1
2
[root@c64-web ~]# yum groupinstall "Development tools" "Server Platform Development" -y #安裝這兩個開發環境的軟體包組
[root@c64-web ~]# yum install pcre* -y #安裝pcre相容的正規表示式

2、建立使用者及目錄

1
2
3
[root@c64-web ~]# useradd -s /sbin/nologin -M mysql #建立mysql使用者,並加入mysql組,不建立家目錄,關閉登陸
[root@c64-web ~]# mkdir /mydata  #建立資料庫存放目錄
[root@c64-web ~]# chown -R mysql.mysql /mydata

3、下載原始碼包

1
2
[root@c64-web ~]# cd /server/tools
[root@c64-web tools]# wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.1/mysql-5.1.70.tar.gz


二、編譯安裝

準備工作已經做好了,現在我們就開始編譯安裝mysql。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@c64-web tools]# tar zxf mysql-5.1.70.tar.gz
[root@c64-web tools]# cd mysql-5.1.70
[root@c64-web mysql-5.1.70]# ./configure \
--prefix=/usr/local/mysql \     #設定mysql安裝路徑,預設為/usr/local
--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \  #指定Mysql socket檔案存放目錄
--localstatedir=/mydata \       #設定mysql的資料檔案存放位置
--enable-assembler \            #允許使用匯編模式(優化效能)
--enable-thread-safe-client \   #以執行緒方式編譯客戶端
--with-mysqld-user=mysql \      #指定MySQL執行的系統使用者
--with-big-tables \             #啟用大表支援
--without-debug \               #使用非debug模式
--with-pthread \                #強制使用pthread執行緒序庫編譯
--with-extra-charsets=complex \ #複雜字符集支援
--with-readline \               #使用系統readline代替捆綁副本。
--with-ssl \                    #啟用ssl加密
--with-embedded-server \        #構建嵌入式MySQL庫 (libmysqld.a)
--enable-local-infile \         #讓mysql支援從本地載入資料庫(預設關閉)
--with-plugins=partition        #mysql分割槽功能支援
--with-plugins=innobase \       #innobas儲存引擎支援
--with-mysqld-ldflags=-all-static \    #伺服器使用靜態庫(優化效能)
--with-client-ldflags=-all-static      #客戶端使用靜態庫(優化效能)
[root@c64-web mysql-5.1.70]# make && make install

執行了上面的操作之後,mysql-5.1.7版本就編譯安裝成功了。

下面為快速複製,編譯配置文字:

1
[root@c64-web mysql-5.1.70]# ./configure --prefix=/usr/local/mysql-5.1.70 --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock --localstatedir=/mydata --enable-assembler --enable-thread-safe-client --with-mysqld-user=mysql --with-big-tables --without-debug --with-pthread --with-extra-charsets=complex --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition --with-plugins=innobase --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static


三、啟動配置

1、建立目錄軟連結

mysql安裝完成之後,我們要先為編譯安裝好的目錄新增軟連結。此操作在生產環境中為重要調優引數,新增這條軟連結的目的有兩點:1、方便人類使用。2、便於以後升級版本。

1
2
[root@c64-web mysql-5.1.70]# cd /root
[root@c64-web ~]# ln -s /usr/local/mysql-5.1.70 /usr/local/mysql

2、建立其它相應目錄及許可權設定

1
2
[root@c64-web ~]# mkdir /mydata   #建立mysql資料檔案目錄
[root@c64-web ~]# chown -R mysql /mydata  #授權mysql使用者訪問mysql資料庫目錄

3、獲取Mysql主配置檔案並修改

由於mysql的主配置檔案,編譯安裝之後預設是沒有的。因此我們需要在mysql的編譯包中,選擇預支的*.cnf結尾的配置檔案將其複製到我們的/etc/目錄下。

1
2
3
4
5
6
7
8
9
[root@c64-web ~]# ll /server/tools/mysql-5.1.70/support-files/*.cnf
-rw-r--r-- 1 root root 4714 11月 6 02:25 /server/tools/mysql-5.1.70/support-files/my-huge.cnf
-rw-r--r-- 1 root root 19763 11月 6 02:25 /server/tools/mysql-5.1.70/support-files/my-innodb-heavy-4G.cnf
-rw-r--r-- 1 root root 4688 11月 6 02:25 /server/tools/mysql-5.1.70/support-files/my-large.cnf
-rw-r--r-- 1 root root 4699 11月 6 02:25 /server/tools/mysql-5.1.70/support-files/my-medium.cnf
-rw-r--r-- 1 root root 2467 11月 6 02:25 /server/tools/mysql-5.1.70/support-files/my-small.cnf  #中小型公司用這個即可
[root@c64-web mysql-5.1.70]# /bin/cp /server/tools/mysql-5.1.70/support-files/my-small.cnf /etc/my.cnf
修改mysql的主配置檔案/etc/my.cnf,新增如下1行
datadir=/mydata  #我們自定義的資料庫存放目錄

4、讓系統識別原始碼包安裝的軟體

a)將mysql的庫檔案路徑加入系統的庫檔案搜尋路徑中

方法一:直接做軟連結

1
[root@c64-web ~]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

方法二:利用ldconfig匯入系統庫(推薦)

1
2
[root@c64-web ~]# echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf
[root@c64-web ~]# ldconfig

b)輸出mysql的標頭檔案到系統標頭檔案

1
[root@c64-web ~]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

c)配置mysql命令全域性使用路徑

1
2
[root@c64-web ~]# echo "PATH=/usr/local/mysql/bin:$PATH" >>/etc/profile
[root@c64-web ~]# source /etc/profile

5、以mysql使用者的身份初始化資料庫並啟動

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@c64-web ~]# /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata --user=mysql  #初始化mysql資料庫檔案
WARNING: The host 'c64-web' 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 !   #此警告我們可以通過在/etc/hosts檔案中修改127.0.0.1後面的localhost為當前c64-web來消除。
Installing MySQL system tables...
131108 18:07:26 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.   #這裡的警告無需理會
OK
Filling help tables...
131108 18:07:27 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.   #這裡的警告無需理會
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:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h lhh password 'new-password'
#教你如何建立root使用者進入mysql資料庫的密碼
Alternatively you can run:
/usr/local/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 /usr/local/mysql /usr/local/mysql/bin/mysqld_safe &
#上面的即為開啟命令中的一種,後面的&為放入後臺執行的意思
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl
#這裡是給你說如何執行測試
Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!
#以上為初始化建立mysql資料庫檔案時產生的資訊。
[root@lhh mydata]# cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
[1] 7146
131108 18:50:37 mysqld_safe Logging to '/mydata/lhh.err'.
131108 18:50:37 mysqld_safe Starting mysqld daemon with databases from /mydata   #此處需要敲擊回車才能回到命令輸入介面。

mysql資料庫啟動的另外一種方法

1
2
3
4
5
[root@c64-web ~]# /bin/cp /server/tools/mysql-5.1.70/support-files/mysql.server /etc/init.d/mysqld    #拷貝mysql啟動指令碼mysql命令路徑
[root@c64-web ~]# chmod 700 /etc/init.d/mysqld      #使指令碼可執行
[root@c64-web ~]# /etc/init.d/mysqld start    #這是啟動資料庫方法之一
[root@c64-web ~]# /etc/init.d/mysqld stop     #關閉資料庫的方法
[root@c64-web ~]# killalll mysqld #關閉資料庫的另外一種方法

6、檢查mysql資料庫是否啟動:

1
2
[root@c64-web ~]# netstat -lnt|grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN

可以看到,我們的mysql服務已經啟動起來了。這裡如果發現3306埠沒起來,請tail -100 /usr/local/mysql/data/機器名.err 檢查日誌報錯進行除錯。

7、設定初始賬戶,並登陸後臺

在上面,初始化建立mysql資料庫的時候,已經給我們提到了如何給mysql資料庫賬號設定密碼,下面我們就進行操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@c64-web ~]# mysqladmin -u root password 123456   #設定密碼
[root@c64-web ~]# history -c  #設定之後注意清除歷史記錄,防止密碼洩露
[root@c64-web ~]# mysql -u root -p    #連線資料庫檢視
Enter password:   #這裡輸入剛才設定的密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.70 Source distribution
Copyright (c) 2000, 2013, 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 |
test |
+--------------------+
3 rows in set (0.00 sec)
mysql> select user();    #檢視現有使用者
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> quit   #退出
Bye

到此,我們mysql-5.1.17的部署就已經完成了,下面將是5.6.13的部署。

5.6.13版本

mysql的第二條產品線和第一條產品線的產品主要在資料儲存引擎和編譯安裝的方法上有點區別,因此這裡我就主要對編譯安裝的部分進行講解,其它地方都是大同小異的。

mysql-5.6.13原始碼包下載地址:http://mysql.ntu.edu.tw/Downloads/MySQL-5.6/mysql-5.6.13.tar.gz

一、更改編譯配置工具為cmake

注意下面mysql-5.6.x系列版本在編譯配置時,放棄了用./configure,進而使用到了cmake,因此我們需要先安裝cmake這個工具,下面才能執行編譯配置。

二、編譯引數更改

mysql-5.6.x系列版本綜合性的編譯引數詳解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@c64-web mysql-5.6.13]# cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \          #指定mysql安裝目錄
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \                #Unix socket檔案路徑,自定義此路徑防報錯
-DDEFAULT_CHARSET=gbk \                            #預設字元
-DDEFAULT_COLLATION=gbk_chinese_ci \               #校驗字元
-DEXTRA_CHARSETS=all \                             #安裝所有擴充套件字符集
-DWITH_MYISAM_STORAGE_ENGINE=1 \                   #安裝myisam儲存引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                 #安裝innodb儲存引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \                  #安裝archive儲存引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \                #安裝blackhole儲存引擎
-DWITH_MEMORY_STORAGE_ENGINE=1 \                   #安裝memory儲存引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1                  #安裝frderated儲存引擎
-DWITH_READLINE=1 \                                #快捷鍵功能
-DENABLED_LOCAL_INFILE=1 \                         #允許從本地匯入資料
-DMYSQL_DATADIR=/usr/local/mysql/data \            #資料庫存放目錄
-DMYSQL_USER=mysql \                               #資料庫屬主
-DMYSQL_TCP_PORT=3306 \                            #資料庫埠
-DSYSCONFDIR=/etc \                                #MySQL配輯檔案
-DWITH_SSL=yes                                     #資料庫SSL

下面為快捷複製執行命令:

1
[root@c64-web mysql-5.6.13]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=gbk -DDEFAULT_COLLATION=gbk_chinese_ci -DEXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/mydata -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/etc -DWITH_SSL=yes

執行之後,還是用make&&make install 來執行編譯安裝。

三、主配置檔案調整

在5.6.X系列的版本中,mysql的主配置檔案發生了變化。可以看到,mysql-5.6.13版本中取消了原來的my-huge.cnf,my-large.cnf,my-small.cnf模版,同時取而代之是my.cnf 或者是my-default.cnf一個配置模板。

1
2
3
[root@c64-web mysql-5.6.13]# ll /server/tools/mysql-5.6.13/support-files/*.cnf
-rw-r--r--. 1 root root 1126 Nov  8 01:37 /server/tools/mysql-5.6.13/support-files/my-default.cnf
[root@c64-web mysql-5.6.13]# /bin/cp /server/tools/mysql-5.6.13/support-files/my-default.cnf /etc/my.cnf

並且,模板檔案裡需要配置的地方很少。這是因為mysql-5.6.x系列將以前許多預設值設定不合理的引數都進行了調整,並且採用了一種out-of-box的思維,即有些值是固定的,有些值是啟動時,根據其他引數或者伺服器的配置來自動設定的,所以不需要初始指定很多值。

主要的區別就是這三點了,其它的操作和5.1.13基本一致,這裡就不再重複寫一遍了。希望大家本篇博文能對大家有幫助。











本文轉自 aaao 51CTO部落格,原文連結:http://blog.51cto.com/nolinux/1321079,如需轉載請自行聯絡原作者

相關文章