Child pages
  • UAPI Functions - Mysql::rename_database
Skip to end of metadata
Go to start of metadata

Description

This function renames a MySQL® database.

Warning:

It is potentially dangerous to rename a MySQL database. We strongly recommend that you perform a backup of the database before you attempt to rename it. 

MySQL does not allow you to rename a database. When cPanel & WHM "renames" a database, the system performs the following steps:
  1. The system creates a new database.
  2. The system moves data from the old database to the new database.
  3. The system recreates grants and stored code in the new database.
  4. The system deletes the old database and its grants.

Warning:

  • If any of the first three steps fail, the system returns an error and attempts to restore the database's original state. If the restoration process fails, the API function's error response describes these additional failures.
  • In rare cases, the system creates the second database successfully, but fails to delete the old database or grants. The system treats the rename action as a success; however, the API function returns warnings that describe the failure to delete the old database or grants.

Important:

In cPanel & WHM version 76 and later, when you disable the MySQL role, the system disables this function. For more information, read our How to Use Server Profiles documentation.

Examples 


 cPanel or Webmail Session URL
https://hostname.example.com:2083/cpsess##########/execute/Mysql/rename_database?oldname=mydb&newname=newlyrenamed


Note:

This example calls the UAPI function via a cPanel session. For more information, read our Guide to UAPI documentation. 

 LiveAPI PHP Class
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
 
// Rename the mydb database.
$rename_db = $cpanel->uapi(
    'Mysql', 'rename_database',
    array(
        'oldname'       => 'mydb',
        'newname'       => 'newlyrenamed',
    )
);


Note:

For more information, read our Guide to the LiveAPI System.

 LiveAPI Perl Module
my $cpliveapi = Cpanel::LiveAPI->new(); # Connect to cPanel - only do this once.
 
# Rename the mydb database.
my $rename_db = $cpliveapi->uapi(
    'Mysql', 'rename_database',
    {
        'oldname'       => 'mydb',
        'newname'       => 'newlyrenamed',
    }
);


Note:

For more information, read our Guide to the LiveAPI System.

 cPanel Template Toolkit
<!-- Rename the database. -->
[% execute('Mysql', 'rename_database', { oldname => 'mydb', newname => 'newlyrenamed' } ) %]


Note:

For more information, read our Guide to Template Toolkit documentation. 

 Command Line
uapi --user=username Mysql rename_database oldname=mydb newname=newlyrenamed


Notes:

  • You must URI-encode values.
  • username represents your account-level username.
  • For more information and additional output options, read our Guide to UAPI documentation or run the uapi --help command. 
  • If you run CloudLinux™, you must use the full path of the uapi command:

    /usr/local/cpanel/bin/uapi


 Output (JSON)
{
  "messages": null,
  "errors": null,
  "status": 1,
  "metadata": {
     
  },
  "data": null
}


Note:

Use cPanel's API Shell interface (cPanel >> Home >> Advanced >> API Shell) to directly test cPanel API calls.

Parameters

ParameterTypeDescriptionPossible valuesExample
oldnamestring

Required

The database's current name.

A database on the cPanel account.

mydb
newnamestring

Required

The database's new name.

A valid database name.

Important:

  • If database prefixing is enabled, you must prefix this value with the account prefix and an underscore (_). For example, for the dbuser database on the user cPanel account, pass in a value of user_dbuser.
  • The maximum length of the database name is 64 characters. However, due to the method that cPanel & WHM uses to store MySQL database names, each underscore character requires two characters of that limit. Therefore, if you enable database prefixing, the maximum length of the database name is 63 characters, which includes both the database prefix and the underscore character. Each additional underscore requires another two characters of that limit.
newlyrenamed

Returns

This function only returns metadata.