专栏首页CaiRuiMysql的用户基本操作

Mysql的用户基本操作

创建用户:

mysql> create user 'cai'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;                                                            
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| cai           | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
| wordpress     | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)
[root@cairui ~]# mysql -ucai -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 275
Server version: 5.7.21-debug Source distribution

Copyright (c) 2000, 2018, 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.

授权:

命令:GRANT privileges ON databasename.tablename TO 'username'@'host'

mysql> grant select,delete,create,insert,update on aa.* to 'cai'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cai@localhost;
+-----------------------------------------------------------------------------+
| Grants for cai@localhost                                                    |
+-----------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cai'@'localhost'                                     |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON `aa`.* TO 'cai'@'localhost' |
+-----------------------------------------------------------------------------+
2 rows in set (0.00 sec)

撤销用户权限:

命令:REVOKE privilege ON databasename.tablename FROM 'username'@'host';

mysql> show grants for cai@localhost;
+-----------------------------------------------------------------------------+
| Grants for cai@localhost                                                    |
+-----------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cai'@'localhost'                                     |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON `aa`.* TO 'cai'@'localhost' |
+-----------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> revoke insert on aa.* from 'cai'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for cai@localhost;
+---------------------------------------------------------------------+
| Grants for cai@localhost                                            |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cai'@'localhost'                             |
| GRANT SELECT, UPDATE, DELETE, CREATE ON `aa`.* TO 'cai'@'localhost' |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec) 

设置与修改用户密码:

  方法一:

[root@cairui ~]# mysqladmin -ucai -p'123456' password '123'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@cairui ~]# mysql -ucai -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 279
Server version: 5.7.21-debug Source distribution

Copyright (c) 2000, 2018, 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> update mysql.user set authentication_string=password('cai') where user='cai' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> flush privileges;
[root@cairui ~]# mysql -ucai -pcai;
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 284
Server version: 5.7.21-debug Source distribution

Copyright (c) 2000, 2018, 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> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@cairui ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 288
Server version: 5.7.21-debug Source distribution

Copyright (c) 2000, 2018, 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> 

忘记密码情况下的登录:

[root@cairui mysql]# bin/mysqld_safe --skip-grant-tables & 
#登录时不需要密码,进入可修改密码

删除用户:

mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| cai           | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
| wordpress     | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)

mysql> drop user 'cai'@'localhost';
Query OK, 0 rows affected (0.00 sec)

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • my php & mysql FAQ

    php中文字符串长度及定长截取问题使用str_len("中国") 结果为6,php系统默认一个中文字符长度为3,可改用mb_strlen函数获得长度,mb_su...

    财主刀刀
  • MySQL和Oracle的添加字段的处理差别 (r10笔记第73天)

    昨天在微信群中有个朋友也是无意中问了一下,说数据库中的表字段想保持一种相对规范的顺序,怎么办?要知道Oracle中这个操作就比较纠结了,因为是按照追加的方式来处...

    jeanron100
  • mysql复制学习二 安装及首次复制配置

    安装 下载rpm版本 server 安装  rpm -ivh MySQL-server-5.5.24-1.linux2.6.i386.rpm  出错1 ...

    财主刀刀
  • mysql的空值与NULL的区别

    Mysql数据库是一个基于结构化数据的开源数据库。SQL语句是MySQL数据库中核心语言。不过在MySQL数据库中执行SQL语句,需要小心两个陷阱。   陷阱一...

    李海彬
  • mysql复制学习一

    mysql复制传统上是基于语句的复制,5.0实现了基于行的复制。基于语句复制将执行语句及执行信息写入二进制日志中。 二进制日志包含 binlog和索引文件。bi...

    财主刀刀
  • MySQL修改数据类型的问题总结(r10笔记第74天)

    昨天快下班的时候,突然开发的同事找我说有个紧急需求,负责这个业务的DBA同事回家了,想让我帮忙看看,运行个SQL语句,几秒钟就好。我一听,就本着人道主义的精神留...

    jeanron100
  • MySQL 5.7安装部署总结(r10笔记第77天)

    之前搭建MySQL环境都是使用公司内部使用的脚本,其实说实话屏蔽了很多细节,对MySQL的安装还是了解比较肤浅,今天有个MySQL 5.7的数据迁移的任务,也...

    jeanron100
  • mysql_stmt_prepare failed! error(1461)Can't create more than

    1461错误, mysql_stmt_prepare failed! 今天现场咨询我们问如何处理1461错误。 mysql_stmt_prepare faile...

    李海彬
  • mysql 高并发更新计数问题

    问题:功能为链接的点击计数,其他两个计数更新 还有两个内容表的插入,只插入链接对应的id,分4个库,共128个表,mysql每天有5000万次插入和5000万次...

    财主刀刀
  • mysql DUPLICATE KEY UPDATE 问题

    DUPLICATE KEY UPDATE batch执行时出死锁错误 背景知识 一、 mysql  insert 与 duplicate key: 典型的插入语...

    财主刀刀

扫码关注云+社区

领取腾讯云代金券