RHEL 7.5 mysql-5.7.17 源码安装
1、解决依赖包并下载源码包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost
wget http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.17.tar.gz
tar -zxvf mysql-5.7.17.tar.gz
如果没有 cmake 则需要单独安装,安装cmake步骤如下:
tar --axf cmake-3.8.2.tar.gz
cd cmake-3.8.2
./configure
make
make install
2、新建MySQL用户和用户组,创建/data/mysql 目录存放mysql数据
groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
mkdir -pv /data/mysql
3、预编译及编译安装
cd mysql-5.7.17
cmake \
-DCMAKE_INSTALL_PREFIX=/data/mysql/ \
-DMYSQL_DATADIR=/data/mysql/data/ \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DDEFAULT_charset=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=3306 \
-DWITH_EXTRA_CHARSETS=all \
-DENABLED_PROFILING=ON \
-DMYSQL_MAINTAINER_MODE=OFF \
-DWITH_INNODB_MEMCACHED=1 \
-DWITH_DEBUG=OFF \
-DWITH_ZLIB=bundled \
-DMYSQL_USER=mysql \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost/
参数注解:
DCMAKE_INSTALL_PREFIX=/usr/local/mysql:安装路径
DMYSQL_DATADIR=/data/mysql:数据文件存放位置
DSYSCONFDIR=/etc:my.cnf路径
DWITH_MYISAM_STORAGE_ENGINE=1:支持MyIASM引擎
DWITH_INNOBASE_STORAGE_ENGINE=1:支持InnoDB引擎
DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock:连接数据库socket路径
DMYSQL_TCP_PORT=3306:端口
DENABLED_LOCAL_INFILE=1:允许从本地导入数据
DWITH_PARTITION_STORAGE_ENGINE=1:安装支持数据库分区
DEXTRA_CHARSETS=all:安装所有的字符集
DDEFAULT_charset=utf8:默认字符
DWITH_EMBEDDED_SERVER=1:嵌入式服务器
开始编译安装:
make
make install
4、初始化MySQL系统授权表
以root初始化操作时要加 --user=mysql 参数,生成一个随机密码(注意保存登录时用)
执行初始化配置脚本,目的是创建系统自带的数据库和表
$ /data/mysql/bin/mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
注意:mysql_install_db 参数在 5.7 版本之后被弃用,请使用 --initialize 参数。初始化数据库时请注意看弹出的信息,以免有错误产生,初始化结束后会生成一个临时密码,需要记录下来,如下:
2024-12-19T07:42:31.832168Z 1 [Note] A temporary password is generated for root@localhost: /HiBW!XkD3e%
5、创建配置文件
将默认生成的 my.cnf 备份或删除
mv /etc/my.cnf /etc/my.cnf.bak
cd /data/mysql/support-files
cp my-default.cnf /etc/my.cnf
chmod 644 /etc/my.cnf
按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行.
vi /etc/my.cnf
[client]
port = 3306
socket = /data/mysql/mysql.sock
default_character-set=utf8
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
user = mysql
port = 3306
socket = /data/mysql/mysql.sock
basedir = /data/mysql
datadir = /data/mysql/data
server_id = 12315
log_bin=/data/mysql/mysql-binlog/mysql-bin
expire_logs_days = 7
slow_query_log = on
slow_query_log_file = /data/mysql/slowlog/slow.log
long_query_time = 1
log_queries_not_using_indexes = on
建议禁用符号链接,可以防止各种安全风险
symbolic-links=0
[mysqld_safe]
log_error = /data/mysql/mysql_error.log
pid_file = /data/mysql/mysql.pid
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=500
time_zone = '+8:00'
explicit_defaults_for_timestamp=true
6、配置防火墙
Linux 7 中默认使用 Firewalld 防火墙,Firewalld中添加端口方法如下:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
在终端内运行下面的命令,刷新防火墙配置:
systemctl restart firewalld
7、配置用户调用环境
修改 /etc/profile文件,在文件末尾添加
PATH=/data/mysql/bin:$PATH
export PATH
运行下面的命令,让配置立即生效
source /etc/profile
8、配置mysql启动服务
启动服务:
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
增加执行权限
chmod 755 /etc/init.d/mysqld
/etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
查看mysql端口
netstat -ntlp |grep mysqld
注意:如需配置自动启动需要把服务加入到启动项,我这里是测试环境,不希望它自启动。
9、设置mysql密码
使用初始化时记录的密码登录数据库并修改密码:
mysql -u root -p
Enter password: /HiBW!XkD3e%
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log
Copyright (c) 2000, 2016, 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> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
若要设置root用户可以远程访问,执行如下命令:
mysql> grant all privileges on *.* to root@'192.168.142.1' identified by 'root' with grant option;
mysql> flush privileges;
到此mysql源码编译安装完成。