1. 程式人生 > >Linux下MySQL原始碼編譯安裝(eg:mysql-5.6.27.tar.gz )

Linux下MySQL原始碼編譯安裝(eg:mysql-5.6.27.tar.gz )

Linux下MySQL原始碼安裝(eg:mysql-5.6.27.tar.gz ):

1:準備MySQL原始碼安裝包:

mysql-5.6.27.tar.gz、cmake-3.3.2.tar.gz、ncurses-6.0.tar.gz

注:centos請安裝:

yum install -y ncurses-devel

yum install -y perl-Module-Install.noarch

網址:

環境:

mysql-5.6.27.tar.gz   

CentOS release 6.5 (Final)

注:MySQL原始碼安裝:從mysql5.5以後是通過cmake來編譯的安裝的,但cmake要依賴ncurses,所以你懂的,有需要依賴就裝給它!

注:所有錯誤和說明解釋、ncurses安裝,在備註附近,文章最下方有解決方案!

2:若未安裝,安裝cmake:

[[email protected] installfiles]# tar zxvf cmake-3.3.2.tar.gz 
[[email protected] installfiles]# cd cmake-3.3.2
[[email protected] cmake-3.3.2]# ls
Auxiliary  CMakeCPack.cmake CMakeGraphVizOptions.cmake  CMakeLogo.gif 
CompileFlags.cmake  CONTRIBUTING.rst  CTestConfig.cmake DartConfig.cmake  Help 
Modules  Source Tests bootstrap  CMakeCPackOptions.cmake.in  CMakeLists.txt
cmake_uninstall.cmake.in  configure Copyright.txt CTestCustom.cmake.in doxygen.config
Licenses  README.rst  Templates  Utilities
[
[email protected]
cmake-3.3.2]# ./bootstrap [[email protected] cmake-3.3.2]# make [[email protected] cmake-3.3.2]# make install

測試:輸入有關cmake的使用命令: 

注:CMake是一個跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程)。 可以一次:make && make install

[[email protected] installfiles]# cmake --version
[
[email protected]
installfiles]# cmake --help
3:安裝MySQL資料庫:

MySQL資料庫新增使用者和組:

檢視是否存在MySQL組:
[[email protected] home]# grep mysql /etc/group
不存在建立MySQL組:
[[email protected] home]# groupadd mysql
檢視是否存在MySQL使用者:
[[email protected] home]# grep mysql /etc/passwd
不存在建立MySQL使用者:
[[email protected] ~]# useradd mysql -g mysql -M -s /sbin/nologin 
檢查:
[[email protected] sbin]# grep mysql /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin
使用groups檢視使用者mysql所在的組
[[email protected] sbin]# groups mysql
mysql : mysql

注:-g:指定新使用者所屬的使用者組(group); -M:不建立根目錄;-s:定義其使用的shell,/sbin/nologin代表使用者不能登入系統。

注:也可以:

[[email protected] home]# useradd mysql -g mysql -d /usr/local/mysql -s /bin/sh
如果有,請修改:
[[email protected] bin]# usermod -s /bin/sh -d /usr/local/mysql -g mysql mysql

 -d:使用者的登入主目錄/usr/local/mysql,-s使用者的登入Shell是/bin/sh

可檢視:[[email protected] bin]# vim /etc/passwd

mysql:x:500:500::/usr/local/mysql:/bin/sh

解壓:

[[email protected] installfiles]# tar zxvf mysql-5.6.27.tar.gz  
[[email protected] installfiles]# cd mysql-5.6.27
[[email protected] mysql-5.6.27]# ls
BUILD  cmake  config.h.cmake  dbug extra           
INSTALL-WIN-SOURCE  libmysqld mysql-test  packaging  regex
sql-bench strings  unittest win BUILD-CMAKE  
CMakeLists.txt  configure.cmake  Docs include libevent
libservices  mysys  plugin scripts  sql-common  support-files 
VERSION zlib client cmd-line-utils COPYING Doxyfile-perfschema
INSTALL-SOURCE libmysql man mysys_ssl README sql storage tests vio

