MySQL操作用户、权限

注意:在MySQL环境中的命令,后面记得不要漏掉分号( ;)

1、操作用户

1.1 添加新用户

命令:create user 用户名 @ 主机名 identified by 密码;

mysql> create user 'user_1'@'%' identified by 'user_1';  //%允许外网连接,localhost只允许本地
Query OK, 0 rows affected (0.00 sec)

 1.2 用户重命名

命令:rename user ‘旧用户名’@‘主机’ to‘ 新用户名’@‘主机’;

mysql> rename user 'user_1'@'%' to 'user_2'@'%';
Query OK, 0 rows affected (0.00 sec)

1.3 修改用户密码

命令1: update user set authentication_string=password('密码') where user ='用户名';

mysql> update user set authentication_string=password('123456') where user ='user_1';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

命令2:set password for '用户名'@'主机'=password('密码');

mysql> set password for 'user_1'@'%'=password('111222');
Query OK, 0 rows affected, 1 warning (0.01 sec)

1.4 查询和删除用户

查询命令:use mysql;  select host, user from user;

删除命令:drop user 'user_1'@'%';

mysql>select  host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| %         | user_1        |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
4 rows in set (0.00 sec)

mysql> drop user 'user_1'@'%';

 

2、操作权限

2.1 将数据库授权给用户

命令:grant all privileges on 数据库名.* to '用户名'@'%' identified by '用户密码' with grant option;

  • all privileges : 表示将所有权限授予用户。(权限可叠加)
  • on : 表示这些权限对哪些数据库和表生效 ,格式:数据库名.表名。*:表示所有
  • to : 将权限授予哪个用户。格式:‘用户名’@‘登录IP或域名’ 。% 表示没有权限,在任何主机都可以登录。
  • identified by : 指定用户的登录密码
  • with grant option : 表示允许用户将自己的权限授权给其他用户
mysql> grant all privileges on db_1.* to 'user_1'@'%' identified by 'user_1' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

2.2 查看用户权限

命令:show grants for 用户名@主机

mysql> show grants for 'user_1'@'%';
+--------------------------------------------------+
| Grants for user_1@%                              |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'user_1'@'%'               |
| GRANT ALL PRIVILEGES ON `db_1`.* TO 'user_1'@'%' |
+--------------------------------------------------+
2 rows in set (0.00 sec)

2.3 删除用户权限

命令:revoke 权限名 on 数据库名.数据库表名 from 用户名@主机;

2.4 刷新权限

命令:flush privileges;

 

3、操作数据库

3.1 创建数据库

命令:create database 数据库名;

mysql> create database db_1;   //db_1是数据库名
Query OK, 1 row affected (0.01 sec)

3.2 查看所有数据库

命令:show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_1               |
| mysql              |
| performance_schema |
| ry-vue             |
| sys                |
| test1_db           |
+--------------------+
7 rows in set (0.00 sec)

3.3 选中数据库

命令: use 数据库名;

mysql> use mysql;
Database changed

3.4 删除数据库

命令: drop database 数据库名;

mysql> drop database test1_db;
Query OK, 0 rows affected (0.01 sec)

3.5 修改数据库名

先导出数据库文件(包含结构和数据),新建数据库,将数据库文件导入到新建的数据库中,删除原有数据库。

 

MySQL操作教程:https://www.runoob.com/mysql/mysql-tutorial.html

posted @ 2021-02-03 22:29  codeing123  阅读(149)  评论(0编辑  收藏  举报