一个专注于IT互联网运维的技术博客

LNMP之源码方式安装MySQL5.7

2018.12.22

WordPress使用MySQL数据库,MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品。MySQL是最流行的关系型数据库管理系统之一,在WEB应用方面,MySQL是最好的RDBMS(Relational Database Management System,关系数据库管理系统)应用软件。这里使用编译源代码的方式安装MySQL5.7。

安装环境

操作系统:CentOS Linux release 7.6.1810 (Core)
Kernel版本:3.10.0-957.1.3.el7.x86_64

准备编译环境

//安装依赖的软件包
[admin@elk_zabbix ~]$ sudo yum -y install gcc gcc-c++ make cmake ncurses-devel bison perl wget
//前面已经创建了安装目录
[admin@ityoudao ~]$ ll /u01
total 16
drwxr-xr-x 3 admin admin 4096 Dec 13 21:31 app
drwxr-xr-x 2 admin admin 4096 Dec 13 20:50 data
drwxr-xr-x 2 admin admin 4096 Dec 13 20:50 log
drwxr-xr-x 3 admin admin 4096 Dec 14 09:49 src

编译安装MySQL

下载MySQL源代码

这里下载的是带boost库的MySQL源代码:

[admin@ityoudao src]$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz
[admin@ityoudao src]$ tar -zxf mysql-boost-5.7.24.tar.gz

cmake配置

[admin@ityoudao src]$ cd mysql-5.7.24/
[admin@ityoudao mysql-5.7.24]$ cmake \
-DCMAKE_INSTALL_PREFIX=/u01/app/mysql-5.7.24/ \
-DMYSQL_DATADIR=/u01/data/mysql \
-DMYSQL_UNIX_ADDR=/var/lib/mysqld/mysql.sock \
-DWITH_SYSTEMD=1 \
-DSYSTEMD_PID_DIR=/var/run/mysqld \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=utf8,gbk \
-DWITH_ZLIB=bundled \
-DWITH_BOOST=boost \
-DWITH_DEBUG=0 
  • 可以使用cmake -L或者cmake -LH查看cmake选项。

遇到的问题和解决方法

1)Could not find (the correct version of) boost

问题详情:

-- 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:238 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:507 (INCLUDE)

解决方法:

下载自带boost库的MySQL源码包,文件名如mysql-boost-5.7.24.tar.gz,然后使用参数-DWITH_BOOST=boost指定使用当前目录下的boost库文件。如果下载的MySQL没有带boost库,文件名如mysql-5.7.24.tar.gz,可以自行下载当前版本MySQL所需的boost库文件,这里是boost_1_59_0,然后使用参数-DWITH_BOOST=path_name指定boost库文件所在位置,另外还可以使用参数-DDOWNLOAD_BOOST=bool设置MySQL自己下载boost库文件(默认-DDOWNLOAD_BOOST_TIMEOUT时间为600s,网速过慢的话会下载失败)。

2)Could NOT find Curses

问题详情:

-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:64 (MESSAGE):
  Curses library not found.  Please install appropriate package,remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:107 (FIND_CURSES)
  cmake/readline.cmake:197 (MYSQL_USE_BUNDLED_EDITLINE)
  CMakeLists.txt:541 (MYSQL_CHECK_EDITLINE)

解决方法:

安装ncurses-devel,移除CMakeCache.txt文件,然后重新cmake:

[admin@ityoudao mysql-5.7.24]$ sudo yum install ncurses-devel
[admin@ityoudao mysql-5.7.24]$ rm CMakeCache.txt 

3)CMake Error: CMAKE_CXX_COMPILER not set

问题详情:

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Performing Test HAVE_MISLEADING_INDENTATION - Failed
-- Performing Test HAVE_NO_BUILTIN_MEMCMP
CMake Error at /usr/share/cmake/Modules/CMakeCXXInformation.cmake:37 (get_filename_component):
  get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
  CMakeLists.txt:3 (PROJECT)

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Performing Test HAVE_NO_BUILTIN_MEMCMP - Failed
-- executable target mysqld debug_target /u01/src/debug/sql/mysqld

解决方法:

安装gcc和gcc-c++。

[admin@ityoudao mysql-5.7.24]$ sudo yum insatll gcc gcc-c++
[admin@ityoudao mysql-5.7.24]$ rm CMakeCache.txt 

注意,安装完gcc和gcc-c++之后必须删除CMakeCache.txt文件然后重新cmake,并且确保输出结果中至少HAVE_GCC_ATOMIC_BUILTINS或者HAVE_GCC_SYNC_BUILTINS的检查结果为Success:

-- Performing Test HAVE_GCC_ATOMIC_BUILTINS
-- Performing Test HAVE_GCC_ATOMIC_BUILTINS - Success
-- Performing Test HAVE_GCC_SYNC_BUILTINS
-- Performing Test HAVE_GCC_SYNC_BUILTINS - Success

如果没有移除CMakeCache.txt文件,cmake虽然成功,但是make的时候会报下面的错误:

[ 10%] Building C object mysys/CMakeFiles/mysys.dir/lf_alloc-pin.c.o
In file included from /u01/src/mysql-5.7.24/include/lf.h:20:0,
                 from /u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c:98:
/u01/src/mysql-5.7.24/include/my_atomic.h:65:4: error: #error Native atomics support not found!
 #  error Native atomics support not found!
    ^
In file included from /u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c:98:0:
/u01/src/mysql-5.7.24/include/lf.h: In function ‘lf_pin’:
/u01/src/mysql-5.7.24/include/lf.h:97:3: warning: implicit declaration of function ‘my_atomic_storeptr’ [-Wimplicit-function-declaration]
   my_atomic_storeptr(&pins->pin[pin], addr);
   ^
/u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c: In function ‘lf_pinbox_get_pins’:
/u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c:156:7: warning: implicit declaration of function ‘my_atomic_add32’ [-Wimplicit-function-declaration]
       pins= my_atomic_add32((int32 volatile*) &pinbox->pins_in_array, 1)+1;
       ^
/u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c:170:3: warning: implicit declaration of function ‘my_atomic_cas32’ [-Wimplicit-function-declaration]
   } while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
   ^
/u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c: In function ‘alloc_free’:
/u01/src/mysql-5.7.24/mysys/lf_alloc-pin.c:359:3: warning: implicit declaration of function ‘my_atomic_casptr’ [-Wimplicit-function-declaration]
   } while (!my_atomic_casptr((void **)(char *)&allocator->top,
   ^
make[2]: *** [mysys/CMakeFiles/mysys.dir/lf_alloc-pin.c.o] Error 1
make[1]: *** [mysys/CMakeFiles/mysys.dir/all] Error 2
make: *** [all] Error 2

4)Bison executable not found in PATH

问题详情:

CMake Warning at cmake/bison.cmake:20 (MESSAGE):
  Bison executable not found in PATH
Call Stack (most recent call first):
  libmysqld/CMakeLists.txt:194 (INCLUDE)

解决方法:

[admin@ityoudao mysql-5.7.24]$ sudo yum install bison
[admin@ityoudao mysql-5.7.24]$ rm CMakeCache.txt 

编译并安装MySQL

cmake完成之后,编译并安装MySQL:

[admin@ityoudao mysql-5.7.24]$ make && make install
[admin@ityoudao mysql-5.7.24]$ cd /u01/app && ln -s mysql-5.7.24 mysql

配置MySQL

修改PATH变量

这里仅修改当前用户的PATH变量,如果要对所有用户有效,可以直接修改/etc/profile文件。

[admin@ityoudao app]$ echo -e '\nexport PATH=/u01/app/mysql/bin:$PATH' >> ~/.bash_profile
[admin@ityoudao app]$ source ~/.bash_profile

创建MySQL组和用户

[admin@ityoudao app]$ sudo groupadd mysql
[admin@ityoudao app]$ sudo useradd -r -g mysql -s /sbin/nologin mysql

初始化数据库

[admin@ityoudao app]$ mysqld --initialize --user=mysql --basedir=/u01/app/mysql --datadir=/u01/data/mysql
2018-12-15T06:01:21.664024Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-12-15T06:01:21.737112Z 0 [Warning] One can only use the --user switch if running as root

2018-12-15T06:01:22.826401Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-15T06:01:22.964764Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-15T06:01:23.052274Z 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: d99674ec-002e-11e9-a453-00163e062417.
2018-12-15T06:01:23.061353Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-15T06:01:23.062085Z 1 [Note] A temporary password is generated for root@localhost: Ql#ilnw!k7oV
  • 注意,记住root用户临时密码root@localhost: Ql#ilnw!k7oV

如果有“[Warning] Changed limits: max_open_files: 1024 (requested 5000)”警告,修改limits.conf文件:

[admin@ityoudao ~]$ sudo vi /etc/security/limits.conf
...
mysql           soft    nofile          50000
mysql           hard    nofile          50000

修改/etc/my.cnf文件,解决“[Warning] Changed limits: table_open_cache: 431 (requested 2000)”和“[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.”警告。CentOS7默认安装了mariadb-libs软件包,需要先移除该软件包的两个配置文件:

[admin@ityoudao ~]$ rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[admin@ityoudao ~]$ rpm -qc mariadb-libs
/etc/my.cnf
/etc/my.cnf.d/mysql-clients.cnf
[admin@ityoudao ~]$ sudo mv /etc/my.cnf /etc/my.cnf.mariadb
[admin@ityoudao ~]$ sudo mv /etc/my.cnf.d/mysql-clients.cnf /etc/my.cnf.d/mysql-clients.cnf.mariadb
[admin@ityoudao ~]$ sudo vi /etc/my.cnf 
[client]
socket=/var/lib/mysqld/mysql.sock
[mysqld]
basedir=/u01/app/mysql
datadir=/u01/data/mysql
socket=/var/lib/mysqld/mysql.sock
table_open_cache = 2000
explicit_defaults_for_timestamp=true

创建MySQL相关目录并设置权限

/var/lib/mysqld、/var/run/mysqld、/u01/data/mysql分别存放MySQL的socket文件、pid文件和数据库数据文件:

[admin@ityoudao ~]$ sudo mkdir /var/{lib,run}/mysqld
[admin@ityoudao ~]$ sudo chown mysql:mysql /var/{lib,run}/mysqld
[admin@ityoudao ~]$ sudo chown -R mysql:mysql /u01/data/mysql

设置开机启动并启动MySQL

可以参考/u01/app/mysql/usr/lib/systemd/system/mysqld.service文件。

[admin@ityoudao ~]$ sudo vi /usr/lib/systemd/system/mysqld.service 
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/var/run/mysqld/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/u01/app/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
[admin@ityoudao ~]$ sudo systemctl enable mysqld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[admin@ityoudao ~]$ sudo systemctl start mysqld.service

运行安全设置脚本

[admin@ityoudao ~]$ mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: 		<== Ql#ilnw!k7oV
The existing password for the user account root has expired. Please set a new password.
New password: 
Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: n

Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
 ... skipping.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

测试MySQL

使用root用户登录测试: MySQL 安装成功

发表评论