美文网首页
mysql用户管理、常用sql语句、mysql数据库备份恢复

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

作者: XiaoMing丶 | 来源:发表于2018-12-06 23:35 被阅读0次

    目录

    一、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

    相关文章

      网友评论

          本文标题:mysql用户管理、常用sql语句、mysql数据库备份恢复

          本文链接:https://www.haomeiwen.com/subject/tnjacqtx.html