準備安裝目錄:

安裝MySQL目錄:/usr/local/mysql 

[[email protected] local]# mkdir -p /usr/local/mysql
MySQL資料庫目錄:/data
[[email protected] /]# mkdir /data  

注:-p引數:如果一個目錄的父目錄不存在,就建立它

編譯原始碼:

[[email protected] mysql-5.6.27]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_bin -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
注:MySQL安裝目錄
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql   
MySQL資料庫存放目錄     
-DINSTALL_DATADIR=/data  
使用utf8字元       
-DDEFAULT_CHARSET=utf8      
校驗字元                  
-DDEFAULT_COLLATION=utf8_bin     
安裝所有擴充套件字符集       
-DEXTRA_CHARSETS=all      
允許從本地匯入資料                      
-DENABLED_LOCAL_INFILE=1  
注:-D 標誌這種特性為大多數編譯器所支援,是傳遞給編譯器的主要引數,絕對路徑
編譯原始碼:
[[email protected] mysql-5.6.27]# make
安裝:
[[email protected] mysql-5.6.27]# make install

也可一次:make && make install

注:

[[email protected] /]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_bin -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
[[email protected] /]# make && make install
[[email protected] mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data --user=mysql

注:初始化日誌,為下,則初始化成功:附件日誌1!

安裝完畢,檢視安裝目錄 eg:/usr/local/mysql :
[[email protected] mysql]# ls
bin  COPYING  data  docs  include  INSTALL-BINARY  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
初始化MySQL資料庫(在安裝目錄下)
[[email protected] mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data --user=mysql
設定,配置檔案,copy(在安裝目錄下):
[[email protected] support-files]# cp -R my-default.cnf /etc/my.cnf

設定啟動服務(在安裝目錄下)

[[email protected] support-files]# cp -R mysql.server /etc/rc.d/init.d/mysqld
更改許可權(涉及到MySQL資料庫的檔案和目錄都屬於mysql使用者):

注:不僅減少了root使用者的登入和管理時間,同樣也提高了安全性。注:chown在授權目錄下,執行:

[[email protected] /]# chown -R mysql.mysql /data
[[email protected] mysql]# pwd
/usr/local/mysql
[[email protected] mysql]# chown -R mysql.mysql /usr/local/mysql/
[[email protected] etc]# chown -R mysql.mysql /etc/my.cnf
[[email protected] init.d]# chown -R mysql.mysql mysqld 
啟動MySQL服務:
[[email protected] bin]# ./mysqld_safe --user=mysql &
或者:
[[email protected] bin]# service mysqld start
Starting MySQL SUCCESS! 
[[email protected] bin]# service mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 
[[email protected] bin]# service mysqld stop
Shutting down MySQL. SUCCESS! 
如果使用MySQL的一些命令需要配置,將這些命令加入到系統環境變數,讓他生效::

MySQL的命令目錄:

[[email protected] ~]# cd /usr/local/mysql/bin/
[[email protected] init.d]# vim /etc/profile
#MySQL
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
[[email protected] init.d]# source /etc/profile
啟動MySQL客戶端client,並修改配置檔案:

在下邊MySQL配置檔案加入客戶端,登陸資訊:

[[email protected] bin]# vim /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /data/mysql.sock
# The MySQL server
[mysqld]
basedir = /usr/local/mysql
datadir = /data
port = 3306
socket = /data/mysql.sock
log-error = /data/mysql-error.log
pid-file = /data/mysql.pid
user = mysql
啟動MySQL客戶端,第一次未設定密碼:
[[email protected] tmp]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.27 Source distribution

Copyright (c) 2000, 2015, 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> quit
Bye

設定mysql為開機啟動:

[[email protected] init.d]# chkconfig --add mysqld 
[[email protected] init.d]# chkconfig mysqld on
[[email protected] init.d]# chkconfig --list | grep mysql
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

備註附件:

原始碼重新編譯:MySQL等原始碼重新編譯,在重新編譯時,需要清除舊的物件檔案和快取資訊。

# make clean
# rm -f CMakeCache.txt
以及安裝軟體涉及到其他檔案,eg:
# rm -rf /etc/my.cnf
安裝ncurses:

注:Ncurses 提供字元終端處理庫,包括面板和選單。在ubuntu安裝:apt-get install libncurses5-dev

解壓:

[[email protected] installfiles]# tar zxvf ncurses-6.0.tar.gz 
[[email protected] installfiles]# cd ncurses-6.0
[[email protected] ncurses-6.0]# ls
aclocal.m4  announce.html.in  config.guess  configure.in          dist.mk  include     
Makefile.in   MANIFEST  mk-0th.awk  mk-hdr.awk  package  README        test
Ada95       AUTHORS           config.sub    convert_configure.pl  doc      INSTALL     
Makefile.os2  menu      mk-1st.awk  ncurses     panel    README.emx    TO-DO
ANNOUNCE    c++               configure     COPYING               form     install-sh  
man           misc      mk-2nd.awk  NEWS        progs    README.MinGW  VERSION
按照你的系統環境製作安裝配置檔案:
[[email protected] ncurses-6.0]# ./configure 
編譯原始碼並且編譯NCURSES庫:
[[email protected] ncurses-6.0]# make
安裝編譯好的NCURSES庫:
[[email protected] ncurses-6.0]# make install
檢測:
[[email protected] bin]# man ncurses
或在目錄:

/usr/lib下查詢是否有libncurses.so或libncurses.a這個庫

問題回答:

問題1:

-- 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,

解決1:

安裝:ncurses,即可!

問題2:

[[email protected] init.d]# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql.pid).
[[email protected] init.d]# service mysqld status
 ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

注(警告):問題2,如果許可權和配置都覺得沒有問題,請先reboot下電腦,再解決~~~!

解決2:

以上安裝中,把安裝MySQL的目錄分配許可權給mysql使用者,並且在MySQL的全新配置檔案加入client的配置:

[[email protected] bin]# vim /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /data/mysql.sock
# The MySQL server
[mysqld]
basedir = /usr/local/mysql
datadir = /data
port = 3306
socket = /data/mysql.sock
log-error = /data/mysql-error.log
pid-file = /data/mysql.pid
user = mysql
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql/
[[email protected] /]# chown -R mysql.mysql /data
[[email protected] etc]# chown -R mysql.mysql /etc/my.cnf
[[email protected] init.d]# chown -R mysql.mysql mysqld 

注:若不能解決請關注:

1:檢視:vim /etc/passwd --->  mysql:x:500:500::/usr/local/mysql:/bin/sh  --->   下mysql使用者的許可權和路徑

2:是否安裝,ncurses-devel和perl-Module-Install.noarch

3:此原因是因為,/data/mysql.pid檔案的建立者為root,所以:關注1很重要!文章尾備註日誌2!

4:檢視,安裝路徑/etc/local/mysql和/data和/etc/my.cnf和啟動指令碼的擁有者和許可權(chown/chomd)

5:花了好多時間解決問題3,甚至查啟動日誌,好久,最後發現,重啟一下,reboot就好了!

備註圖片1,2:最後!

問題3:

Error3:
[[email protected] init.d]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解決3:

問題2的MySQL配置檔案client的那個配置加入即可,指定mysql.sock位置!

錯誤4:

[[email protected] cmake-3.3.2]# ./bootstrap 
---------------------------------------------
CMake 3.3.2, Copyright 2000-2015 Kitware, Inc.
C compiler on this system is: cc 
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /home/installfiles/resourceinstall-mysql/cmake-3.3.2/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------

解決4:

[[email protected] cmake-3.3.2]# yum -y install gcc-c++

問題5:

[[email protected] mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data --user=mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

解決5(centos最小安裝):

[[email protected] scripts]# yum install -y perl-Module-Install.noarch

問題6:

[[email protected] init.d]# service mysqld start
2015-11-08 00:57:58 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-11-08 00:57:58 0 [Note] /etc/init.d/mysqld (mysqld 5.6.27) starting as process 14599 ...
mysql啟動報錯:Segmentation fault

解決6:

[[email protected] data]# yum install -y ncurses-devel
注:然後刪除:/data和/usr/local/mysql/兩個目錄,重新解壓編譯安裝!別嫌麻煩!

或:

[[email protected] ~]# vim /etc/my.cnf
加入(此法並未解決):
[mysqld]
explicit_defaults_for_timestamp=true

注:

這是資料庫升級過程中,timestamp在5.6以前的資料庫中預設not null,如果沒有顯示宣告timestamp的預設值,那麼該列用全0的"0000-00-00 00:00:00"作為預設值
在5.6中後,時間值是不能為全0格式,新增explicit_defaults_for_timestamp 引數
新增之後,timestamp列可以設定預設為空,並且不填充0,如果填充了0,如果SQL_MODE為strict sql則會報錯除非顯示指定default current_time和on update current_time
加入explicit_defaults_for_timestamp=true後,建立表timestamp型別Default值已經預設null了

其他檔案說明:

1:cmake安裝選項
CMAKE_INSTALL_PREFIX值是安裝的基本目錄,其他cmake選項值是不包括字首,是相對路徑名,絕對路徑包括 CMAKE_INSTALL_PREFIX:MySQL的安裝目錄。如-DINSTALL_SBINDIR=sbin的絕對路徑是 /sbin
注:-D 標誌這種特性為大多數編譯器所支援,是傳遞給編譯器的主要引數,絕對路徑
2:MySQL儲存引擎選項
mysql儲存引擎是外掛式的,因此外掛控制選項可以指定那個儲存引擎安裝。
configure編譯外掛選項--with-plugins=csv,myisam,myisammrg,heap,innobase,
archive,blackhole在cmake中沒有直接對應的相同選項。對於csv,myisam,myisammrg,heap在cmake中是不需要明確指定儲存引擎的名稱,因為它們是強制性安裝。
3:可以使用以下選擇來安裝innodb,archive,blackhole儲存引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
注:1也可以使用on代替
如果既不是-DWITH_<ENGINE>_STORAGE_ENGINE 也不是 -DWITHOUT_<ENGINE>_STORAGE_ENGINE 來指定儲存引擎,該儲存引擎將安裝成共享模組式的。如果不是共享模組式的將排除在外。共享模組安裝時必須使用INSTALL PLUGIN語句或--plugin-load才可以使用。
4:其他選項
之前MySQL的編譯選項大多數都支援。新舊版本之間的安裝選項對映成大寫字母,刪除選項前面破折號,中間字元間的破折號替換成下劃線。如:
--with-debug => WITH_DEBUG=1
--with-embedded-server => WITH_EMBEDDED_SERVER
5:除錯配置過程
使用configure編譯完將生成config.log和config.status檔案。
使用cmake編譯完在CMakeFiles目錄下生成CMakeError.log 和CMakeOutput.log檔案。
編譯引數參考:
BUILD_CONFIG   採用官方發行版一致的編譯引數
CMAKE_BUILD_TYPE 指定產品編譯說明資訊   RelWithDebInf
CMAKE_INSTALL_PREFIX 指定MySQL安裝路徑  /usr/local/mysql
CPACK_MONOLITHIC_INSTALL是否建立單個安裝包檔案 OFF   5.6.7
DEFAULT_CHARSET  MYSQL 預設字符集  latin1   5.6.7
DEFAULT_COLLATION MYSQL 預設排序字符集  latin1_swedish_ci 5.6.7
ENABLE_DEBUG_SYNC 是否啟用同步除錯功能  ON   5.6.7
ENABLE_DOWNLOADS 是否下載可選檔案  OFF   5.6.7
ENABLE_DTRACE  是否包含 DTrace 支援     5.6.7
ENABLE_GCOV  是否包含 Gcov 支援     5.5.14
ENABLED_LOCAL_INFILE 是否啟用本地 LOAD DATA INFILE OFF   5.6.7
ENABLED_PROFILING 是否啟用程式碼查詢分析  ON   5.6.7
INSTALL_BINDIR  MySQL 主執行檔案目錄  PREFIX/bin  5.6.7
INSTALL_DOCDIR  文件安裝路徑   PREFIX/docs  5.6.7
INSTALL_DOCREADMEDIR 自述檔案目錄   PREFIX   5.6.7
INSTALL_INCLUDEDIR 標頭檔案目錄   PREFIX/include  5.6.7
INSTALL_INFODIR  關於資訊檔案目錄  PREFIX/docs  5.6.7
INSTALL_LAYOUT  選擇預定義的安裝  STANDALONE  5.6.7
INSTALL_LIBDIR  庫檔案目錄   PREFIX/lib  5.6.7
INSTALL_MANDIR  手冊頁面目錄   PREFIX/man  5.6.7
INSTALL_MYSQLSHAREDIR 共享資料目錄   PREFIX/share  5.6.7
INSTALL_MYSQLTESTDIR mysql-test 目錄   PREFIX/mysql-test 5.6.7
INSTALL_PLUGINDIR 外掛目錄   PREFIX/lib/plugin 5.6.7
INSTALL_SBINDIR  伺服器超級使用者執行檔案目錄 PREFIX/bin  5.6.7
INSTALL_SCRIPTDIR 指令碼目錄   PREFIX/scripts  5.6.7
INSTALL_SHAREDIR aclocal/mysql.m4 安裝目錄 PREFIX/share  5.6.7
INSTALL_SQLBENCHDIR sql-bench 效能測試工具目錄 PREFIX   5.6.7
INSTALL_SUPPORTFILESDIR 擴充套件支援檔案目錄  PREFIX/support-files 5.6.7
MYSQL_DATADIR  資料庫存放目錄      5.6.7
MYSQL_MAINTAINER_MODE 是否啟用MySQL的維護環境  OFF   5.6.7
MYSQL_TCP_PORT  TCP/IP 埠號   3306   5.6.7
MYSQL_UNIX_ADDR  Unix Socket 套接字檔案  /tmp/mysql.sock  5.6.7
SYSCONFDIR  選項配置檔案目錄     5.6.7
WITH_COMMENT  編譯環境發表評論     5.6.7
WITH_DEBUG  是否包括除錯支援  OFF   5.6.7
WITH_EMBEDDED_SERVER 是否要建立嵌入式伺服器  OFF   5.6.7
WITH_xxx_STORAGE_ENGINE 靜態編譯xxx 儲存引擎到伺服器    5.6.7
WITH_EXTRA_CHARSETS 額外的字符集,包括  all   5.6.7
WITH_LIBWRAP  是否包括支援libwrap(TCP包裝) OFF   5.6.7
WITH_READLINE  使用捆綁的readline  OFF   5.6.7
WITH_SSL  是否支援SSL  no   5.6.7
WITH_ZLIB  是否支援Zlib  system   5.6.7
WITHOUT_XXX_STORAGE_ENGINE 不編譯XXX儲存引擎到資料庫

備註日誌1:

Installing MySQL system tables...2015-11-15 18:18:41 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-11-15 18:18:41 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.27) starting as process 19263 ...
2015-11-15 18:18:41 19263 [Note] InnoDB: Using atomics to ref count buffer pool pages
………………
………………
The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

備註日誌2:

[[email protected] data]# ps -ef | grep mysql
root     14043     1  0 19:30 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data --pid-file=/data/tsxs.pid
mysql    14158 14043  0 19:30 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/tsxs.err --pid-file=/data/tsxs.pid
root     14191   958  0 19:35 pts/0    00:00:00 grep mysql

備註圖片1,2: