CentOS 6.7 安裝 MySQL 5.6 思路整理

源代碼及兼容包

源代碼包下載:官網 > Downloads > MySQL Community Edition (GPL) > MySQL Community Server (GPL) > Select Platform > Source Code > Generic Linux (Architecture Independent), Compressed TAR Archive (mysql-5.6.29.tar.gz) > Download] > No thanks, just start my download.html

兼容包下載:官網 > Downloads > MySQL Community Edition (GPL) > MySQL Community Server (GPL) > Select Platform > Linux Generic > Linux - Generic (glibc 2.5) (x86, 32-bit), RPM Package Compatibility Libraries (MySQL-shared-compat-5.6.29-1.linux_glibc2.5.i386.rpm) > Download > No thanks, just start my download.mysql

官網提供了一系列的兼容包(可在下載頁面 Linux Generic 分支中查看),主要是爲了解決新版本的 MySQL 向下兼容的問題,好比 MySQL-shared-compat-xxx.rpm 包是爲了解決 mysql-libs 與新版 MySQL 衝突的問題。linux

以 CentOS 6.7 爲例,mysql-libs 默認被其餘一些軟件所依賴,好比 postfixcrontabs 等。通常,咱們在安裝前都先使用 rpm -qa | grep mysql 查看系統都已經安裝了那些 MySQL 的軟件。而後,對這些軟件進行卸載後,再進行 MySQL 的安裝。可是,當咱們使用 yum uninstall mysql-libs 時,會被要求把所依賴的軟件包(postfixcrontabs)一塊兒刪除,這並非咱們但願的。由於咱們不但願卸載這兩款軟件,而只是卸載 mysql-libssql

不卸載 mysql-libs,直接安裝 MySQL 行不行?根據個人測試結果來看,是不行的。由於 mysql-libs 安裝後,默認配置文件會存放在 /etc/my.conf。若是此時,咱們把 MySQL 都安裝完畢(假如安裝目錄爲默認的 /usr/local/mysql),那麼 MySQL 的配置文件會存放在 /etc/usr/local/mysql/my.cnf,根據 mysqld 服務器啓動讀取配置文件的順序看(見下表),/etc/my.conf 將會是第一個讀取的配置文件,很顯然,這會直接影響到 MySQL 的啓動和運行。shell

On Unix, Linux and OS X, MySQL programs read startup options from the following files, in the specified order (top files are read first, later files take precedence). 出處數據庫

MySQL 啓動時,讀取配置文件的順序以下:安全

File Name Purpose
/etc/my.cnf Global options
/etc/mysql/my.cnf Global options
SYSCONFDIR/my.cnf Global options
$MYSQL_HOME/my.cnf Server-specific options defaults-extra-file The file specified with --defaults-extra-file=file_name, if any
~/.my.cnf User-specific options
~/.mylogin.cnf Login path options

那如何才能夠實現,即可以知足其餘軟件對 mysql-libs 的依賴須要,也可以不影響到新安裝的 MySQL 呢? 爲此,官網提供了 MySQL-shared-compat-xxx.rpm 兼容包,用戶可使用 rpm 或 yum 安裝該軟件包。對此,官網是這樣描述的:服務器

MySQL-shared-compat can safely be installed alongside mysql-libs because libraries are installed to different locations. Therefore, it is possible to install MySQL-shared-compat first, then manually remove mysql-libs before continuing with the installation. After mysql-libs is removed, the dynamic linker stops looking for the client library in the location where mysql-libs puts it, and the library provided by the MySQL-shared-compat package takes over. 出處app

安裝環境及依賴關係

編譯安裝時,所需工具:ide

  • CMake

  • gcc >= 4.2.1

  • make >= 3.75

編譯安裝時,所需依賴軟件包:

  • ncurses-devel

  • bison [可選]

自 MySQL 5.5 開始,MySQL 源代碼的配置過程再也不使用 GUN autotools (Configure),而是使用 CMake工具(參考)。

ncurses-devel 在 MySQL 安裝過程當中是必須存在的一個庫,不然將在執行 cmake 過程當中,出現如下錯誤:

-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.

