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

目录

一、mysql用户管理
二、常用sql语句
三、mysql数据库备份恢复

一、mysql用户管理

mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '12345a';
Query OK, 0 rows affected (0.00 sec)

-授予所有权限给user1,指定user1只能通过ip地址127.0.0.1登录,user1认证密码为12345a。
*.* 表示所有的库中所有的表,第一个*表示所有的库,第二个*表示所有的表。
127.0.0.1可以写成%,此时%表示所有的ip。
grant的命令语句不会被记录到命令历史中。

  • 测试
[root@minglinux-01 ~] mysql -uuser1 -p12345a
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)  
//不指定登录地址时默认会从socket登录  
//由于user1用户已被指定使用127.0.0.1地址登录,所以这里无法登录

[root@minglinux-01 ~] mysql -uuser1 -p12345a -h127.0.0.1
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 4
Server version: 5.6.39-log MySQL Community Server (GPL)

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> 
  • 对user1重新授权
mysql> grant all on *.* to 'user1'@'localhost' identified by '12345a'; //localhost即针对socket
Query OK, 0 rows affected (0.00 sec)  

mysql> quit        //登出mysql时可使用quit、exit或ctrl+d
Bye
[root@minglinux-01 ~] mysql -uuser1 -p12345a 
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 6
Server version: 5.6.39-log MySQL Community Server (GPL)

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> 
  • 部分授权
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.162.130' identified by 'passwd';

授予user2用户对库db1下的所有表拥有SELECT,UPDATE,INSERT权限,限定user2从192.168.31.129登录,登录密码12345a

  • 查看授权
mysql> show grants;  //默认查看当前登录用户的授权
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@localhost                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*8CED534B5F3A666C88EF673FF78C45C2A846521D' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> 
mysql> show grants for user1@`127.0.0.1`;  // 查看指定用户user1的授权
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*8CED534B5F3A666C88EF673FF78C45C2A846521D' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants\G;
*************************** 1. row ***************************
Grants for user1@localhost: GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*8CED534B5F3A666C88EF673FF78C45C2A846521D'
1 row in set (0.00 sec)

ERROR: 
No query specified

即使不知道user1的密码,只要将show grants for user1@127.0.0.1命令显示的grant命令复制,用户名不变,修改地址为localhost后执行就可以让user1@localhost拥有和user1@127.0.0.1相同的密码和权限。

即使不知道user1@localhost的密码,也可以用grant为user1'@'localhost添加权限。

二、常用sql语句

  • 查看db表的行数
mysql> select count(*) from mysql.db; 
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
  • 查看mysql库下的db表中的所有数据
mysql> select * from mysql.db\G;
*************************** 1. row ***************************
                 Host: %
                   Db: test
                 User: 
          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: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
*************************** 2. row ***************************
                 Host: %
                   Db: test\_%
                 User: 
          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: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
2 rows in set (0.00 sec)

ERROR: 
No query specified
  • 查询db表中db字段的值
mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
+---------+
2 rows in set (0.00 sec)
  • 查询db表中db和user字段的值
mysql> select db,user from mysql.db;
+---------+------+
| db      | user |
+---------+------+
| test    |      |
| test\_% |      |
+---------+------+
2 rows in set (0.00 sec)
  • 在db表中模糊查询host字段匹配192.168.*的行
mysql> select * from mysql.db where host like '192.168.%'\G;  //like代表模糊匹配
Empty set (0.01 sec)

ERROR: 
No query specified
  • 在表中插入数据
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.01 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.00 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)
  • 更改表的某一行
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)
  • 清空某个表的数据
mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)

清空db1库中t1表的全部数据,但表结构仍然保留

  • 删除表
mysql> drop table db1.t1;
Query OK, 0 rows affected (0.00 sec)
  • 删除数据库
mysql> drop database db1;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

三、mysql数据库备份恢复

  • MySQL 备份
[root@minglinux-01 ~] mysqldump -uroot -p123456 mysql > /tmp/mysqlbak.sql 
Warning: Using a password on the command line interface can be insecure.
[root@minglinux-01 ~] ls /tmp/mysqlbak.sql
/tmp/mysqlbak.sql

使用root用户通过socket登录备份数据库中的mysql库,将备份的内容重定向到/tmp/mysqlbak.sql中

  • MySQL 恢复
[root@minglinux-01 ~] mysql -uroot -p123456 -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
[root@minglinux-01 ~] mysql -uroot -p123456 mysql2 < /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@minglinux-01 ~] mysql -uroot -p123456 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 11
Server version: 5.6.39-log MySQL Community Server (GPL)

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> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)
mysql> show tables;
+---------------------------+
| Tables_in_mysql2          |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)
mysql> use mysql;    //
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;  //查看mysql库中的表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

  • 备份表
[root@minglinux-01 ~] mysqldump -uroot -p123456 mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
  • 恢复表
[root@minglinux-01 ~] mysql -uroot -p123456 mysql2 < /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.

备份使用mysqldump,恢复使用mysql。
将mysql下的user表备份重定向为到/tmp/user.sql,然后将其恢复到库mysql2下的user表。
恢复时,若需要恢复的库或表已存在则会先执行drop,再重新创建库或表,然后再一步步插入每一行的数据。

  • 备份所有库
[root@minglinux-01 ~] mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql  
Warning: Using a password on the command line interface can be insecure.
//-A表示所有库
  • 仅备份表结构
[root@minglinux-01 ~] mysqldump -uroot -p123456 -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
-d指定只备份表结构,不备份数据

Mysqldump适用于备份量较小时使用,当数据量较大(GB级或以上)时可能会备份的很慢,此时建议使用其他备份工具。

扩展

SQL语句教程 http://www.runoob.com/sql/sql-tutorial.html
什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
根据binlog恢复指定时间段的数据 https://blog.csdn.net/lilongsy/article/details/74726002
相关扩展 https://blog.csdn.net/linuxheik/article/details/71480882
mysql字符集调整 http://xjsunjie.blog.51cto.com/999372/1355013
使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份 http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql
相关视频
链接:http://pan.baidu.com/s/1miFpS9M 密码:86dx
链接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容