1、基本的用户管理mysql
新安装的mysql默认只有root用户,若是全部应用都使用root用户,将涉及到许多的权限与安全问题,因此须要建立不一样的用户并设置密码来执行不一样的操做。sql
mysql中的用户与密码存在mysql库的user表里。数据库
登陆mysql使用以下命令查看用户及密码安全
mysql>select User,Password from mysql.user;
下面演示建立、删除用户与密码设置ide
mysql> create user mytest1; Query OK, 0 rows affected (0.01 sec)
2.设置密码加密
mysql> set password for mytest1 = password('mi123123'); Query OK, 0 rows affected (0.00 sec)
2.使用password('密码')方式为要设置的密码进行加密,否则就会明文保存,不安全。spa
咱们还能够在建立用户的同时设置密码对象
mysql> create user mytest2 identified by 'my123123'; Query OK, 0 rows affected (0.00 sec)
3.查看新建的用户以及设置的密码input
mysql> select User,Password from user; +---------+-------------------------------------------+ | User | Password | +---------+-------------------------------------------+ | root | | | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | mytest1 | *BB9971EEFFFEF5DB7694D41750FC84489F86A753 | +---------+-------------------------------------------+
4.若是须要更改用户的名称,可使用rename user改变用户名称it
mysql> rename user mytest1 to mytest3; Query OK, 0 rows affected (0.00 sec)
5.再也不须要的用户能够进行删除
mysql> drop user mytest2; Query OK, 0 rows affected (0.00 sec)
2、基本的权限管理
# mysql -u mytest3 -p'my123123' ERROR 1045 (28000): Access denied for user 'mytest3'@'localhost' (using password: YES)
5)identified by 设置用户访问密码,是可选的,若是没有,将不须要密码访问。
1.受权mydbuser用户在本机访问mysql时,对mydbtest库的全部表具备任意权限
mysql> grant all on mydbtest.* to mydbuser@'localhost' identified by 'my123'; Query OK, 0 rows affected (0.00 sec)
使用新用户访问
# mysql -u mydbuser -p'my123' Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.61 Source distribution Copyright (c) 2000, 2011, 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>
2.只为mydbuser2在mydbtest数据库中授予select、update、drop权限
mysql> grant select,update,drop on mydbtest.* to mydbuser2@'localhost' identified by 'my123'; Query OK, 0 rows affected (0.00 sec)
3.当权限不在须要的时候,使用revoke收回用户权限,能够所有收回,也可部分收回
首先来查看mydbuser在mydbtest中的权限,在mysql库中,有个db表,记录着每一个数据库及对应用户的权限
mysql> select * from mysql.db where user='mydbuser'\G *************************** 1. row *************************** Host: localhost Db: mydbtest User: mydbuser Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Grant_priv: N References_priv: Y Index_priv: Y Alter_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Execute_priv: Y Event_priv: Y Trigger_priv: Y 1 row in set (0.00 sec)
4.收回mydbuser对mydbtest库的delete、drop、index操做权限。
mysql> revoke delete,drop,index on mydbtest.* from mydbuser@'localhost'; Query OK, 0 rows affected (0.00 sec)
revoke与grant使用方法相似,只须要将to改为from便可。
5.收回mydbuser对mydbtest库全部的操做权限
mysql> revoke all on mydbtest.* from mydbuser@localhost; Query OK, 0 rows affected (0.00 sec)