mysql重设密码及允许远程访问

一.重设密码

1.知道原来的myql的root的密码

方法一:

通过登录mysql系统,
mysql -uroot -p
Enter password: 【输入原来的密码】
mysql>use mysql;
mysql> update user set authentication_string=passworD("test123") wher user='root';
mysql> flush privileges;
mysql> exit;   

方法二:

在mysql系统外,使用mysqladmin
mysqladmin -u root -p password "test123"
Enter password: 【输入原来的密码】

2.不知道原来的myql的root的密码

①.跳过密码启动

mysqld_safe --skip-grant-tables &

②.重设密码

# 直接输入mysql,进入mysql
mysql
mysql> use mysql;
mysql> UPDATE user SET authentication_string=password("test123") WHERE user='root';   
mysql> flush privileges;
mysql> exit;     

③.重新启动mysql

systemctl restart mysql

二.允许外部访问

1.设置允许访问

# password是数据库密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

FLUSH PRIVILEGES;

2.使用navicat检测一下,如果还是不能连接,可能是防火墙限制了。需要在防火墙里面加开放数据库端口的规则。先查看一下目前的防火墙,此时没有放开3306端口!

firewall-cmd --list-all

[root@iZhp3di673f3rx1g5d9dfdZ soft]# firewall-cmd --list-all 
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

3.开放3306端口

firewall-cmd --permanent --add-port=3306/tcp

4.重启防火墙

service firewalld restart

5.查看3306端口是否开放

firewall-cmd --query-port=3306/tcp

[root@iZhp3di673f3rx1g5d9dfdZ soft]# firewall-cmd --query-port=3306/tcp
yes

6.再次查看现在防火墙

firewall-cmd --list-all

[root@iZhp3di673f3rx1g5d9dfdZ soft]# firewall-cmd --list-all 
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 3306/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

7.此时就可以远程连接了

站在巨人肩膀上摘苹果

https://blog.csdn.net/qq_35070711/article/details/70860014

https://www.cnblogs.com/yybrhr/p/9810375.html

# Mysql  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×