【MySQL】源碼編譯安裝和配置MySql 5.5.32(單實例)


【需求描述】

       在CentOS環境中,通過編譯源碼的方式,安裝並且配置“單實例”的MySQL5.5.32數據庫。

       MySQL的安裝目錄為:/application/mysql-5.5.32

       MySQL數據文件的安裝目錄為:/application/mysql-5.5.32/data

       MySQL默認的字符編碼為:UTF8

 

【環境參數】

VMware:10.0.1

Host:Win7

DB:MySql 5.5.32

編譯工具:cmake-2.8.8.tar.gz

其他依賴:ncurses-devel-5.7-3.20090208.el6.x86_64.rpm

Client:CentOS 6.5 x86_64,i686,最小化安裝(Minimal)。

在安裝自定義組件包時,

(1)Base System只安裝了Base,Compatibility Libraries和Debugging Tools。

(2)Development只安裝了Development Tools。

 

【軟件下載】

(1)MySql下載地址:

http://downloads.mysql.com/archives/community/?tpl=files&os=src&version=5.5.33

選擇“Generic Linux (Architecture Independent), Compressed TAR Archive”這個版本的來下載。

(2)下載cmake

cmake的百度雲盤連接:http://pan.baidu.com/s/1dDAWXsl 

CMake是一個跨平台的安裝(編譯)工具,可以用簡單的語句來描述所有平台的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project文件,能測試編譯器所支持的C++特性,類似UNIX下的automake。只是 CMake 的組態檔取名為 CmakeLists.txt。 

(3)下載ncurse-devel依賴包

ncurses-devel-5.7-3.20090208.el6.x86_64.rpm的百度雲盤分享:

http://pan.baidu.com/s/1pJpDRCJ

 

【具體步驟】

1、查看系統環境 

[root@centos65-mini ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@centos65-mini ~]#
[root@centos65-mini ~]#
[root@centos65-mini ~]# uname -r
2.6.32-431.el6.x86_64
[root@centos65-mini ~]#

 

2、安裝相關軟件包
(1) 配置、編譯和安裝cmake編譯工具
①在當前目錄,解壓縮cmake-2.8.8.tar.gz
# tar –zxvf cmake-2.8.8.tar.gz

②從當前目錄切換到cmake-2.8.8目錄中,
# cd cmake-2.8.8

③執行命令“#./configure”來配置cmake工具。
# ./configure

④編譯cmake工具
在cmake配置完成后,會有如下提示:

之后,執行gmake命令,來進行編譯工作,

# gmake

⑤檢查上一條“gmake”命令是否執行成功。
[root@centos65-mini cmake-2.8.8]# echo $?
0
解釋:“echo $?”表示顯示上一條命令執行的結果,若返回值為0,則表示上一條命令執行成功。

⑥安裝cmake工具
# gmake install

⑦安裝完成后,返回上一級目錄
# cd ..

 

(2) 安裝依賴包
1)安裝ncurse-devel依賴包
yum install ncurse-devel -y

若yun無法安裝,則去網上下載“ncurses-devel-5.7-3.20090208.el6.x86_64.rpm ”這個rpm包。

ncurses-devel-5.7-3.20090208.el6.x86_64.rpm的百度雲盤分享:http://pan.baidu.com/s/1pJpDRCJ

 

3、創建mysql用戶和mysql用戶組
[root@centos65-mini /]# groupadd mysql
[root@centos65-mini /]# useradd mysql -s /sbin/nologin -M -g mysql
命令說明:
①“–s /sbin/nologin”:表示禁止該用戶登錄,加強安全。
②“–g mysql”:指定屬於mysql組。
③“–M”:表示不創建用戶家目錄。

檢查創建的用戶
[root@centos65-mini ~]# tail -1 /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin

注意:是參數是“數字1”,而不是字母“l”。

 

4、解壓、編譯和安裝mysql軟件(重要)
(0)創建mysql的安裝目錄
[root@centos65-mini /]# mkdir –p /application/mysql-5.5.32

(1)在臨時目錄中,解壓縮源碼包:mysql-5.5.32.tar.gz
# tar –zxvf mysql-5.5.32 ./

