一、从yum源安装MySQL服务
1、下载安装mysql yum repo
- wget获取用来安装mysql yum repo的rpm包
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
说明:如果没有安装wget命令,请先安装wget命令
yum install -y wget
- 安装mysql yum repo的rpm包
yum -y install mysql57-community-release-el7-10.noarch.rpm
- 查看上一步骤安装的mysql yum源
2、安装mysql-server
- 安装mysql-server
yum -y install mysql-community-server
3、mysql服务的初始化配置
- 首次启动mysql-server
systemctl start mysqld.service
说明:首次启动mysql-server,mysql-server实例会将初始密码打印在日志/var/log/mysqld.log中。
- 检查是否启动成功
#mysql-server实例默认的端口为3306
netstat -tnlp | grep 3306
- 从日志中查询mysql root用户初始密码
grep "password" /var/log/mysqld.log
[root@localhost home]# grep "password" /var/log/mysqld.log
2020-07-04T01:49:21.178869Z 1 [Note] A temporary password is generated for root@localhost: S&8j,Eu7j4M;
- 用mysql自带客户端登录连接mysql-server
mysql -uroot -p
说明:首次登录需要先修改默认的密码,必须大小写特殊字符组成,因为mysql默认非低等级的密码策略。如果想设置简单的密码,可以改变密码策略及密码长度的校验,生产环境禁止这么做。
- 查看密码策略等属性
SHOW VARIABLES LIKE 'validate_password%';
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 | 密码最小长度
| validate_password_mixed_case_count | 1 | 密码大写小写混合个数
| validate_password_number_count | 1 | 密码数字个数
| validate_password_policy | MEDIUM | 密码检查等级
| validate_password_special_char_count | 1 | 密码特殊字符个数
+--------------------------------------+--------+
- 修改密码策略、及长度校验
#修改为低等级的密码策略,可以设置简单密码
set global validate_password_policy=LOW;
#修改密码长度检验值,最短为6位密码
set global validate_password_length=6;
- 修改mysql用户密码
#修改初始密码,方式一
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
#修改初始密码,方式二
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
- 用户授权,%表示所有主机都能访问
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
- 刷新权限,使其立即生效
flush privileges;
- 修改字符集
vi /etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
- 重启mysql-server
systemctl restart mysqld.service
- 查看mysql status,确认配置生效
mysql> status;
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
4、防火墙开放mysql服务端口
- 查看开放的端口号
firewall-cmd --list-all
- 设置开放端口号
firewall-cmd --add-port=3306/tcp --permanent
- 重启防火墙
firewall-cmd --reload
5、修改mysql密码
- 登录状态下修改密码
# 方法一. 设置当前登录用户密码
mysql> set password=password('newpassword');
# 方法二. 直接改用户表
mysql> use mysql;
mysql> update user set authentication_string=password('123abc') where user='root';
# 方法三. 修改密码
mysql> alter user root@'localhost' identified by '123456';
- 未登录状态下
vi /etc/my.cnf
[mysqld]
skip-grant-tables
update user set authentication_string=password('123456') where user='root' and Host = 'localhost';
说明:未登录的情况下,可以跳过权限检查来修改密码,不需要密码即可登录,登录后就可以随便玩了。