---
title: "mysql IP限制 iP授权"
author: "igogo007"
date: "2015-11-11T11:20:43Z"
tags: ["MySQL"]
categories: ["数据库"]
slug: "mysql-ip-ip"
draft: false
lastmod: "2015-11-11T11:20:43Z"
id: 2256125
url: "https://igogo007.iteye.com/blog/2256125"
---

远程连接Mysql时提示如下：
错误码：1045
Access denied for user 'root@xx.xx.xx.xx'(using password:YES)

这个提示通常是由于Mysql默认的IP限制原因。

登录Mysql后执行

```java

mysql>use mysql;
mysql>select user,host from user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
4 rows in set (0.00 sec)

```


这里把本机IP授权后即可登录了，具体操作如下：

```java

mysql>grant all on *.* to root@'10.190.0.118' identified by 'mysqlpwd';
Query OK, 0 rows affected (0.00 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>select user,host from user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | 10.190.0.118          |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
5 rows in set (0.00 sec)

```


mysql grant语法如下：
grant 权限名（所有的权限用all） on 库名（*全部）.表名（*全部） to 要授权的用户名@’IP地址’(可以用%表示所有的IP，也可以指定一个IP或IP段） identified by “密码”；

可以指定权限，例如：

```java

GRANT select,insert,update,delete ON dbname.* to 'test_user'@'10.190.0.118'  identified by 'mysqlpwd'

```


收回权限，可以使用revoke，语法基本差不多，例如：

```java

revoke select,insert,update,delete ON dbname.* to 'test_user'@'10.190.0.118'  identified by 'mysqlpwd'

```


最后记得操作完成要刷新权限，才能生效：

```java

mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>quit;
Bye

```