Skip to content

Instantly share code, notes, and snippets.

@deepyes02
Created September 28, 2021 10:02
Show Gist options
  • Save deepyes02/14f76c3d6e45cd75e656cf54b618f8f7 to your computer and use it in GitHub Desktop.
Save deepyes02/14f76c3d6e45cd75e656cf54b618f8f7 to your computer and use it in GitHub Desktop.
Create a new MySQL user and grant privileges.

Configure MYSQL with a new user in Ubuntu WSL

Requirements

I. wsl-2 II. ubuntu III. mysql fresh installation

  1. Login to mysql as root
# wsl
sudo service mysql start # start mysql service
sudo mysql -u root # login to mysql console as root (you don't need password on fresh installation)
  1. CREATE NEW USER IN THE SQL CONSOLE mysql>
--MYSQL
CREATE USER 'deepesh'@'localhost' IDENTIFIED BY 'pASScODE123xyz';

Note: Replace 'pASScODE123xyz' with your password.

  1. Grant Priviledges for the new USER mysql>
GRANT ALL PRIVILEGES ON * . * TO 'deepesh'@'localhost';

Important: Provides all admininstrative privileges to user 'deepesh' for all databases and tables. On a complex organizational structure, you can also edit these values for precise user access, eg, access to only certain databases for users.

  1. FLUSH THE PRIVILEGES mysql>
FLUSH PRIVILEGES;
  1. Exit and Restart mysql>
exit

bash

sudo service msyql restart
  1. Log in with the new id bash
# Replace deepesh with the new user
sudo mysql -u deepesh -p
  1. Now you should be able to manage your database with the new users

  2. Some useful mysql commands
    mysql>

-- List all users
SELECT user FROM mysql.user;

-- Delete a user
DELETE FROM mysql.user WHERE user="deepesh";

-- Don't forget to flush the database privileges when adding/editing/removing user

FLUSH PRIVILEGES;

-- Output: Query OK, 0 rows affected (0.01 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment