数据库是信息系统中非常重要的一个环节,合理高效的对它进行管理是很重要的工作。通常是由总管理员创建不同的管理账户,然后分配不同的操作权限,把这些账户给相应的管理人员使用。

       1.新建用户,有两种命令格式。

a.  新建用户的命令格式如下

create user ‘username’@’host’ [identified by [password] ‘password’;

其中:

     username : 将创建的用户名。

     host: 指定用户在哪些主机上可以登录,可以使用IP 地址、网段、主机名的形式,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登录,可以使用通配符% 。

   password:因为MYSQL5.7版本启用了密码增强插件,新密码必须符合密码复杂性要求。

mysql> create user 'test01'@'localhost' identified by '123abc';         //创建用户名为 test01 ,密码为 :123abc
Query OK, 0 rows affected (0.00 sec)

mysql> use mysql;                                                                         //先进入名为mysql 的库中          

mysql> select User,authentication_string,Host from user;                //查看系统用户 
+-----------+-------------------------------------------+-----------+
| User      | authentication_string                     | Host      |
+-----------+-------------------------------------------+-----------+
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| mysql.sys | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | %         |
| test01    | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |            //新建的用户 test01
+-----------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

b.新建用户还可以使用

grant all on *.* to 用户@主机 identified by 密码;

其中*.* 表示:所有数据库和所有表

mysql> grant all on *.* to 'test02'@'localhost' identified by '123abc';            //新建用户名为test02,密码为:123abc
Query OK, 0 rows affected, 1 warning (0.32 sec)

mysql> select User,authentication_string,Host from user;                                 //查看系统用户
+-----------+-------------------------------------------+-----------+
| User      | authentication_string                     | Host      |
+-----------+-------------------------------------------+-----------+
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| mysql.sys | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | %         |
| test01    | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| test02    | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |                  //新建用户test02
+-----------+-------------------------------------------+-----------+
5 rows in set (0.00 sec)

2.删除用户。

删除用户命令格式如下: drop user ‘username’@’host’ ;       删除新创建用户 test02

mysql> drop user 'test02'@'localhost';                                 //删除用户test02                                         
Query OK, 0 rows affected (0.00 sec)

mysql> select User,authentication_string,Host from user;
+-----------+-------------------------------------------+-----------+
| User      | authentication_string                     | Host      |
+-----------+-------------------------------------------+-----------+
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| mysql.sys | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | %         |
| test01    | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
+-----------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

删除 ‘test02’@’localhost’ 这个用户后,数据库中已经没有该用户了。既然用户是存储在user 表中,直接使用delect 语句也可以对它执行删除,但和drop 是有区别的。drop不止删除用户表,还会把相关的权限删除,而delect 只是删除用户,权限在数据库中依然存在。


3.重命名用户

重命名用户的命令格式如下:

rename user ‘old_user’@’host’  to ‘new_user’@’host’;

old_user 是旧的用户名,new_user  是新的用户名。

mysql> rename user 'test01'@'localhost' to 'user01'@'localhost';                 //将test01  重命名为 user01
Query OK, 0 rows affected (0.00 sec)

mysql> select User,authentication_string,Host from user;
+-----------+-------------------------------------------+-----------+
| User      | authentication_string                     | Host      |
+-----------+-------------------------------------------+-----------+
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| mysql.sys | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |
| root      | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | %         |
| user01    | *3620754A963ECB3D7296097F9DA00C1FA5476B03 | localhost |        //用户test01 已经改为 user01
+-----------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

4.给用户设置密码

       修改用户密码的方式有两种,可以修改当前登录用户的密码或者修改其他用户密码。

(1) 修改当前登录用户密码的命令格式如下:

set password = password(‘password’);

使用函数password() 对密码加密,退出后重新登录,需要使用新密码。

mysql> set password =password('abc123');                            // 设置当前登录用户的新密码
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit                                                       //退出重新登录
Bye
[root@bogon ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.17-log Source distribution

(2)修改其他用户密码的命令格式如下:

set password for ‘username’@’host’  =  password(‘password’);

mysql> set password for 'user01'@'localhost' = password('abc123');            //修改user01用户密码 为 abc123
Query OK, 0 rows affected, 1 warning (0.34 sec)

mysql> quit
Bye
[root@bogon ~]# mysql -u user01 –p                                //使用 user01 用户重新登录
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17-log Source distribution

5.忘记root密码的解决办法

       使用MySQL 时,如果忘记忘记了其他用户密码。可以使用root  用户重新设置,但是如果忘记root密码,就需要采取特殊的方法进行操作。 直接修改授权表可以修改root 密码,下面介绍它的使用步骤。

     (1)先退出mysql ,关闭mysql 服务,在其主配置文件中添加启动数据库语句


mysql> quit                            //先突出数据库
Bye
[root@bogon ~]# systemctl stop mysqld.service                         //停止mysql 服务
[root@bogon ~]# systemctl status mysqld.service                     //查看其状态
● mysqld.service - MySQL Server
    Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
    Active: inactive (dead) since 二 2018-08-28 15:19:52 CST; 9s ago
      Docs: man:mysqld(8)
           
http://dev.mysql.com/doc/refman/en/using-systemd.html
   Process: 8740 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
   Process: 8723 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  Main PID: 8744 (code=exited, status=0/SUCCESS)

[root@bogon ~]# vim /etc/my.cnf                      //  修改mysql 的主配置文件

[mysqld]                                                                    //在mysqld 下面插入下面 skip-grant-tables

skip-grant-tables                       //启用数据库,其作用是用户登录时不使用授权表,可以不使用密码直接登录
[root@bogon ~]# systemctl start mysqld.service                              //启动mysql 服务


(2)不使用密码直接登录到mysql,使用 update 修改root密码

[root@bogon ~]# mysql                                           //直接mysql 进入,不需要密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution

mysql> update mysql.user set authentication_string=password('123abc');     //使用update 修改密码
Query OK, 2 rows affected, 1 warning (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 1

mysql> quit
Bye

(3)。root 密码修改好后,再将主配置文件的  skip-grant-tables  删除,使用root 用户登录。



[root@bogon ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution