MySQL远程访问权限的设置

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 今儿有位同事提出,一套MySQL 5.6的环境,从数据库服务器本地登录,一切正常,可是若从远程服务器访问,就会报错,ERROR 1045 (28000): Access denied for user 'bisal'@'x.x.x.x' (using password: YES)我才开始接触MySQL,因此每一个错误场景,都是增长经验的机会,这种错误要么是密码错误,要么是未设置远程IP访问权限。

今儿有位同事提出,一套MySQL 5.6的环境,从数据库服务器本地登录,一切正常,可是若从远程服务器访问,就会报错,

ERROR 1045 (28000): Access denied for user 'bisal'@'x.x.x.x' (using password: YES)


我才开始接触MySQL,因此每一个错误场景,都是增长经验的机会,这种错误要么是密码错误,要么是未设置远程IP访问权限。


我们模拟下这个过程,首先,创建用户bisal,如果密码不加引号会报错,

mysql> create user bisal identified by bisal;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bisal' at line 1


创建完成,可以看出,用户bisal的host是%,不是具体某个IP,

mysql>  create user bisal identified by 'bisal';

Query OK, 0 rows affected (0.00 sec)


mysql> select user, password, host from user;

+-------+-------------------------------------------+-----------------+

| user  | password                                  | host            |

+-------+-------------------------------------------+-----------------+

...

| bisal | *9AA096167EB7110830776F0438CEADA9A7987E31 | %               |

+-------+-------------------------------------------+-----------------+


实验一:让指定IP访问数据库

假设数据库服务器IP是x.x.x.1,授权让x.x.x.3用户可以访问,

mysql> grant all privileges on *.* to 'bisal'@'x.x.x.3';

Query OK, 0 rows affected (0.00 sec)


此时从x.x.x.2上访问数据库,就会提示错误,因为仅允许x.x.x.3服务器,可以访问数据库,

mysql -h x.x.x.1 -ubisal

ERROR 1045 (28000): Access denied for user 'bisal'@'app' (using password: YES)


授权让x.x.x.2用户可以访问,

mysql> grant all privileges on *.* to 'bisal'@'x.x.x.2' identified by 'bisal';

Query OK, 0 rows affected (0.00 sec)


此时从x.x.x.2上,就可以访问数据库了,

mysql -h x.x.x.1 -ubisal -pbisal

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1008

Server version: 5.6.31-log MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> use mysql

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed


实验二:让所有IP访问数据库

首先,收回刚才的授权,

mysql> revoke all privileges on *.* from bisal@'%';

Query OK, 0 rows affected (0.00 sec)


mysql> show grants for bisal;

+--------------------------------------------------------------------------------------------+

| Grants for bisal@%                                                                                |

+--------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'bisal'@'%' IDENTIFIED BY PASSWORD '*9AA096167EB7110830776F0438CEADA9A7987E31' |

+--------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)


此时从x.x.x.2访问数据库,会提示错误,

mysql -h x.x.x.x -ubisal -pbisal

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 997

Server version: 5.6.31-log MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> use mysql

ERROR 1044 (42000): Access denied for user 'bisal'@'%' to database 'mysql'


此时授予%所有机器访问权限,

mysql> grant all privileges on *.* to 'bisal'@'%' identified by 'bisal';

Query OK, 0 rows affected (0.00 sec)


从x.x.x.2访问数据库,此处的报错,是因为未输入密码,

mysql -ubisal

ERROR 1045 (28000): Access denied for user 'bisal'@'localhost' (using password: YES)


但如果之前设置的密码,和输入的密码不同,还是会提示错误,

mysql> grant all privileges on *.* to 'bisal'@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)


[root@vm-kvm11853-app ~]# mysql -h x.x.x.129 -ubisal -pbisal

Warning: Using a password on the command line interface can be insecure.

ERROR 1045 (28000): Access denied for user 'bisal'@'vm-kvm11853-app' (using password: YES)


使用正确的密码登录,一切正常了,

mysql -ubisal -p123

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 987

Server version: 5.6.31-log MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> use mysql

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed



总结:

1. MySQL中可以设置某个IP访问权限,也可以设置%所有IP访问权限。、

2. grant all privileges ... identified by 'password',此处的password可以不是这用户的密码,远程访问以这个密码为准。

3. create user设置密码,需要用引号括起来,否则会提示语法错误。

4. create user用户不加@信息,则默认创建的用户host是%。



如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
SQL 关系型数据库 MySQL
mysql用户权限设置
mysql用户权限设置
|
2月前
|
网络协议 关系型数据库 MySQL
mysql8.0远程连接权限设置
mysql8.0远程连接权限设置
73 0
|
3月前
|
关系型数据库 MySQL Docker
MySQL 5.7 timestamp类型设置default value为'0000-00-00 00:00:00'报错的解决方法
MySQL 5.7 timestamp类型设置default value为'0000-00-00 00:00:00'报错的解决方法
|
1月前
|
关系型数据库 MySQL 数据库
mysql添加用户并设置数据库权限
mysql添加用户并设置数据库权限
|
2天前
|
关系型数据库 MySQL 数据库
mysql 设置环境变量与未设置环境变量连接数据库的区别
设置与未设置MySQL环境变量在连接数据库时主要区别在于命令输入方式和系统便捷性。设置环境变量后,可直接使用`mysql -u 用户名 -p`命令连接,而无需指定完整路径,提升便利性和灵活性。未设置时,需输入完整路径如`C:\Program Files\MySQL\...`,操作繁琐且易错。为提高效率和减少错误,推荐安装后设置环境变量。[查看视频讲解](https://www.bilibili.com/video/BV1vH4y137HC/)。
17 3
mysql 设置环境变量与未设置环境变量连接数据库的区别
|
12天前
|
SQL 关系型数据库 MySQL
【MySQL】DQL-基础查询-语句&演示(查询多个字段 / 所有字段/并设置别名/去重)
【MySQL】DQL-基础查询-语句&演示(查询多个字段 / 所有字段/并设置别名/去重)
|
14天前
|
存储 关系型数据库 MySQL
linux安装MySQL8.0,密码修改权限配置等常规操作详解
linux安装MySQL8.0,密码修改权限配置等常规操作详解
|
18天前
|
安全 关系型数据库 MySQL
node实战——后端koa结合jwt连接mysql实现权限登录(node后端就业储备知识)
node实战——后端koa结合jwt连接mysql实现权限登录(node后端就业储备知识)
23 3
|
1月前
|
SQL 关系型数据库 MySQL
MySql创建用户并配置权限
MySql创建用户并配置权限
19 0
|
2月前
|
安全 关系型数据库 应用服务中间件
连接rds设置网络权限
连接阿里云RDS需关注:1) 设置白名单,允许特定IP访问;2) 选择合适网络类型,如VPC或经典网络;3) 确保VPC内路由与安全组规则正确;4) 同VPC内可使用内网地址连接;5) 可启用SSL/TLS加密增强安全性。记得遵循最小权限原则,确保数据库安全。不同服务商操作可能有差异,但基本流程相似。
26 9