1.新建用户
[root@iZ23hdndo4vZ etc]# mysql -u root -p
Enter password: 输入密码Welcome to the MySQL monitor. Commands end with ; or \g.
.....
mysql> use mysql
Database changed
mysql> select user,host from user; //先查询一下mysql.user表中有几条记录
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)
mysql> update user set host='%' where user='root'; //这个地方是设置所有的外网ip都可以访问该数据库
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select user,host from user;
+------+------+
| user | host |
+------+------+
| root | % |
+------+------+
1 row in set (0.00 sec)
mysql> flush privileges; //刷新
Query OK, 0 rows affected (0.00 sec)
mysql> insert into mysql.user(Host,User,Password) values('localhost','yonghuming' ,password('mima')); //localhost表示本机用户,添加用户
Query OK, 1 row affected, 3 warnings (0.00 sec)
mysql> select user from user;
+------+
| user |
+------+
| root |
| yonghuming |
+------+
2 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建用户成功
2、给新建的用户设置某个数据库的操作权限
重新切回到root账户下
[root@iZ23hdndo4vZ ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> grant all privileges on databaseDB.* to yonghuming@localhost identified by 'mima';
or
mysql> grant select,update,insert on datavaseDB.* to yonghuming@localhost identified by 'mima';
解析:all privileges表示所有权限 grant 权限1,权限2,…权限n on 数据库名称。表名称 to 用户名@用户地址 identified by ‘连接口令’;
权限1,权限2,…权限n代表
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限。
当权限1,权限2,…权限n被all privileges或者all代替,表示赋予用户全部权限。
当数据库名称。表名称被*.*代替,表示赋予用户操作服务器上所有数据库所有表的权限。
用户地址可以是localhost,也可以是ip地址、机器名字、域名。也可以用’%’表示从任何地址连接。
‘连接口令’不能为空,否则创建失败。
Query OK, 0 rows affected (0.02 sec)
//
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.删除用户
[root@iZ23hdndo4vZ etc]# mysql -u root -p
Enter password: 输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
.....
mysql> use mysql
mysql>DELETE FROM user WHERE User=”yonghuming” and Host=”localhost”;
mysql>flush privileges;
//删除用户的数据库
mysql>drop database databaseDB;
4.修改指定用户密码
[root@iZ23hdndo4vZ etc]# mysql -u root -p
Enter password: 输入密码
mysql>update mysql.user set password=password(‘xinmima’) where User=”yonghuming” and Host=”localhost”;
mysql>flush privileges;
mysql>quit;
补充
mysql> grant all on databaseDB.* to 'username'@'%' identified by '密码' with grant option;
#允许账户username从任何主机连接到数据库databaseDB