Question

How can I create a new user in mysql on my LAMP droplet (Ubuntu 20.04)

  • Posted on February 7, 2022
  • cnelsonAsked by cnelson

I followed this tutorial (step 6) to set up a new mysql user as I am trying to access the database (already created through phpmyadmin) with PHP. https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04

When I login to the console and try to create the new user with a string like this: CREATE USER ‘example_user’@‘%’ IDENTIFIED WITH mysql_native_password BY ‘password’;

I get an error saying RROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘BY ‘password’’ at line 1


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello, @cnelson

I’ll just want to add that we have an existing tutorial on How To Create a New User and Grant Permissions in MySQL which you can check here:

https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

KFSys’s answer is more than enough to help you through the process of creating an additional user, but you can also check our article as well.

Hope that this helps! Regards, Alex

Hi @cnelson,

To create a user, you can type in:

CREATE USER 'myuser'@localhost IDENTIFIED BY 'mypassword';

Where myuser is the name of your user and mypassword is your actual password.

To grant permissions to your user to a specific database, type in:

GRANT ALL privileges ON 'mydb'.* TO 'myuser'@localhost;

where mydb is is your actual Database name and myuser is your DB username.

Once done, type in

FLUSH PRIVILEGES;

So the changes take effect.

You can check if your user has the proper permissions by typing in:

SHOW GRANTS FOR 'myuser'@localhost;