安装依赖环境

# patchelf,默认安装不成功,通过编译安装
dnf -y install ncurses ncurses-devel cmake patchelf
# 下方两个包是安装过程中发现缺失的,提前安装上
dnf -y install libtirpc-devel rpcgen

编译安装patchelf

wget https://github.com/NixOS/patchelf/releases/download/0.14.5/patchelf-0.14.5.tar.gz
tar  xzvf patchelf-0.14.5.tar.gz
cd patchelf-0.14.5
./configure
make && make install
# 查看是否安装成功
which patchelf

创建用户/组

groupadd mysql
useradd mysql -g mysql -s /sbin/nologin

创建相关目录

mkdir /data/mysql/{data,tmp,run,log} -p
mkdir /usr/local/mysql/{etc,tmp} -p

# 授权
chown -R mysql:mysql /data/mysql

编译安装Mysql 8.0.24

下载包

mysql官方下载地址:https://downloads.mysql.com/archives/community/

cd /usr/local/src/
# 尽可能下载自带boost库的包,不然需要单独编译boost,很麻烦。
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-8.0.24.tar.gz
tar mysql-boost-8.0.24.tar.gz
cd mysql-8.0.24

编译安装

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_charset=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_SSL=system -DWITH_READLINE=on -DMYSQL_DATADIR=/data/mysql -DWITH_BOOST=./boost -DWITH_SYSTEMD=1 -DFORCE_INSOURCE_BUILD=1

make && make install

cp /etc/my.cnf.rpmsave /usr/local/mysql/etc/my.cnf
# 在/usr/local/mysql/etc/my.cnf文件中的[mysqld]中添加如下内容
character-set-server=utf8mb4
# 在/usr/local/mysql/etc/my.cnf文件件中的[client]中添加如下内容,用于解决本机mysql客户端连接需要手动输入socket文件地址的问题。
socket=/data/mysql/run/mysql.sock

# 可以通过添加环境变量方式实现如下功能
ln -sv /usr/local/mysql/bin/mysql /usr/sbin/mysql
ln -sv /usr/local/mysql/bin/mysqladmin /usr/sbin/mysqladmin
ln -sv /usr/local/mysql/bin/mysqldump /usr/sbin/mysqldump

chown -R mysql:mysql /usr/local/mysql

数据初始化

/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data --initialize-insecure

创建启动脚本

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service  /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable --now mysqld

修改密码及授权

# 登陆mysql,直接回车,没有密码
mysql -u root -p

mysql> use mysql;
Database changed

# 修改为永久密码不过期且加密方式为mysql_native_password,不然navicat连接会失败
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';

# 修改账号登陆权限为%
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

星霜荏苒 居诸不息