How to create DB, user and password and give privileges in cpanel using uapi in terminal ?

  WHM

Introduction:

Managing databases and users on a web hosting server often requires using command-line tools like /usr/bin/uapi to automate tasks efficiently. This script demonstrates how to create a MySQL database, create a user, and grant them privileges using the command-line interface.

Prerequisite:

Before executing the script, ensure the following prerequisites are met:

  1. Access to Command Line: You should have access to the command line interface of your server or hosting environment where /usr/bin/uapi is installed and accessible.
  2. Credentials: Replace $CPUSER, $DATABASE, $USERNAME, and $PASSWORD with appropriate values:

$CPUSER: The cPanel username associated with the account.
$DATABASE: The name of the MySQL database you want to create.
$USERNAME: The username for the MySQL user you wish to create.
$PASSWORD: The password for the MySQL user.

  1. Permissions: Ensure your user account ($CPUSER) has the necessary permissions to create databases, users, and assign privileges within your hosting environment.

Explanation:

  1. Create Database: The first command creates a MySQL database named $DATABASE under the cPanel user account $CPUSER.
  2. Create User: The second command creates a MySQL user with the username $USERNAME and assigns the password $PASSWORD.
  3. Set Privileges: The third command grants all privileges (ALL PRIVILEGES) on the database $DATABASE to the user $USERNAME, allowing them full access to manage the database.

Example:

Run the following command in terminal
/usr/bin/uapi –user=”username” –output=json Mysql create_database name=”your DB_name”
/usr/bin/uapi –user=”username” –output=json Mysql create_user name=”Your DB User” password=”Your password”
/usr/bin/uapi –user=”username” Mysql set_privileges_on_database user=”Your DB User” database=”your DB_name” privileges=ALL PRIVILEGES

Conclusion:

Automating the creation of MySQL databases and users using command-line tools like /usr/bin/uapi streamlines administrative tasks on web hosting servers. By ensuring the prerequisites are met and executing the provided script with appropriate credentials, you can efficiently manage database operations without needing to navigate through graphical user interfaces, thus saving time and effort in server administration.

LEAVE A COMMENT