1. 程式人生 > >Mysql新建用戶和數據庫並授權

Mysql新建用戶和數據庫並授權

mysql


一、新建用戶

//登錄MYSQL

[email protected]:~# mysql -uroot -p

Enter password: 密碼

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 138

Server version: 5.5.53-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2016, 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> insert into mysql.user(Host,User,Password) values ("localhost","zhouyuyao",password("Zhouyuyao123"));

mysql> insert into mysql.user(Host,User,Password) values ("%","zhouyuyao",password("Zhouyuyao123"));


//刷新系統權限表

mysql> flush privileges;

這樣就創建了一個名為:zhouyuyao 密碼為:Zhouyuyao123 的用戶。


二、登錄測試

mysql>exit;

@>mysql -u zhouyuyao -p

@>輸入密碼

mysql>登錄成功


三、用戶授權

//登錄MYSQL

@>mysql -u root -p

@>密碼

//首先為用戶創建一個數據庫(test)

mysql>create database test;


//授權zhouyuyao 用戶擁有test數據庫的所有權限

mysql> grant all privileges on test.* to [email protected]‘%‘ identified by ‘Zhouyuyao123‘;


//刷新系統權限表

mysql> flush privileges;

mysql> 其它操作


四、部分授權

mysql>grant select,update on test.* to [email protected]

identified by ‘cplusplus.me‘;


//刷新系統權限表

mysql> flush privileges;


五、刪除用戶

@>mysql -u root -p

@>密碼

mysql>DELETE FROM user WHERE User=”zhouyuyao” and Host=”localhost”;

mysql> flush privileges;


六、刪除數據庫

mysql>drop database test;


七、修改密碼

@>mysql -u root -p

@>密碼

mysql>update mysql.user set password=password(‘新密碼’) where

User=”zhouyuyao” and Host=”localhost”;

mysql> flush privileges;



Mysql新建用戶和數據庫並授權