【原创】Ubuntu 15.04 上源码安装 mysql-5.7.16

源码安装

官网参考:这里

官网给出的 “basic installation command sequence” 如下:

预配置阶段

# Preconfiguration setup
shell> groupadd MysqL
shell> useradd -r -g MysqL -s /bin/false MysqL

问题:执行 -s /bin/false 命令的作用是?

源码安装阶段

# 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

问题:仅执行 cmake . 命令显然不够,如何定制化 cmake 参数?

安装后的处理

# Postinstallation setup
shell> cd /usr/local/MysqL
shell> chown -R MysqL .
shell> chgrp -R MysqL .
shell> bin/MysqL_install_db --user=MysqL    # Before MysqL 5.7.6
shell> bin/MysqLd --initialize --user=MysqL # MysqL 5.7.6 and up
shell> bin/MysqL_ssl_rsa_setup              # MysqL 5.7.6 and up
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

问题:执行 bin/MysqL_ssl_rsa_setup 命令的作用是?

小结

官网给出的安装命令过于粗糙:

  • 相关目录是在何时,通过何种方式被创建出来的没有说明;
  • 命令 chown 和 chgrp 明显可以进行简化组合;
  • bin/MysqLd_safe 在 5.7.xx 版本中已经被移除;
  • support-files/MysqL.server 用作 init.d 下的脚本用于维护 MysqL 服务的方式在已经支持 systemd 的系统中已经不合适了;

安装过程遇到的问题

可能用到的命令

# To list the configuration options
shell> cmake . -L   # overview
shell> cmake . -LH  # overview with help text
shell> cmake . -LAH # all params with help text
shell> ccmake .     # interactive display

# To prevent old object files or configuration information from being used,run these commands before re-running CMake
shell> make clean
shell> rm CMakeCache.txt

boost 版本低问题

在源码安装系统要求中提到:

The Boost C++ libraries are required to build MysqL (but not to use it). Boost 1.59.0 must be installed. To obtain Boost and its installation instructions,visit the official site. After Boost is installed,tell the build system where the Boost files are located by defining the WITH_BOOST option when you invoke CMake. For example:

shell> cmake . -DWITH_BOOST=/usr/local/boost_1_59_0

Adjust the path as necessary to match your installation.

这种方式采用了先安装后配置的方式;事实上,可以基于 cmake 直接进行下载安装;

root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16# cmake . -L
-- Running cmake version 3.0.2
-- Found Git: /usr/bin/git (found version "2.1.4")
-- Configuring with MAX_INDEXES = 64U
...
-- MysqL 5.7.16
-- Packaging as: MysqL-5.7.16-Linux-x86_64
-- Found /usr/include/boost/version.hpp
-- BOOST_VERSION_NUMBER is #define BOOST_VERSION 105500
CMake Warning at cmake/boost.cmake:266 (MESSAGE):
  Boost minor version found is 55 we need 59
Call Stack (most recent call first):
  CMakeLists.txt:455 (INCLUDE)

-- BOOST_INCLUDE_DIR /usr/include
-- LOCAL_BOOST_DIR
-- LOCAL_BOOST_ZIP
-- Could not find (the correct version of) boost.
-- MysqL currently requires boost_1_59_0

CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,it will download and unpack it (in that directory) for you.

  If you are inside a firewall,you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:269 (Could_NOT_FIND_BOOST)
  CMakeLists.txt:455 (INCLUDE)

-- Configuring incomplete,errors occurred!
See also "/root/workspace/WGET/MysqL-5.7.16/CMakeFiles/CMakeOutput.log".
See also "/root/workspace/WGET/MysqL-5.7.16/CMakeFiles/CMakeError.log".
-- Cache values
...
root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16#

可以看到,在 ubuntu 15.04 系统中已安装的 boost 版本不满足 “Boost minor version found is 55 we need 59” 要求,同时给出了修复办法 “You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST= ”;