(2)編譯mysql
切換到解壓縮后的mysql-5.5.32源碼目錄下,執行下列cmake命令,進行編譯(重要)
注意01:下述cmake命令可以重復使用。
注意02:目錄“/application/mysql-5.5.32”為用戶自定義的“mysql安裝目錄”。
注意03:提示:編譯時,可配置的選項有很多,具體可以參考結尾附錄或者官方文檔。
官方文檔mysql5.1_chs.chm,2.8. 使用源碼分發版安裝MySQL

下述命令很重要,可以復用。

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/application/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSET=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=on \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPL_ESTORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_ESTORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_ENBEDDED_SERVER=1 \
-DWITH_DEBUG=0

 

 

編譯完成后,有如下提示:

-- Build files have been written to: /root/zj-tools/mysql-5.5.32

[root@centos65-mini mysql-5.5.32]#

 

(3)安裝mysql

# make && make install

如果上述安裝過程中沒有出錯,則至此,就完成了MySql軟件的安裝。在安裝完成之后,就會在根目錄下出現一個“application”的文件夾,如下圖所示。

 

(4)創建一個Link文件
# ln –s /application/mysql-5.5.32/ /application/mysql


(5)提醒的事項
至此,就完成了以編譯源碼的方式安裝MySQL,安裝完MySQL數據庫后,還要進行各種配置,接下來就需要對MySql進行配置。
若要安裝“多實例”MySQL數據庫,則下列“配置、初始化單實例的MySQL數據文件和服務”筆記可以跳過去

 

5、配置、初始化單實例的MySQL數據文件和服務(單實例,重要)

5.1、查看默認模版配置文件
在MySql-5.5.32的源碼安裝目錄下有一個文件夾“support-files”,MySQL常用的各種模版配置文件都在該目錄下存放的。

[root@centos65-mini mysql-5.5.32]# pwd
/root/zj-tools/mysql-5.5.32
[root@centos65-mini mysql-5.5.32]# cd support-files/
[root@centos65-mini support-files]# ls
binary-configure magic mysql-log-rotate.sh
binary-configure.sh Makefile mysql.m4
build-tags my-huge.cnf mysql-multi.server.sh
CMakeFiles my-huge.cnf.sh mysql.server
cmake_install.cmake my-innodb-heavy-4G.cnf mysql.server.sh
CMakeLists.txt my-innodb-heavy-4G.cnf.sh mysql.server-sys5.sh
compiler_warnings.supp my-large.cnf MySQL-shared-compat.spec
config.huge.ini my-large.cnf.sh MySQL-shared-compat.spec.sh
config.huge.ini.sh my-medium.cnf mysql.spec
config.medium.ini my-medium.cnf.sh mysql.spec.sh
config.medium.ini.sh my-small.cnf ndb-config-2-node.ini
config.small.ini my-small.cnf.sh ndb-config-2-node.ini.sh
config.small.ini.sh mysql.5.5.32.spec plugins.files
CTestTestfile.cmake mysqld_multi.server RHEL4-SElinux
dtrace mysqld_multi.server.sh
MacOSX mysql-log-rotate
[root@centos65-mini support-files]#

 

5.2、選擇配置文件

測試環境選擇小的,生產環境可以根據硬件選擇,例如:my-innodb-heavy-4G.cnf
下列命令將MySQL源目錄下的配置文件拷貝到“/etc”目錄下。

[root@centos65-mini support-files]# cp my-small.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? y
[root@centos65-mini support-files]#

 

5.3、配置環境變量

# echo 'export PATH=/application/mysql/bin:$PATH' >> /etc/profile
# tail -l /etc/profile
# source /etc/profile
# echo $PATH

此處注意:“/application/mysql/bin”路徑與“$PATH”的先后順序問題。
要將“/application/mysql/bin”放置在“$PATH”的前邊,這樣的話當使用mysql相關的命令時,就會先去尋找編譯的MySql這個版本的命令。
因為,未來的生產系統中,有可能已經存在yum或者rpm方式安裝的mysql,這樣的話,當使用mysqldump等mysql命令時,會先去尋找已經存在的mysql命令。
即使用yum或者rpm安裝的客戶端會去連接“編譯版本的”MySql服務端,從而造成驢頭不對馬嘴的錯誤。

故障案例:
由於“/application/mysql/bin”路徑放置在“$PATH”后邊,而出現的棘手錯誤案例:
http://oldboy.blog.51cto.com/2561410/1122867

 

5.4、初始化數據文件(容易出錯的步驟)
# mkdir –p /application/mysql/data
創建mysql數據文件目錄,若在安裝mysql軟件的過程中已經創建了,則不需要創建該目錄。

修改數據文件所在目錄的用戶和用戶組
# chown –R mysql:mysql /application/mysql/data

調整/tmp權限,否則初始化會錯誤。
# chmod –R 1777 /tmp

安裝mysql數據庫文件,注意命令路徑:
mysql的初始化腳本
[root@centos65-mini scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

特別提示:如果用mysql5.0,5.1,省略指定datadir會出錯。
下列為初始化成功后,MySQL的一些自帶信息,其中包括設置和修改MySql的用戶密碼等。

 1 WARNING: The host 'centos65-mini' could not be looked up with resolveip.
 2 This probably means that your libc libraries are not 100 % compatible
 3 with this binary MySQL version. The MySQL daemon, mysqld, should work
 4 normally with the exception that host name resolving will not work.
 5 This means that you should use IP addresses instead of hostnames
 6 when specifying MySQL privileges !
 7 Installing MySQL system tables...
 8 OK
 9 Filling help tables...
10 OK
11 
12 To start mysqld at boot time you have to copy
13 support-files/mysql.server to the right place for your system
14 
15 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
16 To do so, start the server, then issue the following commands:
17 
18 /application/mysql/bin/mysqladmin -u root password 'new-password'
19 /application/mysql/bin/mysqladmin -u root -h centos65-mini password 'new-passwor                d'
20 
21 Alternatively you can run:
22 /application/mysql/bin/mysql_secure_installation
23 
24 which will also give you the option of removing the test
25 databases and anonymous user created by default.  This is
26 strongly recommended for production servers.
27 
28 See the manual for more instructions.
29 
30 You can start the MySQL daemon with:
31 cd /application/mysql ; /application/mysql/bin/mysqld_safe &
32 
33 You can test the MySQL daemon with mysql-test-run.pl
34 cd /application/mysql/mysql-test ; perl mysql-test-run.pl
35 
36 Please report any problems with the /application/mysql/scripts/mysqlbug script!
37 
38 [root@centos65-mini scripts]#

【拓展】
WARNING: The host 'centos65-mini' could not be looked up with resolveip.”
該警告信息對MySQL的功能不會造成影響,但是,可以通過以下設置來消除掉該警告。

原因:主機名與IP地址的映射問題。
解決方案:vim hosts,將本機的主機名添加到該文件中。

 

5.5、初始化並且配置MySql的服務mysqld
(1)配置MySQL的服務mysqld
進入MySql-5.5.32的源碼安裝目錄下的文件夾“support-files”,將其中的“mysql.server”拷貝到目錄“/etc/init.d/mysqld”下。
[root@centos65-mini support-files]# cp mysql.server /etc/init.d/mysqld
[root@centos65-mini support-files]#
[root@centos65-mini support-files]# chmod +x /etc/init.d/mysqld
[root@centos65-mini support-files]#

(2)啟動MySQL服務器
[root@centos65-mini support-files]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@centos65-mini support-files]#

(3)確認MySQL服務器是否啟動成功:
[root@centos65-mini support-files]# netstat -lntup | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 36586/mysqld
[root@centos65-mini support-files]#

 

5.6、設置用戶名和密碼,並且登錄MySql服務器
(1)設置MySql數據的用戶名和密碼
[root@centos65-mini support-files]# /application/mysql/bin/mysqladmin -u root password 'new-password'
[root@centos65-mini support-files]# /application/mysql/bin/mysqladmin -u root -h centos65-mini password 'new-passwor d'

(2)登錄到MySQL數據庫服務器
[root@centos65-mini support-files]# mysql -uroot -pnew-password

 

至此,MySQL的基本配置完成。


免責聲明!

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



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