How to Set Up User Accounts And Permissions In MySQL?

9 minutes read

To set up user accounts and permissions in MySQL, you can follow these steps:

  1. Connect to MySQL: Open the MySQL command-line client or any other MySQL client tool to connect to the MySQL server.
  2. Create a new user account: To create a new user account, use the CREATE USER statement followed by the username and host from which the user will connect. For example, to create a user account named 'john' with the host 'localhost', you can use the following command: CREATE USER 'john'@'localhost';
  3. Set a password for the user: To set a password for the user account, use the SET PASSWORD statement. You can set the password using the PASSWORD() or the old_password() function. For example: SET PASSWORD FOR 'john'@'localhost' = PASSWORD('password');
  4. Grant privileges to the user: Grant specific privileges to the user account using the GRANT statement. The privileges include actions like SELECT, INSERT, UPDATE, DELETE, etc. You can grant privileges at the global level, database level, table level, or even at column level. For example, to grant all privileges on a specific database to 'john'@'localhost', use: GRANT ALL PRIVILEGES ON database_name.* TO 'john'@'localhost';
  5. Reload privileges: After granting or revoking privileges, you need to reload the privileges for the changes to take effect. Use the FLUSH PRIVILEGES statement to do so. For example: FLUSH PRIVILEGES;
  6. Revoke privileges: If needed, you can also revoke previously granted privileges using the REVOKE statement. For example, to revoke the SELECT privilege on a specific table from the 'john'@'localhost' user, use: REVOKE SELECT ON database_name.table_name FROM 'john'@'localhost';
  7. Delete a user account: To delete a user account, use the DROP USER statement followed by the username and host. For example: DROP USER 'john'@'localhost';


Remember to replace 'john', 'localhost', 'password', 'database_name', and 'table_name' with your desired values while executing the commands.


By following these steps, you can easily set up user accounts and permissions in MySQL to control access to your databases.

Best MySQL Books to Read in 2024

1
Murach's MySQL (3rd Edition)

Rating is 5 out of 5

Murach's MySQL (3rd Edition)

2
Learning MySQL: Get a Handle on Your Data

Rating is 4.9 out of 5

Learning MySQL: Get a Handle on Your Data

3
MySQL Crash Course: A Hands-on Introduction to Database Development

Rating is 4.8 out of 5

MySQL Crash Course: A Hands-on Introduction to Database Development

4
PHP & MySQL: Server-side Web Development

Rating is 4.7 out of 5

PHP & MySQL: Server-side Web Development

5
MySQL Cookbook: Solutions for Database Developers and Administrators

Rating is 4.6 out of 5

MySQL Cookbook: Solutions for Database Developers and Administrators

6
Murach's PHP and MySQL (4th Edition)

Rating is 4.5 out of 5

Murach's PHP and MySQL (4th Edition)

7
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Rating is 4.4 out of 5

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

8
MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration

Rating is 4.3 out of 5

MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration


How to delete a user account in MySQL?

To delete a user account in MySQL, you can use the following steps:

  1. Connect to the MySQL server using a MySQL client such as MySQL Command Line Client or MySQL Workbench.
  2. Switch to the MySQL database by running the command: USE mysql;
  3. Use the DROP USER statement to delete the user account. The syntax for this statement is as follows:
1
DROP USER 'username'@'host';


Replace 'username' with the name of the user account you want to delete, and 'host' with the host from which the user is allowed to connect. If the user can connect from any host, use '%' as the host.


For example, to delete a user account named myuser which can connect from any host, you would run the following command:

1
DROP USER 'myuser'@'%';


  1. Once you execute the DROP USER statement, the user account will be deleted.


Note: To delete the user account, you must have the necessary privileges. Users with the DROP USER privilege or users with the DELETE privilege on the mysql database can delete user accounts.


How to start the MySQL server?

To start the MySQL server, follow these steps:

  1. Open the command prompt or terminal. You can do this by pressing Windows+R and typing "cmd" for Windows, or by searching for "Terminal" in Applications for Mac.
  2. Navigate to the directory where MySQL is installed. The default installation location for MySQL on Windows is usually "C:\Program Files\MySQL\MySQL Server X.X\bin", where X.X represents the version number. On Mac, the default location is usually "/usr/local/mysql/bin".
  3. Run the command to start the mysql-server-to-grafana" class="auto-link" target="_blank">MySQL server. The command to start MySQL varies depending on the operating system. For Windows, run the following command: mysql.server start For Mac, run the following command: sudo ./mysqld_safe Note: On Mac, you might need to provide administrator privileges by entering your password.
  4. If successful, you should see a message indicating that the MySQL server has started. You can now connect to the server and begin using MySQL.


Note: If you have customized your MySQL installation, the paths and commands might vary. Adjust the commands accordingly based on your installation and configuration.


How to change the password for a MySQL user?

To change the password for a MySQL user, you can follow these steps:

  1. Open the terminal or command prompt.
  2. Log in to the MySQL server as a root user using the following command: mysql -u root -p Enter the password when prompted.
  3. Once you are logged in, switch to the MySQL database: use mysql;
  4. To change the password for a specific user, use the UPDATE statement in the following format: UPDATE user SET Password=PASSWORD('new_password') WHERE User='username'; Replace new_password with the new desired password and username with the specific user's name.
  5. After updating the password, you need to flush the privileges to apply the changes: FLUSH PRIVILEGES;
  6. To exit from MySQL prompt, type: exit;


Now the password for the MySQL user should be changed. Make sure to update the applications or scripts using this user with the new password to avoid any login authentication issues.

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

To configure MySQL for remote access, follow these steps:Open the MySQL configuration file. The location of the file may vary depending on your operating system. Usually, it is located at "/etc/mysql/my.cnf" or "/etc/my.cnf" on Linux, or "C...
Resetting the MySQL root password can be done using the following steps:Stop the MySQL server. This can be done through the command line by typing: For Linux: sudo service mysql stop For Windows: Use the Services window to stop the MySQL service. Start the MyS...
To create a new database in MySQL, you can follow these steps:Launch the MySQL command-line client by opening a terminal or command prompt window. Enter your MySQL username and password to connect to the MySQL server. The command usually looks like this: mysql...