对于我们刚开始安装的mysql或者mariadb来说,默认是不开启远程连接的。所以需要我们手动开启远程连接的权限。如果你是使用docker安装mysql那需要先进入容器中,这里就不讲如何进入容器了,百度一堆
- 登录mysql
mysql -u root -p
Enter password:
如果刚安装没有设置密码的话就直接回车 - 切换到mysql这个数据库
use mysql; - 修改root的host为%
update user set host = '%' where user = 'root';
检查是否以修改:
select host, user from user;
+--------------+------+
| host | user |
+--------------+------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| dfa0e46aa7ac | |
| dfa0e46aa7ac | root |
| localhost | |
+--------------+------+ - 设置root密码
UPDATE user SET Password = PASSWORD('123456') WHERE user = 'root';
检查:
select host, user , password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| % | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| dfa0e46aa7ac | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ::1 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | | |
| dfa0e46aa7ac | | |
+--------------+------+-------------------------------------------+
注意sql语句不要写错了,是Password = PASSWORD('123456'),PASSWORD函数会对密码进行加密,匹配时同样会对输入的密码加密然后再比对。 - 刷新修改,使其生效
flush privileges; - 使用客户端连接检测是否成功