数据库创建用户及用户权限

一,添加用户,设置用户操作权限

1,mysql -u root -p     登陆mysql

2,创建一个数据库用户名为“user”,密码为“123456”的用户

    2.1,create user user@localhost indentified by '123456';

   或者

    2.2,insert into mysql.user(Host,User,Password) values("localhost","user",password("123456"));

注意:mysql5.7.22-log

添加用户 insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

报以下的错误 ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value错误

mysql5.1以上版本,我是在5.6版本上操作的。

 GRANT USAGE ON *.* TO 'user01'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;

3,create database mydb       创建数据库mydb

4, 授权user用户拥有testDB数据库的所有权限(某个数据库的所有权限):

      mysql>grant all privileges on testDB.* to user@localhost identified by '123456';

   mysql>flush privileges;//刷新系统权限表

   格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

5, 指定部分权限给一用户

      格式:grant select on 数据库.* to 用户名@登录主机 identified by '密码'

    例1,查询、插入、修改、删除的权限。首先用以 root 用户连入 MySQL,然后键入以下命令:
   grant select,insert,update,delete,drop on mydb.* to user@localhost identified by '123456';
 或者

            grant all on mydb.* to user@localhost identifiedby 'password';
   grant all privileges on mydb* to user@localhost identified by 'password';

    例 2:如果你不想 user有密码操作数据库“mydb”里的数据表,可以再打一个命令将密码消掉。
   grant select,insert,update,delete on mydb.* to user@localhost identified by '';

    #注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。
  如果想远程登录的话,将"localhost"改为"%",
  表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。
6,刷新一下权限

  flush privileges;

7,查看当前用户(自己)权限:

 show grants;

 查看其他 MySQL 用户权限:

show grants for zhangkh@localhost;

mysql5.6&5.7创建新用户并给授权指定的数据库权限

案例参考:https://www.cnblogs.com/pkjplayer/p/7681281.html

1、使用 root 管理员登陆 mysql
mysql -uroot -p;
2、创建新用户
CREATE USER 'CPSAdmin'@'%' IDENTIFIED BY 'CPSAdmin123'; 
'%' - 所有情况都能访问
‘localhost’ - 本机才能访问
’111.222.33.44‘ - 指定 ip 才能访问
3、给该用户添加权限
grant all privileges on gps_data.* to 'CPSAdmin'@'%';
all privileges 可以替换为 select,delete,update,insert,create,drop
4、删除用户
Delete FROM mysql.user Where User='user1';
四、可能遇到的问题
flush privileges;

 

二,删除用户

 @>mysql -u root -p

 @>密码

  mysql>Delete FROM user Where User='test' and Host='localhost';

  mysql>flush privileges;

  mysql>drop database testDB; //删除用户的数据库

    删除账户及权限:

   >drop user 用户名@'%';

  >drop user 用户名@ localhost; 

三,修改指定用户密码

   @>mysql -u root -p

  @>密码

  mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

  mysql>flush privileges;

=======================================

1. 列出所有数据库

  mysql>show database;

2. 切换数据库

  mysql>use '数据库名';

3. 列出所有表

  mysql>show tables;

4. 显示数据表结构

  mysql>describe 表名;

5. 删除数据库和数据表

  mysql>drop database 数据库名;

  mysql>drop table 数据表名;

 

你可能感兴趣的:(mysql)