6.4.2 Setting up a database connection

Banner A database is an organized data storage that allows you to efficiently manage and access information. It is an integral part of most modern web applications, providing secure and structured storage of data such as user profiles, orders, products, and much more.

Connecting a database to Your application may be needed for purposes such as:

  • Storing and managing user data (accounts, profiles, settings, etc.).
  • Storing and processing orders, purchases, and transactions.
  • Maintaining a catalog of products or services.
  • Storing and retrieving content such as articles, publications, comments.
  • Keeping logs of user activity or history of actions.
  • Providing data search and filtering functionality.

Connecting an Application to a Database

The first step is to create a new database according to the instructions, or alternatively, if the database already exists, use the current access parameters for it. During the creation of a new database or when obtaining access parameters for an existing one, You will be provided with the database name, username, and password. These credentials will be required for subsequent local or remote connection of the application to the database.

We inform: when working with access parameters, it is important to consider the case of Latin characters, for example, the database name example will not match the name Example. Database names must consist only of Latin letters and digits without spaces and special characters.

Setting up a Local Database

A local connection assumes that the web application and database are hosted on the same server. This ensures maximum data transfer speed. For settings:

1. Open the configuration file of Your site, usually called config.php or configuration.php. You can find out more about how to find the configuration file in this article.

2. Find the section containing the database connection parameters, such as server name, database name, username, password, and port.

3. For local connection, use the server name (host address) localhost.

4. Replace the existing parameter values with the corresponding data of Your local database.

Example: correctly configured configuration file

Go to the site configuration file

5. Make sure that the table prefix matches the one You plan to use in the local database. The table prefix is usually set during CMS installation. Tables can have different prefixes. For example:

  • CMS WordPress uses the default prefix – wp_
  • Opencartoc_
  • Joomlaa7n08_

6. Check for operability. If an error is observed, make sure that the parameters in step 2 were entered correctly.

Setting up a Remote Database

Setting up a remote connection allows you to perform various operations with data directly from your local application or specialized software installed on your personal computer. It may also be necessary in a situation where a web application is hosted on one server, and the database is located on another remote server. This option is typical for production-ready websites and services deployed on cloud infrastructure. For settings:

1. Activate remote access to the database.

Example: activating remote access
  1. Select the required database, go to the Database Users section.

Database User

  1. Activate remote access for the user, and additionally, You can restrict the list of IPs that can connect to the database.

Activating remote access

2. Open the configuration file of Your application, usually called config.php or configuration.php. Similarly, this could be the graphical interface of Your software.

2. Find the section containing the database connection parameters, such as server name, database name, username, password, and port.

  • For external connections, use the standard port for MySQL3306 or for PostgreSQL5432.
  • Specify Your hosting server as the host address (server name), for example s1.thehost.com.ua or s2-de.thehost.com.ua.
  • These data differ for each service. You can view the current parameters in the letter with access parameters.

5. Replace the existing values of these parameters with the corresponding data of Your remote database. Save the changes made, it may require restarting the application.

Example: remote connection

Connection example

Important: when working with databases remotely, it is necessary to use complex passwords, restrict the list of IP addresses that can connect, and set minimal permissions for users. It is also recommended to regularly update the DBMS software and monitor and audit access to the database.