基于 cmake 直接更新 boost 版本的方法如下:

root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16# cmake . -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp

之后就 ok 了;

systemd 相关 cmake 配置问题

参考信息:这里

关键信息:

  • As of MysqL 5.7.6,if you install MysqL using an RPM distribution on the following Linux platforms,server startup and shutdown is managed by systemd
  • To obtain systemd support if you install from a source distribution,configure the distribution using the -DWITH_SYSTEMD=1 CMake option.
  • manual server management using the systemctl command or service command.
  • On platforms for which systemd support is installed,scripts such as MysqLd_safe and the System V initialization script are not installed because they are unnecessary. For example,MysqLd_safe can handle server restarts,but systemd provides the same capability,and does so in a manner consistent with management of other services rather than using an application-specific program.
  • As of MysqL 5.7.13,on platforms for which systemd support is installed,systemd has the capability of managing multiple MysqL instances.
  • Because MysqLd_safe is not installed when systemd is used,options prevIoUsly specified for that program (for example,in an [MysqLd_safe] option group) must be specified another way.

-DSYstemD_PID_DIR=dir_name

The name of the directory in which to create the PID file when MysqL is managed by systemd. The default is /var/run/MysqLd; this might be changed implicitly according to the INSTALL_LAYOUT value.

This option is ignored unless WITH_SYstemD is enabled. It was added in MysqL 5.7.6.

-DSYstemD_SERVICE_NAME=name

The name of the MysqL service to use when MysqL is managed by systemd. The default is MysqLd; this might be changed implicitly according to the INSTALL_LAYOUT value.

This option is ignored unless WITH_SYstemD is enabled. It was added in MysqL 5.7.6.

-DWITH_SYstemD=bool

Whether to enable installation of systemd support files. By default,this option is disabled. When enabled,systemd support files are installed,and scripts such as MysqLd_safe and the System V initialization script are not installed. On platforms where systemd is not available,enabling WITH_SYstemD results in an error from CMake.

For more information about using systemd,see Section 2.5.10,“Managing MysqL Server with systemd”. That section also includes information about specifying options prevIoUsly specified in [MysqLd_safe] option groups. Because MysqLd_safe is not installed when systemd is used,such options must be specified another way.

This option was added in MysqL 5.7.6.

“c++: internal compiler error: Killed (program cc1plus)” 问题

在执行 make 命令时,会出现如下错误

root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16# make
...
[ 38%] Building CXX object sql/CMakeFiles/sql.dir/item_func.cc.o
[ 39%] Building CXX object sql/CMakeFiles/sql.dir/item_geofunc.cc.o
c++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.9/README.Bugs> for instructions.
sql/CMakeFiles/sql.dir/build.make:907: recipe for target 'sql/CMakeFiles/sql.dir/item_geofunc.cc.o' Failed
make[2]: *** [sql/CMakeFiles/sql.dir/item_geofunc.cc.o] Error 4
CMakeFiles/Makefile2:8443: recipe for target 'sql/CMakeFiles/sql.dir/all' Failed
make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2
Makefile:137: recipe for target 'all' Failed
make: *** [all] Error 2
root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16#

可以看到 cc1plus 是由于 “Out of memory” 的原因被杀了;

root@vagrant-ubuntu-trusty:~# dmesg |grep -i kill
[29269.576976] automount invoked oom-killer: gfp_mask=0x201da,order=0,oom_score_adj=0
[29269.577051]  [<ffffffff8117d04b>] oom_kill_process+0x22b/0x390
[29269.577294] Out of memory: Kill process 4094 (cc1plus) score 866 or sacrifice child
[29269.577339] Killed process 4094 (cc1plus) total-vm:965712kB,anon-RSS:453088kB,file-RSS:0kB
root@vagrant-ubuntu-trusty:~#

问题的根源在于,我的 ubuntu 15.04 虚拟机环境只分配了 500M 的内存,结果表明是不够用的;

在 stackoverflow 上找了个比较好的说明,摘录如下:

Most likely that is your problem. The problem above occurs when your system runs out of memory. In this case rather than the whole system falling over,the operating systems runs a process to score each process on the system. The one that scores the highest gets killed by the operating system to free up memory. If the process that is killed is cc1plus,gcc (perhaps incorrectly) interprets this as the process crashing and hence assumes that it must be a compiler bug. But it isn't really,the problem is the OS killed cc1plus,rather than it crashed.

If this is the case,you are running out of memory.

解决办法可以参考这里;摘录如下:

# 创建分区文件,大小 2G
dd if=/dev/zero of=/swapfile bs=1k count=2048000
# 生成 swap 文件系统
mkswap /swapfile
# 激活 swap 文件(立即激活而不是在启动时自动开启)
swapon /swapfile
# 在系统重启时自动挂载交换分区并启用,需要在 /etc/fstab 文件增加如下内容
/swapfile  swap  swap    defaults 0 0

删除之前创建的 swap 分区:

通过上面的办法确实能够避免由于 oom 而被杀的问题,与此同时,因为使用了 swap 所以编译速度也慢的可以~~

--explicit_defaults_for_timestamp 选项问题

在进行 MysqL 初始化时,会有如下告警信息;

root@vagrant-ubuntu-trusty:/usr/local/MysqL# bin/MysqLd --initialize --user=MysqL
2016-12-08T02:39:01.362885Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-12-08T02:39:01.739983Z 0 [Warning] InnoDB: New log files created,LSN=45790
2016-12-08T02:39:01.804289Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-12-08T02:39:01.875989Z 0 [Warning] No existing UUID has been found,so we assume that this is the first time that this server has been started. Generating a new UUID: 7a7039cf-bcef-11e6-a01e-0800274ac42f.
2016-12-08T02:39:01.878411Z 0 [Warning] Gtid table is not ready to be used. Table 'MysqL.gtid_executed' cannot be opened.
2016-12-08T02:39:01.880601Z 1 [Note] A temporary password is generated for root@localhost: 7rc!uu#i!%Az
root@vagrant-ubuntu-trusty:/usr/local/MysqL#

原则上该告警可以不解决;官网给出的参数说明如下:

explicit_defaults_for_timestamp

Introduced	5.6.6
Deprecated	5.6.6
Command-Line Format	--explicit_defaults_for_timestamp=#
System Variable	Name	explicit_defaults_for_timestamp
Variable Scope	Global,Session
Dynamic Variable	No
Permitted Values	Type	boolean
Default	FALSE
In MysqL,the TIMESTAMP data type differs in nonstandard ways from other data types:

TIMESTAMP columns not explicitly declared with the NULL attribute are assigned the NOT NULL attribute. (Columns of other data types,if not explicitly declared as NOT NULL,permit NULL values.) Setting such a column to NULL sets it to the current timestamp.

The first TIMESTAMP column in a table,if not declared with the NULL attribute or an explicit DEFAULT or ON UPDATE clause,is automatically assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.

TIMESTAMP columns following the first one,if not declared with the NULL attribute or an explicit DEFAULT clause,are automatically assigned DEFAULT '0000-00-00 00:00:00' (the “zero” timestamp). For inserted rows that specify no explicit value for such a column,the column is assigned '0000-00-00 00:00:00' and no warning occurs.

Those nonstandard behaviors remain the default for TIMESTAMP but as of MysqL 5.6.6 are deprecated and this warning appears at startup:

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
As indicated by the warning,to turn off the nonstandard behaviors,enable the explicit_defaults_for_timestamp system variable at server startup. With this variable enabled,the server handles TIMESTAMP as follows instead:

TIMESTAMP columns not explicitly declared as NOT NULL permit NULL values. Setting such a column to NULL sets it to NULL,not the current timestamp.

No TIMESTAMP column is assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. Those attributes must be explicitly specified.

