Table of Contents
1. User management
1. Create a MySQL user
2. Delete MySQL user
3. Modify the MySQL user
2. Permission management
1. Permission description
2. Permission storage location (understand)
3. Authorize users
4. Query user permissions
5. with grant option option
6.revoke to recover permissions
Home Database Mysql Tutorial Summary and sharing of user creation and permission management in MySQL

Summary and sharing of user creation and permission management in MySQL

Sep 02, 2022 pm 04:59 PM
mysql

Recommended learning: mysql video tutorial

1. User management

atmysqlThere is a user table in the libraryYou can view the created users

1. Create a MySQL user

Note: Not available in MySQL To describe the user simply by username, the host must be added. Such as hhy@10.1.1.1

Basic syntax:

mysql> create user '用户名'@'被允许连接的主机名称或主机的IP地址' identified by '用户密码';
mysql> select user,host from mysql.user;
Copy after login

Case: Create a MySQL account, Username: hhy, user password: 123

mysql> create user 'hhy'@'localhost' identified by '123';
/*或*/
mysql> create user 'hhy'@'127.0.0.1' identified by '123';
Copy after login

Case: Create a MySQL account (requires opening a remote connection), host IP address: 192.1668.44.110, username: test, user password :123

mysql> create user 'test'@'192.1668.44.110' identified by '123';
Copy after login

Test:On the host with IP address 192.168.44.110

# yum install mysql -y
# mysql -h 192.168.44.110 -P 3306 -uharry -p
Enter password:123
Copy after login

Option description:192.168.44.110: MySQL server side IP address

yum installation mysql: represents the MySQL client installed
yum installation mysql-server: represents the installation of MySQL Server side

Case:Create a MySQL account (requires opening a remote connection), host IP network segment: 10.1.1.0, user name: jack, user password: 123

create user 'jack'@'192.168.44.%' identified by '123'
Copy after login

Case: Create a MySQL account (requires opening a remote connection), which is required to be open to all hosts, user name: root, user password: 123

create user 'root'@'%' identified by '123';
Copy after login

2. Delete MySQL user

Basic user:

mysql> drop user 'username'@'host name or IP address of the host';

Special Note:

If you do not specify the name of the host or the IP address of the host when deleting a user, all information about this account will be deleted by default.

Case: Delete the account hhy

drop user 'hhy'@'localhost';
Copy after login

Case: Delete the account jack

drop user 'jack'@'192.168.44.%';
Copy after login

Case:Create two harry accounts (localhost/10.1.1.23), and then delete one of them

mysql> create user 'harry'@'localhost' identified by '123';
mysql> create user 'harry'@'192.168.44.110' identified 

mysql> drop user 'harry'@'192.168.44.110';
Copy after login

Another way to delete the MySQL account

mysql> delete from mysql.user where user='root' and host='%';
mysql> flush privileges;
Copy after login

3. Modify the MySQL user

Special Note: MySQL user renaming can usually change two parts, one is the user's name, and the other is the host name or IP address of the host that is allowed to access.

Basic syntax:

mysql> rename user 旧用户信息 to 新用户信息;
Copy after login

Case: Change user 'root'@'%' to 'root'@'10.1.1. %'

mysql> rename user 'root'@'%' to 'root'@'10.1.1.%';
Copy after login

Case: Rename 'harry'@'localhost' to 'hhy'@'localhost'

mysql> create user 'tom'@'localhost' identified by '123';
mysql> rename user 'tom'@'localhost' to 'hhy'@'localhost';
Copy after login

Use update statement to update user information

mysql> update mysql.user set user='hhy',host='localhost' where user='tom' and host='localhost';

mysql> flush privileges;
Copy after login

2. Permission management

1. Permission description

All permission description

USAGE	无权限,只有登录数据库,只可以使用test或test_*数据库
ALL		所有权限
以下权限为指定权限
select/update/delete/super/replication slave/reload...
with grant option 选项表示允许把自己的权限授予其它用户或者从其他用户收回自己的权限
Copy after login

By default, if the with grant option is not specified when assigning permissions , means that this user cannot grant permissions to other users, but this permission allocation cannot exceed its own permissions.

2. Permission storage location (understand)

  • mysql.user:The account number and password of all mysql users, as well as the user's access to the entire database Table permissions (*.*)
  • mysql.db:Authorizations for non-mysql libraries are stored here (db.*)
  • mysql.table_priv: Authorization of a certain table in a certain database (db.table)
  • mysql.columns_priv :Authorization of a certain column in a certain table in a certain database (db.table.col1)
  • mysql.procs_priv :Authorization of stored procedures in a certain library

3. Authorize users

Create database table:

create database java;
use java;
create table tb_student(
	id mediumint not null auto_increment,
	name varchar(20),
	age tinyint unsigned default 0,
	gender enum('男','女'),
	address varchar(255),
	primary key(id)
) engine=innodb default charset=utf8;

insert into tb_student values (null,'刘备',33,'男','湖北省武汉市');
insert into tb_student values (null,'貂蝉',18,'女','湖南省长沙市');
insert into tb_student values (null,'关羽',32,'男','湖北省荆州市');
insert into tb_student values (null,'大乔',20,'女','河南省漯河市');
insert into tb_student values (null,'赵云',25,'男','河北省石家庄市');
insert into tb_student values (null,'小乔',18,'女','湖北省荆州市');
Copy after login

Basic syntax:

mysql> grant 权限1,权限2 on 库.表 to 用户@主机
mysql> grant 权限(列1,列2,...) on 库.表 to 用户@主机
Copy after login

Library.Table representation method: *.* represents all data tables in all databases, db_itheima.* represents all data in the db_itheima database Table, db_itheima.tb_admin, represents the tb_admin table in the db_itheima database

Case: Assign query permissions to the java database to the thy account

mysql> grant select on java.* to 'hehanyu'@'192.168.44.%';

mysql> flush privileges;
Copy after login

Case : Assign permissions to the java.tb_student data table to the hehanyu account (required to only change the age field)

mysql> grant update(age) on java.tb_student to 'hehanyu'@'192.168.44.%';

mysql> flush privileges;
Copy after login

Case: Add a root@% account, and then assign all permissions

create user 'root'@'%' identified by '123';
grant all on *.* to 'root'@'%';
flush privileges;
Copy after login

4. Query user permissions

Query current user permissions:

mysql> show grants;
Copy after login

Query other user permissions:

mysql> show grants for '用户名称'@'授权的主机名称或IP地址';
Copy after login

5. with grant option option

mysql> grant all on *.* to 'amy'@'10.1.1.%' identified by '123' with grant option;
mysql> grant all on *.* to 'harry'@'10.1.1.%' identified by '123';
Copy after login

As shown in the above command: amy has the function of granting permissions, but harry does not have the function of granting permissions.

If grant authorization does not have the with grant option option, it cannot authorize other users.

6.revoke to recover permissions

Basic syntax:

revoke 权限 on 库.表 from 用户;
查看hehanyu用户权限
mysql> show grants for 'hehanyu'@'192.168.44.%';
撤消指定的权限
mysql> revoke update on java.tb_student from 'tom'@'192.168.44.%';
撤消所有的权限
mysql> revoke select on java.* from 'tom'@'192.168.44.%';
Copy after login

推荐学习:mysql视频教程

The above is the detailed content of Summary and sharing of user creation and permission management in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles