MySQL编译安装详细教程

首先去https://downloads.mysql.com/archives/community/下载MySQL源代码,可通过以下条件快速筛选找到下载地址:
Product Version:5.7.34
Operating System:Source Code
OS Version:All
 
此时最后一条搜索结果应该是mysql-boost-5.7.34.tar.gz,这就是MySQL源代码压缩包,将其下载回来即可。

创建mysql组和mysql用户
[root@localhost ~]# groupadd mysql && useradd -s /sbin/nologin -g mysql -M mysql

以下是编译安装过程:
[root@localhost ~]# cd /src/ # 进入/src/目录
[root@localhost src]# rz -b # 选择MySQL源代码压缩包并上传
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring mysql-boost-5.7.34.tar.gz...
  100%   51651 KB    25825 KB/sec    00:00:02       0 Errors
[root@localhost src]# tar -xf mysql-boost-5.7.34.tar.gz # 解压缩MySQL源代码压缩包
[root@localhost src]# cd mysql-5.7.34/ # 进入MySQL源代码目录
[root@localhost mysql-5.7.34]# cmake . -DCMAKE_INSTALL_PREFIX=/program/mysql \ # 指定安装目录
-DMYSQL_DATADIR=/program/mysql/data \ # 指定数据库文件保存目录
-DSYSCONFDIR=/program/mysql \ # 指定MySQL配置文件保存目录
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/dev/shm/mysql.sock \ # 指定socket文件路径
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_BOOST=boost \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
[root@localhost mysql-5.7.34]# make # make过程非常耗时,需耐心等待
[root@localhost mysql-5.7.34]# make install # 执行完make install才会建立/program/mysql/目录

编译安装完成后要对MySQL进行配置,要注意的是安装完后MySQL会自动生成几个配置文件,为避免出现配置混乱建议只保留/program/mysql/my.cnf这一个配置文件,其它配置文件一律删除。
[root@localhost ~]# /program/mysql/bin/mysql --help | grep 'Default options' -A 1
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /program/mysql/my.cnf ~/.my.cnf
[root@localhost ~]# rm -rf /etc/my.cnf /etc/mysql/my.cnf /program/mysql/my.cnf ~/.my.cnf
[root@localhost ~]# vim /program/mysql/my.cnf
# 配置文件/program/mysql/my.cnf的具体内容可看『my.cnf文件配置示例』。
[root@localhost ~]# 

建立相关目录及文件
[root@localhost ~]# mkdir /program/mysql/data/
[root@localhost ~]# mkdir /program/mysql/log/
[root@localhost ~]# touch /program/mysql/log/error.log
[root@localhost ~]# touch /program/mysql/log/general.log
[root@localhost ~]# touch /program/mysql/log/slow-query.log
[root@localhost ~]# chown -R mysql:mysql /program/mysql/

把MySQL加入chkconfig方便管理
[root@localhost ~]# cp /program/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on # 开机自动启动MySQL

初始化数据库
[root@localhost ~]# /program/mysql/bin/mysqld --initialize --user=mysql
[root@localhost ~]# cat /program/mysql/log/error.log 
2021-10-16T03:20:25.906578Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-16T03:20:25.906685Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2021-10-16T03:20:26.825216Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-16T03:20:26.963492Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-16T03:20:27.090363Z 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: 02249875-2e30-11ec-bf52-000c290e1b25.
2021-10-16T03:20:27.092126Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-16T03:20:27.547013Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-16T03:20:27.583261Z 1 [Note] A temporary password is generated for root@localhost: *i5fx=?5kIG. # 这是root帐号初始密码
[root@localhost ~]# 

生成SSL相关文件(注意:此部分是为增强安全性,非必要操作,对安全性要求不高可跳过)
[root@localhost ~]# mkdir /program/mysql/ssl
[root@localhost ~]# /program/mysql/bin/mysql_ssl_rsa_setup --datadir=/program/mysql/ssl # 生成SSL证书,参数--datadir可以指定证书保存目录
Generating a 2048 bit RSA private key
...............+++
..........................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
......................................................................................................................+++
.........................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..............................................................................................................+++
..............+++
writing new private key to 'client-key.pem'
-----
[root@localhost ~]# chown -R mysql:mysql /program/mysql/
[root@localhost ~]# ls -1 /program/mysql/ssl # 查看生成的SSL证书,注意ls后面的-1是12345的1
ca-key.pem
ca.pem # 服务端CA证书
client-cert.pem # 客户端证书(客户端连接数据库时需要使用该文件)
client-key.pem # 客户端密钥(客户端连接数据库时需要使用该文件)
private_key.pem
public_key.pem
server-cert.pem # 服务端证书
server-key.pem # 服务端密钥
[root@localhost ~]# 
备注①:SSL仅对使用IP地址连接数据库启用,而对使用socket方式连接数据库则不会启用。
备注②:使用SSL能提高安全性,但是也会影响性能使QPS降低23%左右,可根据实际情况权衡是否要启用SSL。
备注③:如果服务端启用了SSL那么客户端必须使用相关证书才能连接,否则会报错。

在MySQL配置文件的[mysqld]节点下增加三个参数指定SSL相关证书文件
[root@localhost ~]# vim /program/mysql/my.cnf
[mysqld]
# 指定SSL相关证书文件
ssl-ca=/program/mysql/ssl/ca.pem
ssl-cert=/program/mysql/ssl/server-cert.pem
ssl-key=/program/mysql/ssl/server-key.pem
[root@localhost ~]# 
备注:服务端指定了SSL证书后,客户端连接数据库时也要使用SSL证书,以Navicat软件为例,常规设置和非SSL连接一样,把MySQL服务器的IP地址、端口、用户名、密码填写上去就可以了,主要是SSL设置需要指定客户端密钥(client-key.pem文件)和客户端证书(client-cert.pem)。
注意事项①:Navicat软件引用客户端密钥和客户端证书时文件路径不能有中文,否则会报“2026 - SSL connection error”错误。
注意事项②:不需要指定CA证书。

启动数据库并修改root帐号初始密码
[root@localhost ~]# service mysqld status # 查看MySQL运行状态
MySQL is not running                                       [失败]
[root@localhost ~]# service mysqld start # 启动MySQL
Starting MySQL..                                           [  确定  ]
[root@localhost ~]# 
[root@localhost ~]# /program/mysql/bin/mysql -u root -p
Enter password: *i5fx=?5kIG. # 这里输入root帐号初始密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34-log
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
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> SET PASSWORD = PASSWORD('这里填写root帐号新密码'); -- 修改root帐号密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
 
mysql> EXIT;
Bye
[root@localhost ~]# 

MySQL默认是禁止root帐号远程连接的,如要允许root帐号远程连接可前往『允许root帐号远程连接』。

Copyright © 2023 码农人生. All Rights Reserved