mysql用户管理

1.创建一个普通用户并授权
[root@gary-tao ~]# mysql -uroot -p'szyino-123'
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 24 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> grant all on *.* to 'user1'@'127.0.0.1' identified by 'szyino-123'; //创建一个普通用户并授权 Query OK, 0 rows affected (0.00 sec) 
用法解释说明:
  • grant:授权;
  • all:表示所有的权限(如读、写、查询、删除等操作);
  • .:前者表示所有的数据库,后者表示所有的表;
  • identified by:后面跟密码,用单引号括起来;
  • 'user1'@'127.0.0.1':指定IP才允许这个用户登录,这个IP可以使用%代替,表示允许所有主机使用这个用户登录;
2.测试登录
[root@gary-tao ~]# mysql -uuser1 -pszyino-123 //由于指定IP,报错不能登录
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES) [root@gary-tao ~]# mysql -uuser1 -pszyino-123 -h127.0.0.1 //加-h指定IP登录,正常 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 26 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> mysql> grant all on *.* to 'user1'@'localhost' identified by 'szyino-123'; //授权localhost,所以该用户默认使用(监听)本地mysql.socket文件,不需要指定IP即可登录 Query OK, 0 rows affected (0.00 sec) mysql> ^DBye [root@gary-tao ~]# mysql -uuser1 -pszyino-123 //正常登录 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 28 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> 
3.查看所有授权
mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*B1E761CAD4A61F6FD6B02848B5973BC05DE1C315' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) 
4.指定用户查看授权
mysql> show grants for user1@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B1E761CAD4A61F6FD6B02848B5973BC05DE1C315' | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) 
注意:假设你想给同个用户授权增加一台电脑IP授权访问,你就可以直接拷贝查询用户授权文件,复制先执行一条命令再执行第二条,执行的时候把IP更改掉,这样就可以使用同个用户密码在另外一台电脑上登录。

常用sql语句

1.最常见的查询语句

第一种形式:

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select count(*) from mysql.user; +----------+ | count(*) | +----------+ | 8 | +----------+ 1 row in set (0.00 sec) //注释:mysql.user表示mysql的user表,count(*)表示表中共有多少行。

第二种形式:

mysql> select * from mysql.db;//它表示查询mysql库的db表中的所有数据mysql> select db from mysql.db; +---------+ | db | +---------+ | test | | test\_% | +---------+ 2 rows in set (0.00 sec) //查询db表里的db单个字段 mysql> select db,user from mysql.db; +---------+------+ | db | user | +---------+------+ | test | | | test\_% | | +---------+------+ 2 rows in set (0.00 sec) //查看db表里的db,user多个字段 mysql> select * from mysql.db where host like '192.168.%'\G; //查询db表里关于192.168.段的ip信息 
2.插入一行
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> select * from db1.t1; Empty set (0.00 sec) mysql> insert into db1.t1 values (1, 'abc'); //插入一行数据 Query OK, 1 row affected (0.01 sec) mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | abc | +------+------+ 1 row in set (0.00 sec) mysql> insert into db1.t1 values (1, '234'); Query OK, 1 row affected (0.00 sec) mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | abc | | 1 | 234 | +------+------+ 2 rows in set (0.00 sec) 
3.更改表的一行。
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from db1.t1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 1 | aaa | +------+------+ 2 rows in set (0.00 sec) 
4.清空某个表的数据
mysql> truncate table db1.t1;  //清空表
Query OK, 0 rows affected (0.03 sec)mysql> select * from db1.t1;
Empty set (0.00 sec) mysql> desc db1.t1; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | char(40) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) 
5.删除表
mysql> drop table db1.t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
6.删除数据库
mysql> drop database db1;
Query OK, 0 rows affected (0.00 sec)

mysql数据库备份恢复

1.备份恢复库
[root@gary-tao ~]# mysqldump -uroot -pszyino-123 mysql > /tmp/mysql.sql  //备份库
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao ~]# mysql -uroot -pszyino-123 -e "create database mysql2"  //创建一个新的库
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao ~]# mysql -uroot -pszyino-123 mysql2 < /tmp/mysql.sql //恢复一个库 Warning: Using a password on the command line interface can be insecure. [root@gary-tao ~]# mysql -uroot -pszyino-123 mysql2 Warning: Using a password on the command line interface can be insecure. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> select database(); +------------+ | database() | +------------+ | mysql2 | +------------+ 1 row in set (0.00 sec) 
2.备份恢复表
[root@gary-tao ~]# mysqldump -uroot -pszyino-123 mysql user > /tmp/user.sql  //备份表
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao ~]# mysql -uroot -pszyino-123 mysql2 < /tmp/user.sql  //恢复表
Warning: Using a password on the command line interface can be insecure.
3.备份所有库
[root@gary-tao ~]# mysqldump -uroot -pszyino-123 -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao ~]# less /tmp/mysql_all.sql
4.只备份表结构
[root@gary-tao ~]# mysqldump -uroot -pszyino-123 -d mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.