bison 是可選的一個庫,它不會影響到 MySQL 的正常編譯和安裝過程。該庫只要求在安裝 MySQL 開發版源代碼(development source code)時安裝上。可是從咱們對 MySQL 源碼進行編譯的過程中發現,它在編譯完成後報一個警告。把 bison 安裝上只是爲了防止出現警告,減小沒必要要的困惑。

Warning: Bison executable not found in PATH

在安裝這些工具和軟件包前,咱們建議先檢查該軟件包是否已經存在以及版本是否知足

rpm -q PACKAGE
yum list installed | grep PACKAGE

使用 yum 進行安裝和更新

yum install PACKAGE
yum update PACKAGE

安裝方法

在工具以及依賴軟件都安裝好後,便可進行安裝。下面以以簡單的方式展現了整個 MySQL 安裝過程,包括了編譯安裝、初始化數據目錄、添加啓動腳本。若是你但願修改數據庫默認編碼爲 utf8 等等,稍後咱們將會介紹 cmake 的相關參數,以實現更好定製 MySQL 的安裝。

# Preconfiguration setup
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> cmake .
shell> make
shell> make install
# End of source-build specific instructions
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

首先,咱們添加了 mysql 用戶及用戶組,並把 /usr/local/mysql 目錄的全部者和所屬組都修改成 mysql。建立該用戶的目的是爲了,咱們須要有一個用戶啓動和管理 mysql 服務器,同時在稍後執行數據目錄初始化時,也會以該用戶身份執行。

實際上,在數據目錄初始化後,咱們把 /usr/local/mysql 目錄的全部者修改成了 root ,而只是把 /usr/local/mysql/data 的全部者修改成 mysql。那是由於這樣的權限設置,就足以知足 mysql 用戶對 MySQL 數據庫服務器的平常管理.

執行 mysql_install_db 腳本實現數據目錄初始化的過程。實際上就是建立 data 目錄,而且建立 mysql 系統數據庫。該數據庫包含了 MySQL 服務器的用戶表、權限表等等。同時,在用戶表當中,默認已經建立了 root 和 匿名用戶(用戶名爲空),但密碼都爲空。下面的測試過程,咱們會使用到這兩個用戶。

cmake 是對源代碼進行配置的過程,至關於 ./configure。上面示例直接執行 cmake,沒有帶上任何選項,這是最簡單的一個配置方式。固然,咱們能夠像如下方式,給 cmake 帶上選項參數,以定製其行爲,更多參數選項請參考:2.9.4 MySQL Source-Configuration Options

cmake . -DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1

測試安裝結果

首先,可使用 mysqld_safe 啓動 MySQL 數據庫:

bin/mysqld_safe --user=mysql &

實際上,mysqld_safe 腳本最終仍是調用了 bin/mysqld 來啓動 MySQL 數據庫。你也能夠直接執行 mysqld 進行啓動,可是 mysqld_safe 有更多更爲安全的特徵,它也是官方所推薦的。好比說當 MySQL 服務器發生錯誤的時候,它會自動重啓 MySQL等等。官方文檔中這樣描述:

mysqld_safe is the recommended way to start a mysqld server on Unix. mysqld_safe adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log file. A description of error logging is given later in this section.

mysqld_safe tries to start an executable named mysqld... 出處

爲了測試數據庫服務器,咱們可使用如下一些腳本進行:

bin/mysqladmin version
bin/mysqladmin variables

若是你已經爲 root 用戶設置了密碼,那麼你應該這樣執行:

bin/mysqladmin -u root -p version
Enter password: (enter root password here)

先關閉服務器,而後試着再次重啓它,測試它是否運行正常。一樣的,若是 root 已經設置過密碼,那麼應該加上 -p 選項

bin/mysqladmin -u root shutdown
bin/mysqld_safe --user=mysql &

查看數據庫,並執行簡單的查詢腳本,以測試它的數據庫能正常訪問。

# 查看全部數據庫
bin/mysqlshow

# 查看指定數據庫 mysql
bin/mysqlshow mysql

# 執行查詢操做
bin/mysql -e "SELECT User, Host, plugin FROM mysql.user" mysql

