Sharing methods for creating new users and authorization in Mysql

This article summarizes and introduces the methods of creating new users and authorization in Mysql. It first introduces the author's own project experience, and then attaches reference articles. I hope it will be helpful to everyone in learning mysql.

During the project development process, you may need to open your own database to others, but for security reasons, you cannot open other databases in your server at the same time. Then you can create a new user and grant specific database permissions to the user.

Test environment: Centos 6.3 and Mysql 5.3

1. Create a new user

The code is as follows:
//Log in to MYSQL
@>mysql -u root -p
@>Password
//Create user
mysql> insert into mysql.user(Host,User,Password) values("localhost","cplusplus",password("cplusplus .me"));
//Refresh the system privileges table
mysql>flush privileges;

This creates a user named: cplusplus with the password: cplusplus.me.

2. Login test

1

2

3

4

mysql>exit;

@>mysql -u cplusplus -p

@>输入密码

mysql>登录成功

3. User authorization

1

2

3

4

5

6

7

8

9

10

//登录MYSQL

@>mysql -u root -p

@>密码

//首先为用户创建一个数据库(cplusplusDB)

mysql>create database cplusplusDB;

//授权cplusplus用户拥有cplusplusDB数据库的所有权限。

>grant all privileges on cplusplusDB.* to cplusplus@localhost identified by 'cplusplus.me';

//刷新系统权限表

mysql>flush privileges;

mysql>其它操作

4. Partial authorization

1

2

3

mysql>grant select,update on cplusplusDB.* to cplusplus@localhost identified by 'cplusplus.me';

//刷新系统权限表。

mysql>flush privileges;

5. Delete users

1

2

3

4

@>mysql -u root -p

@>密码

mysql>DELETE FROM user WHERE User="cplusplus" and Host="localhost";

mysql>flush privileges;

6. Delete database

1

mysql>drop database cplusplusDB;

7. Change password

1

2

3

4

@>mysql -u root -p

@>密码

mysql>update mysql.user set password=password('新密码') where User="cplusplus" and Host="localhost";

mysql>flush privileges;

Let me share with you an experience from a netizen:

1. Create a new user

Log in to mysql as user root and create a new user with the same name as the database.

1

mysql> INSERT INTO mysql.user(Host,User,Password) VALUES('localhost', 'sun', password('sun123456'));

Refresh system permission table

1

mysql> FLUSH PRIVILEGES;

If an error is reported

1

#1364 – Field ‘ssl_cipher' doesn't have a default value

Modify the MySQL configuration file to my.cnf for Linux systems and my.ini for Windows systems

1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

change into

1

sql_mode=NO_ENGINE_SUBSTITUTION

Restart the MySQL service

2. Authorize users

1

2

mysql> GRANT ALL ON sun.* to sun@localhost identified BY 'sun123456';

mysql> FLUSH PRIVILEGES;

Source: Weidian Reading    https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131808382