9.9 KiB
| slug | title | authors | tags | ||||
|---|---|---|---|---|---|---|---|
| Third-blog-post | Third Blog Post |
|
|
import CodeBlock from '@site/src/components/CodeBloack';
Creating a New User and Setting Permissions in MySQL
This guide will show you how to make a new friend in MySQL (we call them a user) and give them special powers to do different things. Let's get started!
If you don't have a MySQL database yet and want to set one up, we have a guide on How To Install MySQL. It doesn't matter if your computer works with a different system – creating a new MySQL friend and giving them special powers will be pretty similar. Ready to go? Let's do it!
Note: As we go through this guide, you might notice some parts of the commands that need your attention. We'll point them out with a special highlight like this. It's kind of like adding your own emojis to a message – just keep an eye out for these notes, and you'll know when to put your own touch on things!
This step shows you how to use the root user to make a new friend (a user) and give them some powers.
In Ubuntu systems running MySQL 5.7 and later, the root user doesn't use a password by default – it's a bit like having your house key always ready. But to make changes, you'll need to use a special command called sudo. This makes sure you have the right permissions to do what you want with MySQL. Ready to make some new friends? Let's go!
<CodeBlock code={sudo mysql } />
<CodeBlock code={mysql -u root -p } />
<CodeBlock code={CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password'; } />
You can pick how your user logs in to MySQL. The auth_socket plugin is easy because users don't need a password to get in. But it can also make it tricky for programs outside your computer to talk to MySQL because it blocks remote connections.
Instead of specifying the authentication plugin, you can skip that part to let the user log in with MySQL's default security system, called caching_sha2_password. This is a good choice if you want to use a password for logging in because it's very secure. To create a user that uses caching_sha2_password, run this command. Just replace "sammy" with the username you want and pick a strong password that you'll remember.
<CodeBlock code={CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password'; } />
<CodeBlock code={CREATE USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; } />
<CodeBlock code={ALTER USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; } />
<CodeBlock code={GRANT PRIVILEGE ON database.table TO 'username'@'host'; } />
Here's an example command: it gives a user the ability to do various things like creating, altering and dropping databases and tables. They can also add, update and delete data from any table and perform other actions like querying data and creating foreign keys. But keep in mind, it's a good idea to only give users the specific abilities they actually need. So, feel free to adjust what your user can do based on what tasks they should be able to perform.
You can see the complete list of things users can do in the official MySQL documentation. To give these abilities to your user, just use the GRANT statement. Instead of "sammy," use the name of your own MySQL user. Easy, right?
<CodeBlock code={GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION; } />
Caution: Be careful with granting the ALL PRIVILEGES privilege to your MySQL user. It gives them extensive powers, almost like being the boss (root user). Here's how some users might do it:
<CodeBlock code={GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION; } />
Lots of guides recommend using the FLUSH PRIVILEGES command right after you make a NEW USER or give them special powers. This helps to make sure that the changes take effect right away.
<CodeBlock code={FLUSH PRIVILEGES; } />
If you ever need to take away a permission, the process is almost the same as giving it:
<CodeBlock code={REVOKE type_of_permission ON database_name.table_name FROM 'username'@'host'; } />
To check what a user can currently do, run the SHOW GRANTS command:
<CodeBlock code={SHOW GRANTS FOR 'username'@'host'; } />
<CodeBlock code={DROP USER 'username'@'localhost'; } />
<CodeBlock code={exit } />
<CodeBlock code={mysql -u sammy -p } />
Excited to manage your MySQL database?
Begin by making new users and deciding what they can do! Try out different settings to improve your database skills. Let's get started!