mysql用户管理

  • 创建用户并授权登录
    • grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
      第一个*表示db_name;第二个*表示tb_name
      指定其来源IP127.0.0.1(只可通过此IP登录)也可以使用通配符%,代表所有IP
      identified by设置密码
      [root@akuilinux01 ~]# mysql -uuser1 -p123456 -h'127.0.0.1'
      mysql>
    • grant all on . to 'user2'@'localhost' identified by '123456';
      [root@akuilinux01 ~]# mysql -uuser2 -p123456
      mysql>
      通过本机登录,必须使用localhost
  • 对具体权限进行授权
    mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.21.128' identified by '123456';
    Query OK, 0 rows affected (0.00 sec)
    #创建user2用户,并授予其针对db1库SELECT,UPDATE,INSERT权限
    mysql>  grant all on db1.* to 'user'@'%' identified by '123456';
    Query OK, 0 rows affected (0.01 sec)
    #创建user用户,并针对所有IP授予其db1库所有权限
  • 查看权限命令
    mysql> show grants;
    #查看当前用户的权限
    mysql> show grants for user2@192.168.21.128;
    #查看指定用户的权限
  • 不知道密码的时候更改用于的权限
    mysql> GRANT USAGE ON *.* TO 'user2'@'192.168.21.128' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE44568DDA
    -> ;
    Query OK, 0 rows affected (0.00 sec)
    mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.21.128';
    Query OK, 0 rows affected (0.00 sec)
    mysql> show grants for user2@192.168.21.128;
    +-------------------------------------------------------------------------------------------------------------------+
    | Grants for user2@192.168.21.128                                                                                   |
    +-------------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'user2'@'192.168.21.128' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
    | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.21.128'                                               |
    +-------------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)

    常用sql语句

mysql> use db1;
Database changed
#选择库

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       12 |
+----------+
1 row in set (0.04 sec)
#查看指定库的内容的行数

mysql> select * from mysql.db\G;
#查看库的所有内容

mysql> select db,user from mysql.db;
#查看库指定内容

mysql> select * from mysql.db where host like '192.168.%'\G;
#查看某些IP对应的库内容,like表示匹配

mysql> create table t1(`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.39 sec)
#在db1库下创建表t1

mysql> select * from db1.t1;
Empty set (0.03 sec)
#查看表中信息:空表

mysql> insert into db1.t1 values(1,'abc');
Query OK, 1 row affected (0.09 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.08 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> delete from db1.t1 where id=1;
Query OK, 2 rows affected (0.10 sec)
#删除表中指定内容
mysql> select * from db1.t1;
Empty set (0.00 sec)

mysql> truncate db1.t1;
Query OK, 0 rows affected (0.09 sec)
#清空一个表中内容

mysql> drop table t1;
Query OK, 0 rows affected (0.04 sec)
#删除表
mysql> drop database db1;
Query OK, 0 rows affected (0.13 sec)
#删除库

mysql> use mysql;
mysql> delete from user where User='user1' and Host='127.0.0.1';
Query OK, 1 row affected (0.06 sec)
#删除用户,在删除用户前需要先指定表

MySQL数据库备份恢复