Create Databases and Users using Command Line on WHM Server

Category : cPanel/WHM

Don’t like web interface and looking for commands to create MySQL Databases and Users using the command line on WHM Server? Want to automate database and users creation using scripts on your WHM Server? This guide will show how we can use terminal to create MySQL Databases and Users on the cPanel/WHM Server. Commands are very simple and neat.

Let’s proceed further with the guide.

Requirements

  • Root access to the WHM Server.

Procedure

Let’s say the cPanel account username is ‘basezap’, and you want to create a database named ‘basezap_website’ under this cPanel account.

  1. Log in to the root of your Server
  2. Run the following command to create the database named ‘basezap_website’ under ‘basezap’ cPanel account.
    uapi --user=basezap Mysql create_database name=basezap_website
    Note – Database name should contain cPanel Username followed by an underscore or else uapi command won’t accept and give you an error for same.
  3. Run the following command to create the MySQL user named ‘basezap_dbuser’.
    uapi --user=basezap Mysql create_user name=basezap_dbuser password=helloworld
    Simply replace ‘basezap_dbuser’ and ‘helloworld’ in the above command with the database username and password of your preference.
    Note – Database user should contain cPanel Username followed by an underscore or else uapi command won’t accept and give you an error for same.
    Finally, add the user to the database with this command.
    uapi --user=basezap Mysql set_privileges_on_database user=basezap_dbuser database=basezap_website privileges=ALL%20PRIVILEGES
    Replace “ALL%20PRIVILEGES” with the specific privileges that match your preference, as documented at:
    https://documentation.cpanel.net/display/SDK/UAPI+Functions+-+Mysql%3A%3Aset_privileges_on_database

With this, we have finished creating Database, Database user and Adding privileges to the Database user.