# 用 SQLyog 或者 Navicat 图形界面软件 连接 Ubuntu 中的 mysql 数据库出错分析

发布于:2024-05-17 ⋅ 阅读:(119) ⋅ 点赞:(0)

用 SQLyog 或者 Navicat 图形界面软件 连接 Ubuntu 中的 mysql 数据库出错分析

一、分析原因:

1、mysql 加密方式不同,

2、不能直接用 root 用户连接。

3、mysqld.cnf 配置文件,默认绑定地址是 127.0.0.1

二、解决方法:

1、用 root 用户登录,创建新用户:

sudo mysql -u root -p

mysql>  create user 'username'@'%' identified by 'password';

2、如果无法设置成功,可能是 mysql 密码规则限制,查看 mysql 密码规则:

show variables like 'validate_password%';

密码规则说明:

  • policy : 密码强度属性,分为:low, medium, strong。
  • length : 密码长度。
    默认密码长度不小于8位,且含有大小写英文和数字组合。

3、设置密码规则:

- 设置密码强度
set global validate_password.policy=0;
- 设置密码长度
set global validate_password.length=4;
- 设置密码
CREATE USER 'test'@'%' IDENTIFIED BY '123';

4、新用户创建后,进行授权:

grant all on *.* to 'test'@'%';
或者
grant all on . to ‘test’@‘%’ with grant option;

5、刷新权限:

flush privileges;

6、退出 mysql 用新创建的用户名密码登录。

- 退出 mysql 
mysql>  exit 或者:quit
- 用新用户密码登录:
mysql -u test -p123

7、如果不能正常登录,说明前面设置有误,可以用 root 登录,重新修改。

mysql -u root -p123  (假如:root 密码也是 123)

--- 连接权限数据库
mysql> use mysql;  
 
--- 更新用户: 
 
--- 低版本 mysql 5.x 
mysql> update user set password=password('123') where user='test' and host='localhost';  
 
--- 高版本 mysql 8.x 
mysql> update user set authentication_string='123' where user='test';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--- 重置密码:

--- 低版本 mysql 5.x 
ALTER USER ‘test’@‘%’ IDENTIFIED BY ‘123’;

--- 高版本 mysql 8.x 
mysql> alter user 'test'@'%' identified with mysql_native_password by '123';

--- 刷新权限:
mysql> flush privileges;  

--- 重启 mysql :
sudo service mysql restart
或
sudo systemctl restart mysql.service

8、如果不能更新用户、重置密码,可以试试以下方法。

--- 连接权限数据库
mysql> use mysql;  
 
--- 查看 user 主机名:
mysql> select user, host from user; 

--- 如果 test 用户的 host 是 localhost 本地用户,就使用:
--- 低版本 mysql 5.x 
ALTER USER ‘test’@‘localhost’ IDENTIFIED BY ‘123’;
--- 高版本 mysql 8.x 
mysql> alter user 'test'@'localhost' identified with mysql_native_password by '123';

--- 如果 test 用户的 host 是 % 网络用户,就使用:
--- 低版本 mysql 5.x 
ALTER USER ‘test’@‘%’ IDENTIFIED BY ‘123’;
--- 高版本 mysql 8.x 
mysql> alter user 'test'@'%' identified with mysql_native_password by '123';

--- 还可以查看 再次查看 mysql 数据库中 user 表的 plugin 字段
mysql>  select user, host, plugin from user;

--- 如果发现 test 用户是 caching_sha2_password 的插件,
而不是 mysql_native_password 插件,可以把它改成 mysql_native_password 插件。
mysql>  update user set plugin='mysql_native_password' where user='test';

--- 也可以把 test 用户的 host 更改成 localhost 进行相应的操作。
mysql>  update user set host='localhost' where host='%' and user='test';

9:修改 /etc/mysql/mysql.conf.d/ 目录下的 mysqld.cnf 配置文件。

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# bind-address          = 127.0.0.1
# mysqlx-bind-address   = 127.0.0.1

bind-address 			= 0.0.0.0
mysqlx-bind-address     = 0.0.0.0

10、重启 mysql :

sudo service mysql restart
或
sudo systemctl restart mysql.service

11、关闭 ubuntu 防火墙设置

--- 查看防火墙状态:
sudo ufw status  ( 可能会查询到 Status: inactive 未激活)
或者
sudo systemctl status ufw 
或者
sudo systemctl status ufw.service 

--- 关闭防火墙:
sudo ufw disable (禁用防火墙,如果查询到 inactive 未激活,可能此命令失效)
或者:
sudo systemctl stop ufw
sudo ufw disable (开机禁用防火墙)

--- 如果后续想再开启防火墙,可以使用以下命令
sudo start ufw 
sudo systemctl ufw enable (开机启用防火墙)

12、再次用 SQLyog 或者 Navicat 图形界面软件 连接 Ubuntu 中的 mysql


网站公告

今日签到

点亮在社区的每一天
去签到