本文中MySQL安装目录为/usr/local/mysql
-
上传MySQL安装包至/usr/local目录,执行命令解压
[root@JetXi local]# tar zxf mysql-5.7.28-el7-x86_64.tar.gz
-
对该目录进行改名,便于配置
[root@JetXi local]# mv mysql-5.7.28-el7-x86_64 mysql
-
创建data目录和log目录,并创建log文件
[root@JetXi local]# mkdir mysql/data
[root@JetXi local]# mkdir mysql/log
[root@JetXi local]# touch mysql/log/mysql.log -
创建mysql组和mysql用户
[root@JetXi local]# groupadd mysql
[root@JetXi local]# useradd -g mysql mysql -
将MySQL安装目录所属的用户和组改为MySQL
[root@JetXi local]# chown -R mysql:mysql mysql
-
将MySQL安装目录的权限修改为755,即文件拥有者对其可读可写可执行,其他同组用户和组外用户对其可读可执行
[root@JetXi local]# chmod 755 mysql
-
拷贝mysql.server文件
[root@JetXi local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
-
配置该文件
[root@JetXi local]# vi /etc/init.d/mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data -
配置/etc/my.cnf文件
[root@JetXi local]# vi /etc/my.cnf
[mysql]
default-character-set=utf-8
socket=/usr/local/mysql/mysql.sock
?
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
port=3306
default-storage-engine=INNODB
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
?
[mysqld_safe]
log-error=/usr/local/mysql/log/mysql.log
pid-file=/usr/local/mysql/mysql.pid -
将MySQL添加到环境变量
[root@JetXi local]# vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@JetXi local]# source /etc/profile -
初始化MySQL,初始化成功输出的信息中,最后一行是MySQL的临时密码
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
...
2021-02-19T20:41:02.054551Z 1 [Note] A temporary password is generated for root@localhost: BUcyY36C3L;< -
启动MySQL服务
[root@JetXi local]# service mysql start
-
使用初始化生成的密码登录MySQL,登录成功说明配置成功
[root@JetXi ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28
?
Copyright (c) 2000, 2019, 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> -
修改MySQL密码
mysql> set password=password("JetXi");
Query OK, 0 rows affected, 1 warning (0.00 sec) -
若登录时提示密码过期,则编辑my.cnf文件,配置免密码登录
[root@JetXi local]# vi /etc/my.cnf
skip-grant-tables重启MySQL服务
[root@JetXi local]# service mysql restart
登录MySQL,直接回车进入
[root@JetXi local]# mysql -uroot -p
更新相关字段,解除密码过期限制
mysql> update mysql.user set password_expired=‘N‘
退出MySQL,注释掉刚才my.cnf中的跳过验证代码,保存退出,重启MySQL服务
[root@JetXi local]# service mysql restart