TIMESTAMP columns declared as NOT NULL and without an explicit DEFAULT clause are treated as having no default value. For inserted rows that specify no explicit value for such a column,the result depends on the sql mode. If strict sql mode is enabled,an error occurs. If strict sql mode is not enabled,the column is assigned the implicit default of '0000-00-00 00:00:00' and a warning occurs. This is similar to how MysqL treats other temporal types such as DATETIME.

Note
explicit_defaults_for_timestamp is itself deprecated because its only purpose is to permit control over Now-deprecated TIMESTAMP behaviors that will be removed in a future MysqL release. When that removal occurs,explicit_defaults_for_timestamp will have no purpose and will be removed as well.

This variable was added in MysqL 5.6.6.

简单的处理方式如下

root@vagrant-ubuntu-trusty:/usr/local/MysqL# bin/MysqLd --initialize --user=MysqL --explicit_defaults_for_timestamp=true

关于 my.cnf 和 MysqLd.service 文件

my.cnf

root@vagrant-ubuntu-trusty:/usr/local/MysqL# cp support-files/my-default.cnf /etc/my.cnf

MysqLd.service

root@vagrant-ubuntu-trusty:/usr/local/MysqL# cp lib/systemd/system/MysqLd.service /usr/lib/systemd/system/

MysqLd 启动失败问题

启动 MysqL 服务,失败;

root@vagrant-ubuntu-trusty:/usr/local/MysqL# systemctl start MysqLd.service
Job for MysqLd.service Failed. See "systemctl status MysqLd.service" and "journalctl -xe" for details.
root@vagrant-ubuntu-trusty:/usr/local/MysqL#
root@vagrant-ubuntu-trusty:/usr/local/MysqL# systemctl status MysqLd.service
● MysqLd.service - MysqL Server
   Loaded: loaded (/usr/lib/systemd/system/MysqLd.service; disabled; vendor preset: enabled)
   Active: Failed (Result: start-limit) since Thu 2016-12-08 14:34:27 CST; 17s ago
  Process: 24527 ExecStart=/usr/local/MysqL/bin/MysqLd --daemonize --pid-file=/var/run/MysqLd/MysqLd.pid $MysqLD_OPTS (code=exited,status=1/FAILURE)
  Process: 24512 ExecStartPre=/usr/local/MysqL/bin/MysqLd_pre_systemd (code=exited,status=0/SUCCESS)

Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: MysqLd.service: control process exited,code=exited status=1
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: Failed to start MysqL Server.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: Unit MysqLd.service entered Failed state.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: MysqLd.service Failed.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: MysqLd.service holdoff time over,scheduling restart.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: start request repeated too quickly for MysqLd.service
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: Failed to start MysqL Server.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: Unit MysqLd.service entered Failed state.
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: MysqLd.service Failed.
root@vagrant-ubuntu-trusty:/usr/local/MysqL#

查看 systemd 的日志,可以看到服务启动的详细信息;

root@vagrant-ubuntu-trusty:/usr/local/MysqL# journalctl -xe
...
Dec 08 14:34:27 vagrant-ubuntu-trusty MysqLd[24527]: 2016-12-08T06:34:27.628759Z 0 [ERROR] /usr/local/MysqL/bin/MysqLd: Can't create/write to file '/var/run/MysqLd/MysqLd.pid' (Errcode: 2 - No such file or directory)
Dec 08 14:34:27 vagrant-ubuntu-trusty MysqLd[24527]: 2016-12-08T06:34:27.629114Z 0 [ERROR] Can't start server: can't create PID file: No such file or directory
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: MysqLd.service: control process exited,code=exited status=1
Dec 08 14:34:27 vagrant-ubuntu-trusty systemd[1]: Failed to start MysqL Server.
-- Subject: Unit MysqLd.service has Failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit MysqLd.service has Failed.
--
-- The result is Failed.
...

从上面可以看出,失败的原因是由于 “Can't create/write to file '/var/run/MysqLd/MysqLd.pid'” ;

可以通过如下命令解决

root@vagrant-ubuntu-trusty:/usr/local/MysqL# mkdir -p /var/run/MysqLd
root@vagrant-ubuntu-trusty:/usr/local/MysqL# chown MysqL:MysqL /var/run/MysqLd

再次重新启动,成功;

root@vagrant-ubuntu-trusty:/usr/local/MysqL# systemctl start MysqLd.service
root@vagrant-ubuntu-trusty:/usr/local/MysqL# systemctl status MysqLd.service
● MysqLd.service - MysqL Server
   Loaded: loaded (/usr/lib/systemd/system/MysqLd.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2016-12-08 14:44:33 CST; 17s ago
  Process: 24760 ExecStart=/usr/local/MysqL/bin/MysqLd --daemonize --pid-file=/var/run/MysqLd/MysqLd.pid $MysqLD_OPTS (code=exited,status=0/SUCCESS)
  Process: 24745 ExecStartPre=/usr/local/MysqL/bin/MysqLd_pre_systemd (code=exited,status=0/SUCCESS)
 Main PID: 24764 (MysqLd)
   CGroup: /system.slice/MysqLd.service
           └─24764 /usr/local/MysqL/bin/MysqLd --daemonize --pid-file=/var/run/MysqLd/MysqLd.pid

Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.384861Z 0 [Note] InnoDB: Loading buffer pool(s) from /usr/local/MysqL/data/ib_buffer_pool
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.386295Z 0 [Note] InnoDB: Buffer pool(s) load completed at 161208 14:44:33
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.386705Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.386936Z 0 [Note] IPv6 is available.
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.387219Z 0 [Note]   - '::' resolves to '::';
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.387398Z 0 [Note] Server socket created on IP: '::'.
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.401092Z 0 [Note] Event Scheduler: Loaded 0 events
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: 2016-12-08T06:44:33.403270Z 0 [Note] /usr/local/MysqL/bin/MysqLd: ready for connections.
Dec 08 14:44:33 vagrant-ubuntu-trusty MysqLd[24760]: Version: '5.7.16'  socket: '/tmp/MysqL.sock'  port: 3306  Source distribution
Dec 08 14:44:33 vagrant-ubuntu-trusty systemd[1]: Started MysqL Server.
root@vagrant-ubuntu-trusty:/usr/local/MysqL#

关于 MysqL_secure_installation 命令

详细信息参考官网文章这里

This program enables you to improve the security of your MysqL installation in the following ways:

  • You can set a password for root accounts.
  • You can remove root accounts that are accessible from outside the local host.
  • You can remove anonymous-user accounts.
  • You can remove the test database (which by default can be accessed by all users,even anonymous users),and privileges that permit anyone to access databases with names that start with test_.

需要注意的是,若要允许远端机器进行访问,则需要在执行该命令时进行相关设置;之后还需要为 MysqL 添加远程访问账号和密码;

MysqL> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

最终用于生成 Makefile 文件的 cmake 指令

root@vagrant-ubuntu-trusty:~/workspace/WGET/MysqL-5.7.16# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL -DMysqL_DATADIR=/usr/local/MysqL/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp -DWITH_SYstemD=1 -DENABLE_DOWNLOADS=1

详细 cmake 配置说明见这里

其它有用的只言片语:

  • Systemd replaces SysV and upstart initialization systems and is the default init system in most modern Linux distributions.
  • MysqL 支持 systemd 的较早版本中(5.7.6 之前的版本),存在如下限制和问题:
    • Use of ping tricks to check whether MysqLd is ready to serve client connections.
    • Though systemd had superior process control for automatic restarts,MysqLd_safe was still used to identify abnormal MysqLd termination and do automatic restarts.
    • There was no auto-detection of systemd configuration paths to generate service unit files for varIoUs distributions.
    • There was no integration with our CMake build system.

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...