centos7-编译安装MySQL5.7
实验准备:虚拟机 centos77
**MySQL5.7 源码包:**https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31.tar.gz
依赖boost_1_59_0 http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
前期准备:虚拟机建议4G内存以上,处理器尽可能多些
1、安装相关依赖库文件
[root@77 ~]# yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
2、创建用户和数据目录
[root@77 ~]# groupadd -r -g 306 mysql
[root@77 ~]# useradd -r -g 306 -u 306 -d /data/mysql mysql
[root@77 ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
# 创建数据库数据存放目录
[root@77 ~]# mkdir -p /data/mysql
[root@77 ~]# chown mysql.mysql /data/mysql
[root@77 ~]# ll -d /data/mysql/
drwxr-xr-x. 2 mysql mysql 6 Sep 24 15:14 /data/mysql/
3、源码编译安装
1、下载并解压缩源码包
[root@77 ~]# ls /data/test/
boost_1_59_0.tar.gz
mysql-5.6.49.tar.gz
mysql-5.7.31.tar.gz
[root@77 ~]# tar -zxf /data/test/mysql-5.7.31.tar.gz -C /data/test/
[root@77 ~]# tar -zxf /data/test/boost_1_59_0.tar.gz -C /data/test/
[root@77 ~]# ls /data/test/
boost_1_59_0 mysql-5.6.49.tar.gz mysql-5.7.31.tar.gz
boost_1_59_0.tar.gz mysql-5.7.31
[root@77 ~]# mkdir -p /app/mysql/
[root@77 ~]# cd /data/test/mysql-5.7.31
[root@77 mysql-5.7.31]# ls
BUILD Doxyfile-perfschema LICENSE regex unittest
client extra man scripts VERSION
cmake include mysql-test source_downloads vio
CMakeLists.txt INSTALL mysys sql win
cmd-line-utils libbinlogevents mysys_ssl sql-common zlib
config.h.cmake libbinlogstandalone packaging storage
configure.cmake libmysql plugin strings
dbug libmysqld rapid support-files
Docs libservices README testclients
2、源码编译安装
# 编译mysql没有configure脚本,需要使用cmake,指定参数,生成Makefile文件
[root@77 mysql-5.7.31]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/app/mysql \ # 指定安装路径
> -DMYSQL_DATADIR=/data/mysql/ \ # 指定数据保存路径
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \ # 指定用户
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \ # 指定套间字路径
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DDOWNLOAD_BOOST=1 \
> -DWITH_BOOST=/data/test/boost_1_59_0 # 指定boost所在目录
[root@77 mysql-5.7.31]# make && make install
4、配置环境变量
[root@77 ~]# ls /app/mysql/
bin include LICENSE mysql-test README-test support-files
docs lib man README share
[root@77 ~]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@77 mysql-5.7.31]# . /etc/profile.d/mysql.sh
5、修改配置文件
[root@77 ~]# vim /etc/my.cnf
[mysqld]
# 指定数据库数据存放路径
datadir=/data/mysql
# 指定套间字路径
socket=/data/mysql/mysql.sock
# 设置日志文件路径
log-error=/data/mysql/mysqld.log
#关闭DNS反向解析
skip_name_resolve=on
port=3306
#pid-file=/tmp/mysql.pid
[client]
port=3306
# 指定套间字路径
socket=/data/mysql/mysql.sock
6、生成数据库文件
[root@77 ~]# cd /app/mysql/
[root@77 mysql]# ls
bin include LICENSE mysql-test README-test support-files
docs lib man README share
[root@77 mysql]# bin/mysqld --initialize --datadir=/data/mysql --user=mysql
# 或者
bin/mysqld --initialize --datadir=/data/mysql --user=mysql --explicit_defaults_for_timestamp=1
7、添加到开机自启服务中
[root@77 mysql]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@77 mysql]# chkconfig --add mysqld
[root@77 mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@77 mysql]# service mysqld start
Starting MySQL. SUCCESS!
8、获取初始密码测试登录
[root@77 mysql]# awk '/temporary password/{print $NF}' /data/mysql/mysqld.log
bhd4r0xld/Uo
[root@77 mysql]# systemctl restart mysqld
[root@77 mysql]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (exited) since Thu 2020-09-24 17:15:54 CST; 3s ago
Docs: man:systemd-sysv-generator(8)
Process: 39967 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
Sep 24 17:15:54 77 systemd[1]: Starting LSB: start and stop MySQL...
Sep 24 17:15:54 77 mysqld[39967]: Starting MySQL SUCCESS!
Sep 24 17:15:54 77 systemd[1]: Started LSB: start and stop MySQL.
Sep 24 17:15:54 77 mysqld[39967]: 2020-09-24T09:15:54.723235Z mysqld_safe A mysqld process alr...ists
Hint: Some lines were ellipsized, use -l to show in full.
[root@77 mysql]# mysql -uroot -pbhd4r0xld/Uo
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31
Copyright (c) 2000, 2020, 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>
9、安全加固
[root@77 ~]# cd /app/mysql/
# 执行安全加固脚本
[root@77 mysql]# bin/mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: <-------------- 输入初始化密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 25
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y <------
New password: <----------- 输入新密码,最少八位
Re-enter new password: <---------- 再次输入新密码
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y <------
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!
[root@77 bin]#