---
title: "mysql 创建新用户相关"
author: "William_Third"
date: "2013-01-30T21:14:27Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2013-01-30T21:21:44Z"
id: 1780010
url: "https://william-third.iteye.com/blog/1780010"
---

首先以 root权限登录
创建用户

```sql
create user username identified by 'password';
```

该用户可在任意地方登录数据库


```sql
create user username@ip identified by 'password';
```

限定登录主机

给新用户授权

```sql
grant privileges on database.table to user@ip identified by "password"
```

   select, insert, update, delete, all

通过这种方法，对数据库的修改立即写入内存，即时生效，无须重启服务器

但是以新增用户登录后，提示
ERROR 1045 (28000): Access denied for user 'database'@'localhost' (using password: YES)

需要删除匿名用户

以root身份登录

```sql
use mysql
select host, user from mysql;
```

发现存在若干个user项为空的记录，删除之

```sql
delete from mysql where user="";
```


操作仍然涉及权限，但非grant操作未即时写入内存，或者重启，或者 
```sql
flush privileges;
```