數據庫是信息系統中很是重要的一個環節,合理高效的對它進行管理是很重要的工做。一般是由總管理員建立不一樣的管理帳戶,而後分配不一樣的操做權限,把這些帳戶給相應的管理人員使用。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 distributionmysql> 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: 1mysql> 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