基础环境准备

  1. #系统版本
  2. CentOS 7
  3. #查看系统 glibc支持的版本
  4. strings /lib64/libc.so.6 |grep ^GLIBC
  5. #添加mysql用户
  6. id mysql
  7. useradd -s /sbin/nologin -M mysql
  8. #下载wget下载工具
  9. yum -y install wget
  10. #卸载自带的MariaDB 5.5
  11. rpm -qa | grep mariadb
  12. yum -y remove mariadb-libs
  13. #安装依赖
  14. yum -y install libaio-devel

1、下载安装包

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
# 校验MD5值与下载页对比
md5sum mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
tar -zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

2、安装MySQL软件

#创建目录结构,授权
mkdir -p /data/app
mkdir -p /data/3306/data
mkdir -p /data/3306/logs
mv /root/mysql-5.7.28-linux-glibc2.12-x86_64 /data/app/mysql5728
chown -R mysql.mysql /data
ll -d /data

#添加环境变量
vim /etc/profile
export PATH=/data/app/mysql5728/bin:$PATH

#刷新环境变量配置并检验
source /etc/profile
mysql -V

#显示此信息代表MySQL软件安装完成
mysql  Ver 14.14 Distrib 5.7.28, for linux-glibc2.12 (x86_64) using  EditLine wrapper

3、初始化数据目录

初始化目的: 创建系统库

mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql5728 --datadir=/data/3306/data

#正确输出信息
2021-06-10T15:30:49.769938Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-10T15:30:53.769239Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-10T15:30:54.232752Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-10T15:30:54.347668Z 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: d8595709-ca00-11eb-a738-000c2909345a.
2021-06-10T15:30:54.348783Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-10T15:30:55.189334Z 0 [Warning] CA certificate ca.pem is self signed.
2021-06-10T15:30:55.364802Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

可能遇到的报错

#缺少 libaio-devel 依赖
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法:
yum -y install libaio-devel

4、编写配置文件

cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/data/app/mysql5728
datadir=/data/3306/data
port=3306
socket=/tmp/mysql.sock
server_id=6
log_bin=/data/3306/logs/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1

[client]
socket=/tmp/mysql.sock
EOF

5、准备启动脚本

cp -a /data/app/mysql5728/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start
/etc/init.d/mysqld stop

6、使用systemd管理mysql

cat > /etc/systemd/system/mysql.service  <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/data/app/mysql5728/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF

#启动和开机自启mysql
systemctl enable mysql
systemctl start mysql
systemctl status mysql
systemctl list-unit-files | grep mysql

7、设置用户密码

#输入mysql命令,进入到mysql命令提示符
mysql

#设置root管理员用户密码(只能本机登录)
alter user 'root'@'localhost' identified by '123';

#创建admin用户,所有库,所有表,远程访问权限
create user 'admin'@'%' identified by '123';
mysql> grant all on *.* to 'admin'@'%';

#刷新权限表
flush privileges;

#查看用户列表
select host,user,plugin from mysql.user;

#退出
exit

#测试连接
#本地访问
mysql -uroot -p123
#远程访问
mysql -uadmin -p123 -h 192.168.10.14 -P 3306