咱們看到,MySQL 提供了不少工具腳本,讓咱們直接管理數據庫。那究竟怎麼樣使用了,你能夠查看官方文檔:Chapter 4 MySQL Programs

增強用戶帳號的安全性

因爲 MySQL 服務器安裝成功後,默認就建立了 root 和匿名用戶,可是他們的密碼都是空的。這個固然不是咱們所但願的。咱們能夠經過下面幾種方法給他們設置密碼:

  • 使用 SET PASSWORD 語法

  • 使用 UPDATE 語法

  • 使用 mysqladmin 客戶端命令行工具

好比說,使用 mysqladmin 設置 root 密碼:

bin/mysqladmin -u root password "new_password"

而匿名用戶,可使用 root 用戶先登陸 MySQL 服務器後,再對它使用 UPDATE 語法設置密碼:

shell> bin/mysql -u root -p
Enter password: (enter root password here)
mysql> UPDATE mysql.user SET Password = PASSWORD('new_password')
    ->     WHERE User = '';
mysql> FLUSH PRIVILEGES;

更多和更細的設置密碼的方法,以及更多安全方面的知識,請參考:2.10.4 Securing the Initial MySQL Accounts

服務開機自啓動

咱們將經過把 support-files/mysql.server 複製到 /etc/rc.d/init.d/ 系統服務啓動腳本目錄 下。而後經過 chkconfig 命令,把它設置爲隨開機自啓動等等。下面是操做的方法:

cp support-files/mysql.server /etc/init.d/mysql

chkconfig --add mysql

# 查看開機啓動服務列表
chkconfig --list

下次,咱們啓動和關閉 MySQL 服務器,就能夠經過如下命令進行:

service mysql start|stop|status ....

PATH 環境變量

設置環境變量 PATH 主要的做用是可以在 Shell 窗口中,可以直接執行各類各樣的 MySQL 管理命令,而不須要每次都寫上命令所在的前綴。實現這個目標的方法好多,而這裏,咱們是但願給全部的用戶均可以直接不須要目錄前綴就執行命令,那麼咱們採用在 /etc/profile.d/ 建立腳本文件的方式。

建立 mysql.sh 以支持 Bash Shell:

vi /etc/profile.d/mysql.sh

if ! echo ${PATH} | /bin/grep -q /usr/local/mysql/bin ; then
    PATH=/usr/local/mysql/bin:${PATH}
fi

:wq

建立 mysql.csh 支持 C shell 和 Tenex C shell:

vi /etc/profile.d/mysql.csh

if ( "${path}" !~ */usr/local/mysql/bin* ) then
    set path = ( /usr/local/mysql/bin $path )
endif

:wq

最後,咱們爲了可以讓這些腳本立刻生效,咱們可使用如下腳本實現:

source /etc/profile.d/mysql.sh

此時,你就能夠在 Shell 命令行中直接使用 mysql、mysqladmin、mysqlshow 等等命令了。同時,你能夠可使用 man 命令查看幫助文檔了:

man mysql

補充幾點

啓動腳本一共有三種,分別是 mysqldmysqld_safemysql.server,它們各自的做用是不同的。不管是使用 mysqld_safe 仍是 mysql.server,它最終仍是經過執行 mysqld 啓動 MySQL 的。上面我已經簡單解釋過 mysqld_safemysqld 之間的關係,這裏就再也不多說,其實個人建議是直接看官方手冊:4.3 MySQL Server and Server-Startup Programs 說得很明白了。關於 mysql.server 的做用,請參考官方這樣一番話:

MySQL distributions on Unix include a script named mysql.server, which starts the server using mysqld_safe. It can be used on systems such as Linux and Solaris that use System V-style run directories to start and stop system services. It is also used by the OS X Startup Item for MySQL. 出處

總結

MySQL 的安裝其實比較簡單,這得益於官網提供思路很是清晰的安裝文檔。本文只是但願記錄一下在我安裝過程當中,須要注意的一些地方和思路的整理。有什麼不對的地方,也但願你們可以指出,謝謝!

參考文獻

  1. MySQL 5.6 Reference Manual

  2. Installing MySQL Server

相關文章
相關標籤/搜索