本文共 14622 字,大约阅读时间需要 48 分钟。
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:
[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]# lsAuxiliary CMakeCPack.cmake CMakeGraphVizOptions.cmake CMakeLogo.gif CompileFlags.cmake CONTRIBUTING.rst CTestConfig.cmake DartConfig.cmake Help Modules Source Tests bootstrap CMakeCPackOptions.cmake.in CMakeLists.txtcmake_uninstall.cmake.in configure Copyright.txt CTestCustom.cmake.in doxygen.configLicenses 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 --help3:安装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/passwdmysql:x:500:500::/home/mysql:/sbin/nologin使用groups查看用户mysql所在的组[root@tsxs sbin]# groups mysqlmysql : 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]# lsBUILD cmake config.h.cmake dbug extra INSTALL-WIN-SOURCE libmysqld mysql-test packaging regexsql-bench strings unittest win BUILD-CMAKE CMakeLists.txt configure.cmake Docs include libeventlibservices mysys plugin scripts sql-common support-files VERSION zlib client cmd-line-utils COPYING Doxyfile-perfschemaINSTALL-SOURCE libmysql man mysys_ssl README sql storage tests vio
准备安装目录:
安装MySQL目录:/usr/local/mysql
[root@tsxs local]# mkdir -p /usr/local/mysqlMySQL数据库目录:/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]# lsbin 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 startStarting MySQL SUCCESS! [root@tsxs bin]# service mysqld restartShutting down MySQL. SUCCESS! Starting MySQL. SUCCESS! [root@tsxs bin]# service mysqld stopShutting down MySQL. SUCCESS!如果使用MySQL的一些命令需要配置,将这些命令加入到系统环境变量,让他生效::
MySQL的命令目录:
[root@tsxs ~]# cd /usr/local/mysql/bin/
[root@tsxs init.d]# vim /etc/profile
#MySQLexport MYSQL_HOME=/usr/local/mysqlexport 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_passwordport = 3306socket = /data/mysql.sock
# The MySQL server[mysqld]basedir = /usr/local/mysqldatadir = /dataport = 3306socket = /data/mysql.socklog-error = /data/mysql-error.logpid-file = /data/mysql.piduser = mysql启动MySQL客户端,第一次未设置密码:
[root@tsxs tmp]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.27 Source distributionCopyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quitBye
设置mysql为开机启动:
[root@Master init.d]# chkconfig --add mysqld [root@Master init.d]# chkconfig mysqld on[root@Master init.d]# chkconfig --list | grep mysqlmysqld 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 提供字符终端处理库,包括面板和菜单。
解压:
[root@tsxs installfiles]# tar zxvf ncurses-6.0.tar.gz [root@tsxs installfiles]# cd ncurses-6.0[root@tsxs ncurses-6.0]# lsaclocal.m4 announce.html.in config.guess configure.in dist.mk include Makefile.in MANIFEST mk-0th.awk mk-hdr.awk package README testAda95 AUTHORS config.sub convert_configure.pl doc INSTALL Makefile.os2 menu mk-1st.awk ncurses panel README.emx TO-DOANNOUNCE 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_passwordport = 3306socket = /data/mysql.sock# The MySQL server[mysqld]basedir = /usr/local/mysqldatadir = /dataport = 3306socket = /data/mysql.socklog-error = /data/mysql-error.logpid-file = /data/mysql.piduser = 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_编译参数参考:_STORAGE_ENGINE 也不是 -DWITHOUT_ _STORAGE_ENGINE 来指定存储引擎,该存储引擎将安装成共享模块式的。如果不是共享模块式的将排除在外。共享模块安装时必须使用INSTALL PLUGIN语句或--plugin-load才可以使用。4:其他选项之前MySQL的编译选项大多数都支持。新旧版本之间的安装选项映射成大写字母,删除选项前面破折号,中间字符间的破折号替换成下划线。如:--with-debug => WITH_DEBUG=1--with-embedded-server => WITH_EMBEDDED_SERVER5:调试配置过程使用configure编译完将生成config.log和config.status文件。使用cmake编译完在CMakeFiles目录下生成CMakeError.log 和CMakeOutput.log文件。
BUILD_CONFIG 采用官方发行版一致的编译参数CMAKE_BUILD_TYPE 指定产品编译说明信息 RelWithDebInfCMAKE_INSTALL_PREFIX 指定MySQL安装路径 /usr/local/mysqlCPACK_MONOLITHIC_INSTALL是否建立单个安装包文件 OFF 5.6.7DEFAULT_CHARSET MYSQL 默认字符集 latin1 5.6.7DEFAULT_COLLATION MYSQL 默认排序字符集 latin1_swedish_ci 5.6.7ENABLE_DEBUG_SYNC 是否启用同步调试功能 ON 5.6.7ENABLE_DOWNLOADS 是否下载可选文件 OFF 5.6.7ENABLE_DTRACE 是否包含 DTrace 支持 5.6.7ENABLE_GCOV 是否包含 Gcov 支持 5.5.14ENABLED_LOCAL_INFILE 是否启用本地 LOAD DATA INFILE OFF 5.6.7ENABLED_PROFILING 是否启用代码查询分析 ON 5.6.7INSTALL_BINDIR MySQL 主执行文件目录 PREFIX/bin 5.6.7INSTALL_DOCDIR 文档安装路径 PREFIX/docs 5.6.7INSTALL_DOCREADMEDIR 自述文件目录 PREFIX 5.6.7INSTALL_INCLUDEDIR 头文件目录 PREFIX/include 5.6.7INSTALL_INFODIR 关于信息文件目录 PREFIX/docs 5.6.7INSTALL_LAYOUT 选择预定义的安装 STANDALONE 5.6.7INSTALL_LIBDIR 库文件目录 PREFIX/lib 5.6.7INSTALL_MANDIR 手册页面目录 PREFIX/man 5.6.7INSTALL_MYSQLSHAREDIR 共享数据目录 PREFIX/share 5.6.7INSTALL_MYSQLTESTDIR mysql-test 目录 PREFIX/mysql-test 5.6.7INSTALL_PLUGINDIR 插件目录 PREFIX/lib/plugin 5.6.7INSTALL_SBINDIR 服务器超级用户执行文件目录 PREFIX/bin 5.6.7INSTALL_SCRIPTDIR 脚本目录 PREFIX/scripts 5.6.7INSTALL_SHAREDIR aclocal/mysql.m4 安装目录 PREFIX/share 5.6.7INSTALL_SQLBENCHDIR sql-bench 性能测试工具目录 PREFIX 5.6.7INSTALL_SUPPORTFILESDIR 扩展支持文件目录 PREFIX/support-files 5.6.7MYSQL_DATADIR 数据库存放目录 5.6.7MYSQL_MAINTAINER_MODE 是否启用MySQL的维护环境 OFF 5.6.7MYSQL_TCP_PORT TCP/IP 端口号 3306 5.6.7MYSQL_UNIX_ADDR Unix Socket 套接字文件 /tmp/mysql.sock 5.6.7SYSCONFDIR 选项配置文件目录 5.6.7WITH_COMMENT 编译环境发表评论 5.6.7WITH_DEBUG 是否包括调试支持 OFF 5.6.7WITH_EMBEDDED_SERVER 是否要建立嵌入式服务器 OFF 5.6.7WITH_xxx_STORAGE_ENGINE 静态编译xxx 存储引擎到服务器 5.6.7WITH_EXTRA_CHARSETS 额外的字符集,包括 all 5.6.7WITH_LIBWRAP 是否包括支持libwrap(TCP包装) OFF 5.6.7WITH_READLINE 使用捆绑的readline OFF 5.6.7WITH_SSL 是否支持SSL no 5.6.7WITH_ZLIB 是否支持Zlib system 5.6.7WITHOUT_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.comSupport MySQL by buying support/licenses at http://shop.mysql.comNew default config file was created as /usr/local/mysql/my.cnf andwill 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 mysqlroot 14043 1 0 19:30 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data --pid-file=/data/tsxs.pidmysql 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.pidroot 14191 958 0 19:35 pts/0 00:00:00 grep mysql备注图片1,2: