目录



1.环境描述

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)


2.下载源码包并安装依赖包

链接:​​https://pan.baidu.com/s/166MCqR2Svd3-30ARS0vg5w ​

提取码:r3qj

2.1 安装依赖包

yum -y install cmake
yum install gcc gcc-c++ -y
yum -y install ncurses-devel


3.上传源码包Linux服务器并解压预编译

3.1 上传解压

# 上传源码包到Linux服务器
rz命令

# 解压
tar -zxvf mysql-5.7.21.tar.gz
tar -zxvf boost_1_59_0.tar.gz

# 将boost解压后的文件移动到/usr/local/boost
mv boost_1_59_0 /usr/local/boost

# 进入到目录
cd mysql-5.7.21


3.2 预编译

cmake  .  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5/ \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0 \
-DWITH_BOOST=/usr/local/boost


出现下图表示cmake成功

【Mysql】Centos7.4下源码安装mysql-5.7.21_mysql

3.3 make && make install

# -j4表示同时开启4个进程
make -j4 && make install -j4


在编译安装的过程中可能出现如下错误:

【Mysql】Centos7.4下源码安装mysql-5.7.21_预编译_02

出现上述错误后,继续使用命令编译即可:

make  && make install 


4.进行启动和初始化

# 进入安装目录
[root@localhost mysql-5.7.21]# cd /usr/local/mysql5/
[root@localhost mysql5]# pwd
/usr/local/mysql5
[root@localhost mysql5]# ll
total 64
drwxr-xr-x. 2 root root 4096 Aug 21 17:49 bin
-rw-r--r--. 1 root root 17987 Dec 28 2017 COPYING
-rw-r--r--. 1 root root 17987 Dec 28 2017 COPYING-test
drwxr-xr-x. 2 root root 55 Aug 21 17:49 docs
drwxr-xr-x. 3 root root 4096 Aug 21 17:49 include
drwxr-xr-x. 4 root root 191 Aug 21 17:49 lib
drwxr-xr-x. 4 root root 30 Aug 21 17:49 man
drwxr-xr-x. 10 root root 4096 Aug 21 17:50 mysql-test
-rw-r--r--. 1 root root 2478 Dec 28 2017 README
-rw-r--r--. 1 root root 2478 Dec 28 2017 README-test
drwxr-xr-x. 28 root root 4096 Aug 21 17:50 share
drwxr-xr-x. 2 root root 90 Aug 21 17:50 support-files

mysql 5.7版本的配置文件和执行启动文件需要自己写或者拷贝
Centos 系统默认有/etc/my.cnf文件,需要替换或者删除文件,重新写入mysql 5.7版本的配置文件
mysql 5.7 版本默认密码是在第一次启动之后会在日志中生成,可以直接搜索password字段,第一次登录修改密码,就需要使用默认密码,然后再去修改

# 配置用户和安装目录
groupadd mysql
useradd -g mysql -r mysql
cd /usr/local/mysql5 && chown mysql:mysql -R .
mkdir -p /data/mysql && chown mysql:mysql -R /data/mysql

# 配置完成之后,修改/etc/my.cnf文件
[root@localhost mysql5]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql5
datadir = /data/mysql
port = 3306
socket = /tmp/mysql.sock


如下图:

【Mysql】Centos7.4下源码安装mysql-5.7.21_上传_03

配置完成之后,初始化数据库

[root@localhost etc]# /usr/local/mysql5/bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql5
2019-08-21T10:21:38.295997Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-21T10:21:39.343579Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-21T10:21:39.648591Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-21T10:21:39.718993Z 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: 76b46fdb-c3fd-11e9-b098-000c2916d1d0.
2019-08-21T10:21:39.720827Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-08-21T10:21:39.726003Z 1 [Note] A temporary password is generated for root@localhost: VI:N(rjb0M;H
从上述返回结果我们可以知道初始密码为:VI:N(rjb0M;H


【Mysql】Centos7.4下源码安装mysql-5.7.21_mysql_04

5.配置命令连接和脚本启动

ln -s /usr/local/mysql5/bin/* /usr/bin/
\cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
# 启动数据库
service mysqld restart


出现上图表示服务已经启动成功

查看mysql进程

【Mysql】Centos7.4下源码安装mysql-5.7.21_mysql_05

6.使用默认密码连接数据库

[root@localhost bin]# cd /usr/local/mysql5/bin/ && ./mysql -uroot -p'VI:N(rjb0M;H'
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 5
Server version: 5.7.21

Copyright (c) 2000, 2018, 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>
# 注意-u与-p后边没有空格,有空格则会在登录的时候提示输入免密


作者:奔跑的金鱼