Create MySQL Database and User
To create a MySQL database, user, and grant full access, follow these steps in the MySQL command line or a script:
1. Create the Database and User
First, create the target database (if it doesn't exist) and define the new user with a password. It is best practice to restrict the user to localhost for local applications, or specify a specific IP for remote access.
-- Create the database
CREATE DATABASE IF NOT EXISTS my_database;
-- Create the user (replace 'password' with a strong password)
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
2. Grant Full Access
Grant all privileges on the specific database to the user. This allows the user to perform any action (SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, etc.) within that database.
-- Grant all privileges on the specific database
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
-- Reload privileges to apply changes immediately
FLUSH PRIVILEGES;
3. Verify Access
You can verify the user's permissions using:
SHOW GRANTS FOR 'my_user'@'localhost';
Note: If you need the user to connect from any host (e.g., for remote testing), replace 'localhost' with '%' in the CREATE USER and GRANT statements, but be aware this poses a security risk in production environments.