linux下安装mysql

1.安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#获取安装用的Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
#安装MySQL服务
yum -y install mysql-community-server
#查看版本
mysql -v
#启动mysql
systemctl start mysqld.service

#找回初始密码并登录 jWTAeq-DF9tJ
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
#注意;找不到初始密码及其他情况参考下文"修改密码"

#使用初始密码登录后需要先修改一次密码后才能操作
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

#为避免Repository每次yum操作都会自动更新
yum -y remove mysql57-community-release-el7-10.noarch

2.修改密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#1.找回密码
grep 'temporary password' /var/log/mysqld.log

#2.强制修改密码
systemctl stop mysqld #停止服务
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" #跳过密码
systemctl start mysqld #启动服务
mysql -u root #登录
UPDATE mysql.user SET authentication_string = PASSWORD('新密码') WHERE User = 'root' AND Host = 'localhost'; #设置新密码
FLUSH PRIVILEGES; #刷新权限
quit #退出

#3.用操作系统账号修改密码
mysql -uroot -p #使用centos账号密码登录
UPDATE mysql.user SET authentication_string = PASSWORD('新密码') WHERE User = 'root' AND Host = 'localhost'; #设置新密码
FLUSH PRIVILEGES; #刷新权限
quit #退出

3.修改远程连接权限

1
2
3
4
5
6
7
8
9
10
11
#选择mysql
use mysql;

#授权远程连接
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'YYBrhr_2018' WITH GRANT OPTION;

#注意:如果navicat远程连接报1521错误
GRANT ALL ON *.* TO 'root'@'%'; #远程连接授权
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #更改加密规则
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; #更改密码
FLUSH PRIVILEGES; #刷新权限

注意,这一步需要小心,在自己的服务器或者内网的服务器就算了,如果是在外网的生产服务器上,务必先咨询同事。

0%