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

網址:

https://cmake.org/download/

ftp://invisible-island.net/ncurses/

http://ftp.kaist.ac.kr/mysql/Downloads/

環境:

mysql-5.6.27.tar.gz   

CentOS release 6.5 (Final)

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

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

2:若未安裝,安裝cmake:

[root@tsxs installfiles]# tar zxvf cmake-3.3.2.tar.gz 
[root@tsxs installfiles]# cd cmake-3.3.2
[root@tsxs 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
[root@tsxs cmake-3.3.2]# ./bootstrap 
[root@tsxs cmake-3.3.2]# make 
[root@tsxs cmake-3.3.2]# make install

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

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

[root@tsxs installfiles]# cmake --version
[root@tsxs installfiles]# cmake --help
3:安裝MySQL數據庫:

MySQL數據庫添加用戶和組:

查看是否存在MySQL組:
[root@tsxs home]# grep mysql /etc/group
不存在創建MySQL組:
[root@tsxs home]# groupadd mysql
查看是否存在MySQL用戶:
[root@tsxs home]# grep mysql /etc/passwd
不存在創建MySQL用戶:
[root@tsxs ~]# useradd mysql -g mysql -M -s /sbin/nologin 
檢查:
[root@tsxs sbin]# grep mysql /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin
使用groups查看用戶mysql所在的組
[root@tsxs sbin]# groups mysql
mysql : mysql

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

注:也可以:

[root@Master home]# useradd mysql -g mysql -d /usr/local/mysql -s /bin/sh
如果有,請修改:

[root@tsxs bin]# usermod -s /bin/sh -d /usr/local/mysql -g mysql mysql

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

可查看:[root@tsxs bin]# vim /etc/passwd

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

解壓:

[root@tsxs installfiles]# tar zxvf mysql-5.6.27.tar.gz  
[root@tsxs installfiles]# cd mysql-5.6.27
[root@tsxs 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 

[root@tsxs local]# mkdir -p /usr/local/mysql
MySQL數據庫目錄:/data

[root@tsxs /]# mkdir /data  

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

編譯源代碼:

[root@tsxs 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 標誌這種特性爲大多數編譯器所支持,是傳遞給編譯器的主要參數,絕對路徑
編譯源代碼:

[root@tsxs mysql-5.6.27]# make
安裝:
[root@tsxs mysql-5.6.27]# make install

也可一次:make && make install

注:

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

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

安裝完畢,查看安裝目錄 eg:/usr/local/mysql :

[root@tsxs mysql]# ls
bin  COPYING  data  docs  include  INSTALL-BINARY  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
初始化MySQL數據庫(在安裝目錄下)
[root@tsxs mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data --user=mysql
設置,配置文件,copy(在安裝目錄下):

[root@tsxs support-files]# cp -R my-default.cnf /etc/my.cnf

設置啓動服務(在安裝目錄下)

[root@Master support-files]# cp -R mysql.server /etc/rc.d/init.d/mysqld

更改權限(涉及到MySQL數據庫的文件和目錄都屬於mysql用戶):

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

[root@tsxs /]# chown -R mysql.mysql /data
[root@Master mysql]# pwd
/usr/local/mysql
[root@Master mysql]# chown -R mysql.mysql /usr/local/mysql/
[root@tsxs etc]# chown -R mysql.mysql /etc/my.cnf
[root@tsxs init.d]# chown -R mysql.mysql mysqld 
啓動MySQL服務:

[root@tsxs bin]# ./mysqld_safe --user=mysql &
或者:

[root@tsxs bin]# service mysqld start
Starting MySQL SUCCESS! 
[root@tsxs bin]# service mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@tsxs bin]# service mysqld stop
Shutting down MySQL. SUCCESS! 
如果使用MySQL的一些命令需要配置,將這些命令加入到系統環境變量,讓他生效::

MySQL的命令目錄:

[root@tsxs ~]# cd /usr/local/mysql/bin/

[root@tsxs init.d]# vim /etc/profile
#MySQL
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
[root@tsxs init.d]# source /etc/profile
啓動MySQL客戶端client,並修改配置文件:

在下邊MySQL配置文件加入客戶端,登陸信息:

[root@tsxs 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客戶端,第一次未設置密碼:

[root@tsxs 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爲開機啓動:

[root@Master init.d]# chkconfig --add mysqld 
[root@Master init.d]# chkconfig mysqld on
[root@Master 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

解壓:

[root@tsxs installfiles]# tar zxvf ncurses-6.0.tar.gz 
[root@tsxs installfiles]# cd ncurses-6.0
[root@tsxs 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
按照你的系統環境製作安裝配置文件:

[root@tsxs ncurses-6.0]# ./configure 
編譯源代碼並且編譯NCURSES庫:

[root@tsxs ncurses-6.0]# make
安裝編譯好的NCURSES庫:

[root@tsxs ncurses-6.0]# make install
檢測:

[root@tsxs 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:

[root@tsxs init.d]# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql.pid).
[root@tsxs 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的配置:

[root@tsxs 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
[root@tsxs local]# chown -R mysql.mysql /usr/local/mysql/
[root@tsxs /]# chown -R mysql.mysql /data
[root@tsxs etc]# chown -R mysql.mysql /etc/my.cnf
[root@tsxs 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:
[root@tsxs 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:

[root@Master 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:

[root@Master cmake-3.3.2]# yum -y install gcc-c++

問題5:

[root@Master 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最小安裝):

[root@Master scripts]# yum install -y perl-Module-Install.noarch

問題6:

[root@Master 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:

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

或:

[root@Master ~]# 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:

[root@tsxs 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:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章