Databases Questions
Database support articles: if your website uses a database to serve content, this is where you can find further information and answers.
Does 'Back Up Multiple Sites' back up databases?
Yes.
You can back up multiple sites from the Back Up Web Hosting icon under the Reseller Manager section within My20i.
Here you can select multiple packages to create a backup of all at once.
The backup created contains both site files and database information.
How do I import a .sql file via the command-line?
If you've set up SSH access for a package then you can use one of two methods to import a MySQL database via the CLI.
If needed, please see our guide on how to connect to your package via SSH.
MySQL CLI
To import a SQL file via the command line, you can use the mysql command. Here’s how.
mysql -h hostname -u username -p database_name < file.sql
Replace hostname with your database server name.
Replace username with your database username.
Replace database_name with the name of the database where you want to import the SQL file.
Replace file.sql with the path to the SQL file you want to import.
After entering the command, you'll be prompted to enter the password for the specified username.
Enter the correct password, and the import process will begin.
Make sure the SQL file contains valid SQL commands and is in the correct format for the database you're using.
If the import is successful, the data from the file will be imported into the specified database.
Compressed Files
If the database file is zipped then you’ll need to unzip it first.
For a .zip file you can use the unzip command:
unzip databasefile.sql.zip
For a .gz file you can use the gunzip command:
gunzip databasefile.sql.gz
From here you can proceed to import the unzipped file as normal.
If the database file is zipped then you’ll need to unzip it first.
WP CLI
To import a SQL file using the WP-CLI tool, you can follow these steps. WP-CLI is a command-line tool for managing WordPress installations, and it includes a command for importing databases.
1. Navigate to your WordPress installation:
SSH into your package and navigate to the root directory of your WordPress installation.
2. Backup your current database (optional but recommended):
It's a good practice to create a backup of your existing database before performing an import. You can use the following WP-CLI command to export your database:
wp db export
This command will create a SQL dump of your database in the WordPress installation folder. You can use it as a backup in case anything goes wrong.
3. Import the SQL file:
Use the wp db import command to import the SQL file. Here's the command:
wp db import file.sql
4. Verify the import:
After the import is complete, you can verify by checking your WordPress site to ensure that the data from the SQL file has been imported correctly.
That's it! You've successfully imported an SQL file into your WordPress database using WP-CLI.
This method is especially useful for migrating WordPress sites or importing data into a new WordPress installation.
How do I check MySQL Database sizes across packages in My20i?
20i provides an easy way to monitor and view all MySQL database sizes across all packages in your My20i account.
To access the MySQL Database Size Reports:
- Login to My20i.
- Head to the Reporting section on the home page and select MySQL Database Sizes.
You’ll see a full list of all the databases under your 20i account and their sizes. You’re also shown which domain they are associated-with so that you can manage the MySQL database, should you need-to.
How do I import a .sql file via phpMyAdmin?
You can use phpMyAdmin to import an .SQL file with your database information. To do this:
- Log in to your database via phpMyAdmin (My20i > Manage Hosting > [select package] > phpMyAdmin)
- Choose the database you want to import your .SQL file to from the left hand side.
- Click the Import tab at the top of the page.
- Select Choose File and locate the .SQL file on your device or computer.
- Click Go
The .SQL file will be uploaded and imported to the database you've selected.
For further help there's a video showing how to import and export databases via phpMyAdmin:
20i's shared web hosting offers 1 GB MySQL databases.
What is MariaDB?
MariaDB is a fork of MySQL.
The database structure and indexes of MariaDB are the same as MySQL. This makes MariaDB a backward compatible, drop-in replacement for MySQL.
We currently run a combination MariaDB version 10.6
and 10.11
To check the version of MariaDB:
- Head to Manage Hosting, then phpMyAdmin.
- Select the database in the left-hand panel.
- Select the Query tab at the top.
- Type the query: SELECT VERSION(); and select Go.
- The version of MariaDB will then be outputted.
How do I restore a site using Timeline Backups?
You can easily restore lost data using the Timeline Backups tool which you can order from within your 20i account. Timeline Backups takes a backup of all your websites and databases every day automatically, and stores them for 30 days.
You can restore from any available snapshot at any time.
Site Files
- Head to the package you want to retrieve data for.
- Select Timeline Backups.
- You should see two types of backup: Web and Database. To restore the site files select View Snapshots next to Web.
- Select a Snapshot Time, the package you want to restore the data to, and (optionally) a path.
- Leaving the path field empty will replace the current files with that of the restore.
Databases
- Head to the package you want to retrieve data for.
- Select Timeline Backups.
- You should see two types of backup: Web and Database. To restore the site files select View Snapshots Databases.
- Select a Snapshot Time, the target package and the database you want to overwrite.
- Optionally, you can select to Restore to file which will dump a .SQL file in the webspace which you can manually import.
Restoring a Deleted Hosting Package
- Head to Manage Hosting and recreate the hosting package that was deleted.
- Select Timeline Backups.
- You should see that the Web & Database has (deleted) next to them. These are the backups from when the hosting package was deleted. To restore this, select View Snapshots.
- Select a Snapshot Time, the target package and the site files & database you want to restore into.
5. Once restored, the configuration file of the website will need to be updated with the new database details and credentials. For WordPress, this will be the wp-config.php.
How to fix the "Error Establishing Database Connection" in WordPress
The Error Establishing Database Connection error is one of the most commonly experienced errors on WordPress sites. The error itself indicates that the connection between your website files and the database has dropped.
As the database is queried for most of your WordPress site’s content, this drop of connection stops any of the site content from displaying until the connection is re-established. So it’s important to address this error as soon as possible.
There are a number of reasons that the connection to the database may drop – the details that are being used to attempt the connection may be incorrect, or the database itself may not be loading correctly or may have been corrupted. This guide will break down the core areas to check to re-establish the connection as soon as possible and get your site loading.
Note: Take a backup of your website database before attempting any of the following solutions. You can do so using our Timeline Backups service.
1. Checking wp-config.php
In a WordPress site, the database connection details are held within a file called wp-config.php, generally located in the root folder of your WordPress site. If you navigate to this file using our File Manager or via FTP, you’ll see code such as the following:
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** Database username */
define( 'DB_USER', 'username_here' );
/** Database password */
define( 'DB_PASSWORD', 'password_here' );
/** Database hostname */
define( 'DB_HOST', 'hostname_here' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
Each section is labelled via comments that show what each area is used for.
The main details you’ll need to check here are the Database name, Database hostname, Database username and Database password. If any of these details are wrong, the connection to the database won’t be possible and you’ll need to change the details accordingly. But where do we find these database details?
Your site’s database details can be accessed and updated via your hosting package:
- Log into My20i and head to your Manage Hosting area
- Select Options > Manage on the hosting package you’d wish to edit
- Head to the Web Tools section and select MySQL Databases
- Under Manage MySQL Databases you’ll see your databases and their current Server, Database/Username and an area where you can update the password
Going back to the wp-config.php file, you can use the above details to fill out and update your connection details:
- The Server is your Database hostname
- Database/Username is the Database name and Database username
- The Database password is the password you’ve added to the Change Password area.
Once these have been updated, recheck your site and the database connection error should no longer display.
2. Checking the database connection
You can also use the above details to test whether your database connection is working. This can be useful to find out if the problem is with the details being used or if there’s something wrong with the database itself.
To do this, head to your site’s File Manager or use FTP to create a new file in the root folder of your site. You can name this whatever you’d like, but make sure that the file is saved as a .php file such as test.php.
Once done, add the following code to the file:
$host = 'hostname';
$user = 'username';
$pass = 'pass';
$test = mysqli_connect($host, $user, $pass);
if (!$test) {
die('MySQL Error: ' . mysqli_error());
}
echo 'Database connection is working properly!';
mysqli_close($test);
Replacing ‘hostname’, ‘username’ and ‘pass’ with the database hostname, database username and database password respectively.
Once that’s set up and saved, load the script via your site. If, for example, you named your file test.php you can load this by visiting your site and appending /test.php to the end of the URL i.e., domain.com/test.php.
If the script loads and displays an error, there’s a problem with the details being used. If it instead displays the line Database connection is working correctly! then the credentials being used for the database are correct.
If that’s the case, and your site is still showing a database error, then the problem may be with the database itself. It will need to be repaired.
3. Repairing a corrupt database
The issue can sometimes be that your database itself has become corrupt. This isn’t a common occurrence. However, it can happen with the amount of data, rows and tables that can be created and removed by various third-party plugins and functions.
One way to test if this is the case is to try and connect via wp-admin. If you receive the following error:
It means that the database has been corrupted. Fortunately, there are tools that can be used to repair this, using both WordPress and PHPMyAdmin.
4. Using WordPress’ database repair tool
To access the tool WordPress provides to fix this, you’ll need to edit your wp-config.php file and add the following line of code before the line: ‘/* That’s all, stop editing! Happy publishing. */’.
Define(‘WP_ALLOW_REPAIR’, true);
Once this code has been added, head to the following URL:
https://domain.com/wp-admin/maint/repair.php
Replacing ‘domain.com’ with your domain. This will open up a repair wizard built into WordPress that will attempt to repair your database. Follow the steps in the wizard and, when it’s finished repairing, your site should come back online.
Once you’ve finished with the repair tool, remove the above line of code from your wp-config.php file.
WordPress CLI also provides a quick tool to repair the database. To access this, connect to your site via SSH and navigate to the root folder of your WordPress installation. You can then run the command:
wp db repair
This will start a full repair of the WordPress database.
5. Repairing a database with phpMyAdmin
You can also repair the database using phpMyAdmin.
To do so:
- Log into My20i and head to your Manage Hosting area
- Select Options > Manage on the hosting package you’d wish to edit
- Head to the Web Tools section and select phpMyAdmin
- Select Sign-In on the database you need to repair
- You’ll be signed into phpMyAdmin. Select your database on the left, and you’ll see a selection of your database’s current tables.
- Select all of your tables with the checkbox next to each
- From the drop-down at the bottom of the screen, select Repair table
This will run the SQL REPAIR TABLE command on all of your selected tables, which will attempt to repair and remove any corruption or broken data from these tables.
6. Repairing corrupted WordPress files
In some rare instances, the issue may lie with the files of the site rather than the database itself. This can happen when the connection during an FTP upload is interrupted, or malware infects the WordPress site and modifies the core files. If this is the case, you’ll need to replace the core files of the site with a fresh copy directly from WordPress.
In order to do this, first head to download WordPress’ latest version – you can find this here:
https://wordpress.org/download/
Once you have a copy of these files, unzip them on your local device and remove the wp-content and wp-config.php files. Then, connect to your site via FTP and upload the folders and files to your site’s root folder, overwriting your current core files with the fresh upload.
Note: provided that you remove the wp-content files, doing this won’t affect the content of your website – text, images etc.
Once done, your site should be loading with the correct files necessary.
Alternatively, you can use the following WP CLI command after connecting via SSH to achieve this as well:
wp core download --force
There are a number of potential causes and fixes for ‘Error Establishing a Database Connection’ errors within WordPress. By making use of the above steps, you should be able to pinpoint and fix this error, and bring your site back online as soon as possible. Always be sure to take a backup before attempting any form of manual change to your site files and database.
Building a MySQL instance on CentOS 7
You might need a larger database - for large ecommerce stores or complex sites, for example. To do this, you can configure a CentOS 7 VPS as a MySQL instance on an Unmanaged virtual private server.
This guide will show you how to build a MySQL instance using a 20i VPS.
Step 1 - Install MariaDB
You'll first need to install MariaDB - here's a guide: https://www.tecmint.com/install-mariadb-in-centos-7/
Once you've installed MariaDB you can test the installation by running the following:
[root@vps-b92a95 ~]# mysql -V
Which should give you:
mysql Ver 15.1 Distrib 10.1.44-MariaDB, for Linux (x86_64) using readline 5.1
Step 2 - Access MySQL
You can access MySQL with:
[root@vps-b92a95 /]# mysql -u root -p
You will then be prompted for the password, set when following the guide above. It might be easier to use the VPS password for convenience at this stage.
Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 10.1.44-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
Step 3 - Create the database
So now that MariaDB is installed, you will want to create the databases and database users within MariaDB.
To create a database:
[root@vps-b92a95 ~]# create database DATABASE_NAME;
If the site is to be hosted on the shared platform and the database is to be hosted on the VPS, you should create a user with remote access privileges.
So in this format:
[root@vps-b92a95 ~]# create user 'DATABASE_USER'@'%' identified by 'PASSWORD';
For example:
[root@vps-b92a95 ~]# create user 'wordpress_user'@'%' identified by 'password123!';
Now that the user and databases are created, you will want to grant all privileges.
[root@vps-b92a95 ~]# grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'%';
For example:
[root@vps-b92a95 ~]# grant all privileges on wordpress_testing.* TO 'wordpress_user'@'%';
Then let the privileges take effect:
[root@vps-b92a95 ~]# flush privileges;
You can check to see if the users and databases are accessible:
[root@vps-b92a95 /]# mysql -u wordpress_user -p wordpress_testing Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 18 Server version: 10.1.44-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [wordpress_testing]>
Step 4 - Upload the .SQL file
Once you're happy with the database and user access, you can now look at uploading the database file to the server. You can use WinSCP if you're a Windows user. When the file is uploaded, you'll need to import the file:
[root@vps-b92a95 ~]# mysql -u DATABASE_USERNAME -p DATABASE < importedfile.sql
For example:
[root@vps-b92a95 ~]# mysql -u wordpress_user -p wordpress_testing < wordpress-3132333666.sql
Now that the database is imported you should open the firewall for the incoming MariaDB connections:
[root@vps-b92a95 ~]# firewall-cmd --permanent --add-service=mysql && firewall-cmd --reload
Conclusion
So that's the server all set up and ready. You would need to update the configuration file to connect to the database on the VPS, and you should be good to go.
What's the maximum MySQL database size allowed?
At 20i, each MySQL database you create can store up to 1024 MB (1 GB) of data. As databases comprise of only text, this is a lot of space in real terms; to put it into perspective, a 20i customer’s average database size is just 10 MB.
In the rare event that you need more than 1024 MB, you'll need to add an additional database. You aren't able to add more MySQL databases to websites hosted on our WordPress hosting platform; you will need to complete a Platform Transfer first.
If you have a Home, Business, or Reseller Hosting package with us, there’s no limit to the number of additional databases you can add. If you have a Starter Hosting package, you are limited to one MySQL database but you can upgrade your hosting package through My20i, or purchase an additional hosting plan.
For a step-by-step guide on creating new MySQL databases, please see the following article: How do I create a MySQL database in My20i.
While 1024 MB is enough storage for the majority of databases you may occasionally need a larger one. If you do need to host a database larger than this that can be done using a Managed Hosting on a VPS.
How do I create a MySQL database in My20i?
You can create MySQL databases in seconds directly from your My20i control panel.
This option is unavailable for packages hosted on our WordPress platform due to its WP-optimised nature. If you need to create a MySQL database, you'll need to carry out a Platform Transfer to move the website to our Linux Web Hosting platform.
Starter Hosting packages are limited to one MySQL database. For more databases, you can upgrade your hosting package through My20i, or purchase an additional hosting plan.
To create new MySQL databases:
1. Log in to My20i and select Manage Hosting.
2. Choose Manage next to the hosting package you wish to create a MySQL database for.
3. Under Web Tools, select MySQL Databases. Here you can complete the following actions:
- Create new MySQL databases. Each MySQL database has a maximum size of 1024MB (1GB).
- Manage existing databases.
- Add additional MySQL users to databases.
- Manage your additional MySQL users.
4. Under Add a new MySQL Database, choose a name for your database. Database names must be between 6 and 20 characters long, and we automatically add a suffix to avoid name collisions with other users.
5. Once you have chosen a name, click Create Database. Your new MySQL database will automatically appear in the Manage MySQL Databases section underneath.
From here you can change the automatically generated password, monitor usage, remove your MySQL database, or log in to your database.
How do I manage my Microsoft SQL database?
We recommend using SQL Server Management Studio (SSMS) for managing your Microsoft SQL databases.
This industry-standard software provides a comprehensive environment for Microsoft SQL database creation, access, configuration, development, and other management features. It includes a variety of components including visual database tools, code and text editors, object management, boilerplate text management, and more. With SSMS you can deploy, monitor, and upgrade the data-tier components used by your applications, as well as build queries and scripts.
SSMS also helps you download and back up your MS SQL databases, which we recommend you do at regular intervals in the interests of best practice security.
You can download the software for free on the Microsoft SSMS download page.
To connect remotely you will need to use Server 'mssql.stackcp.com'.
How do I download or back up my MySQL Database?
There are various ways to export your MySQL databases for safe keeping or transfer. The first method allows you to back up multiple databases via your My20i control panel and the second on a 'per database' basis via phpMyAdmin.
Backup/Restore
The control panel provides an easy way to download all databases associated with your hosting package. To access this page:
- Access the Backup/Restore page: My20i > Manage Hosting > [select package] > Backup/Restore
- Ensure Databases is checked under Backup Type
- Click Create Backup
A backup task will then be queued by our system. Once finished, a download link will be provided; this usually takes 1-2 minutes, dependent on the size of the database. You can then download a zip file containing all your MySQL databases.
As a 20i Reseller you'll also have the ability to back up your website files and databases in bulk. Choose 'Back Up Web Hosting' from the My20i homepage and select the packages you wish to back up.
phpMyAdmin
Here's a video covering the movement of .SQL files using the phpMyAdmin tool included in My20i:
The phpMyAdmin management software lets you import and export your databases.
- Login to your database via phpMyAdmin (My20i > Manage Hosting > [select package] > phpMyAdmin)
- Choose the database you want to back up down the left hand side
- Click the Export tab at the top of your page
- Leave Quick selected under Export Method
- Select your desired format - we recommend "SQL" for most cases
- Click Go
Within a few seconds a download should begin in your browser.