Mysql数据库用户管理

      数据库是信息系统中很是重要的一个环节,合理高效的对它进行管理是很重要的工做。一般是由总管理员建立不一样的管理帐户,而后分配不一样的操做权限,把这些帐户给相应的管理人员使用。html

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

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

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

其中:vim

     username : 将建立的用户名。ide

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

   password:由于MYSQL5.7版本启用了密码加强插件,新密码必须符合密码复杂性要求。ui

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

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

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 -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.17 Source distribution

相关文章
相关标签/搜索