转载于:https://www.cnblogs.com/pta188/p/9206738.html

13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢复相关推荐

  1. MySQL、Postgresql、Oracle常用SQL语句(主要演示下DDL的区别)

    SQL分类 一.DDL(Data Definition Language)数据定义语言 用来定义数据库对象:数据库,表,列等.关键字:create, drop,alter 等 DDL对于不同的数据库实 ...

  2. 常用sql语句整理:mysql

    增 增加一张表 CREATE TABLE `table_name`(...)ENGINE=InnoDB DEFAULT CHARSET=utf8; 增加记录 INSERT INTO `your_tab ...

  3. 13.4 MySQL用户管理;13.5 常用sql语句;13.6 MySQL数据库备份恢复

    扩展 : SQL语句教程 http://www.runoob.com/sql/sql-tutorial.html 什么是事务?事务的特性有哪些? http://blog.csdn.net/yenang ...

  4. MySQL用户管理、常用sql语句、数据库备份

    13.4 MySQL用户管理 创建用户并授权 指定登录IP [root@centos-01linux ~]# mysql -uroot -p Enter password: Welcome to th ...

  5. MySQL(用户管理,常用sql语句,数据库备份恢复,MySQL调优,恢复误操作数据)...

    一.MySQL用户管理. 一个MySQL数据库里可以跑多个库,总不能给所有人的程序员root用户,则可以给他们单独的用户访问数据库. 创建用户:(grant all on *.* to 'user1' ...

  6. 0521MySQL常用操作---设置更改root密码、数据库备份恢复、连接mysql、mysql用户管理...

    2019独角兽企业重金招聘Python工程师标准>>> 13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令 13.4 mysql用户管理 13.5 ...

  7. ORACLE常用SQL语句大全

    ORACLE常用SQL语句大全 DDL:数据库定义语言(CREATE.ALTER.DROP.TRUNCATE.COMMENT.RENAME),用来创建数据库中的表.索引.视图.存储过程.触发器等对象的 ...

  8. mysql备份数据库语句6_13.4 MySQL用户管理;13.5 常用sql语句;13.6 MySQL数据库备份恢复...

    扩展 : SQL语句教程 什么是事务?事务的特性有哪些? 根据binlog恢复指定时间段的数据 mysql字符集调整 使用xtrabackup备份innodb引擎的数据库  innobackupex  ...

  9. mysql用户管理,常用sql语句,mysql数据库备份恢复

    2019独角兽企业重金招聘Python工程师标准>>> mysql用户管理 grant all on . to 'user1' identified by 'passwd'; gra ...

最新文章

  1. VC编写自己构造http协议数据的post上传图片类(MFC环境 带编码转换)(转)
  2. 时间复杂度和空间复杂度3 - 数据结构和算法05
  3. 主流html5桌面应用开发,主流HTML5开发工具推荐
  4. VTK:Utilities之Box
  5. 昆仑通态触摸屏数据转发上传_嵌_ModbusTcpIp数据转发 昆仑通态屏与屏之间通讯 - 下载 - 搜珍网...
  6. 工程实践之 复杂保存解构 TODO
  7. 表、数据的增删改查(所有列、指定列、添加常量、字段起别名、和并列、去除重复、条件查询、比较、判断空、模糊查询)...
  8. Linux之find学习--详解
  9. 【java】判断某段字符串的编码方式,并按照新的编码方式输出
  10. spring cloud config git库文件搜索顺序
  11. Python新闻网站项目-3.Gerapy爬虫分布式部署
  12. 遥感计算机的分类原理,遥感图像的计算机分类
  13. 实用:旋转矩阵与方向余弦矩阵(DCM)
  14. EasyRecovery2022真正不收费的数据恢复软件
  15. Docker容器获取宿主机信息
  16. html表格标题居于标题左侧,css如何设置表格标题(caption标签)的位置
  17. C++中的重载丶重写丶重定义丶重定向的区别
  18. 小米MIUI或其他手机黑暗深色模式部分控件失效无效
  19. ak和sk的意思及用法
  20. CentOS 7无法启动,在进度条卡死问题解决

热门文章

  1. python pygame sdl2教程_无法安装pygame sdl2
  2. 卷积神经网络训练准确率突然下降_基于联邦学习和卷积神经网络的入侵检测方法...
  3. python 函数式_10分钟学习函数式Python
  4. mysql 查询一张表在另一张表中不存在的数据
  5. elementui 单独修改一个label的样式
  6. 关于文件路径的斜杠和反斜杠问题
  7. 蠕虫mysql_mysql蠕虫复制基础知识点
  8. VBS操作注册表设置新建读取,删除等操作(更新中)
  9. 在UAP中如何通过WebView控件进行C#与JS的交互
  10. 网络工程师成长日记382-西部数据Juniper网络设备调试