How to create a MySQL user and grant permissions? For good security, separate user accounts need to be created for each application rather than root accessing the database. This will ensure that applications cannot access other applications' databases. Therefore mysql administrator (root) rights are required to create user accounts and assign permissions to the database.
For information, the MySQL root account is different from the system root account and there is no relationship between them. (Related recommendations: MySQL Tutorial)
1. Create a new user in MySQL
Use the root user with shell access to log in to the MySQL server and create a new user named "rahul". The following command only allows access to user rahul's MySQL server from the localhost system.
mysql> CREATE USER 'rahul'@'localhost' IDENTIFIED BY 'password';
Now assign permissions to a specific database. The following command will allow user rahul to have all permissions on the database "mydb".
mysql> GRANT ALL ON mydb.* TO 'rahul'@'localhost';
After creating the user and assigning the appropriate permissions, make sure to reload the permissions.
mysql> FLUSH PRIVILEGES;
2. Create a MySQL user with remote access
Allow any user to connect to the MySQL server from a remote system. You need to specify the hostname or IP address of the remote system. You can also use % to allow any host
mysql> CREATE USER 'rahul'@'123.45.67.89' IDENTIFIED BY 'password'; mysql> CREATE USER 'rahul'@'%' IDENTIFIED BY 'password'; mysql> FLUSH PRIVILEGES;
3. Grant specific user permissions in MySQL
The following is a list of common permissions for MySQL users. Visit here for a complete list of permissions for MySQL users.
ALL [PRIVILEGES] - Grants the user all permissions.
CREATE - Grants the user permission to create new databases and tables.
DROP - Grants the user permission to delete (drop) databases and tables.
DELETE - Grants the user permission to delete rows in the table.
ALTER - Grants the user permission to modify the table structure.
INSERT - Grants the user permission to insert (add) rows into the table.
SELECT - Grants the user permission to run the select command to read data from the table.
UPDATE - Grants the user permission to update data in the table.
EXECUTE - Grants the user permission to execute a stored routine.
FILE - Grants the user access to files on the server host.
GRANT OPTION - Grants a user the ability to grant or remove permissions from other users.
Here, you can specify comma-separated permissions instead of all permissions. For example, allow CREATE, DELETE, INSERT, UPDATE access to 'rahul'@'localhost' on database mydb.
mysql> GRANT CREATE,DELETE,INSERT,UPDATE ON mydb.* TO 'rahul'@'localhost'; mysql> FLUSH PRIVILEGES;
4. Revoke user permissions in MySQL
Use the REVOKE command to remove any specific permissions from the user. For example, remove DELETE permission from user 'rahul'@'localhost' on mydb database.
mysql> REVOKE DELETE ON mydb.* TO 'rahul'@'localhost'; mysql> FLUSH PRIVILEGES;
5. Delete users in MySQL
You can use the DROP command to delete any user from MySQL. For example, to delete user 'rahul'@'localhost', you can use the following command.
mysql> DROP USER 'rahul'@'localhost'; mysql> FLUSH PRIVILEGES;
The above is the detailed content of How to create users and grant permissions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

InnoDB is suitable for applications that require transaction support and high concurrency, while MyISAM is suitable for applications that require more reads and less writes. 1.InnoDB supports transaction and bank-level locks, suitable for e-commerce and banking systems. 2.MyISAM provides fast read and indexing, suitable for blogging and content management systems.

There are four main JOIN types in MySQL: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN returns all rows in the two tables that meet the JOIN conditions. 2.LEFTJOIN returns all rows in the left table, even if there are no matching rows in the right table. 3. RIGHTJOIN is contrary to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet JOIN conditions.

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools
