Use the same values for the database connection locally and remotely
If you use a connection to the MySQL database in PHP applications, the values used for the database connection on the local system with MAMP PRO and the remote server are not identical in most cases. As a result, you have to provide different sets of connection data for each of the different execution locations (local, remote). But this does not have to be the case. With the capabilities provided by MAMP PRO, this extra effort is not necessary.
The plan is therefore to use the same database connection values for the local server environment and the remote server at the provider. We have to keep in mind that certain values are predefined by the provider and cannot be easily adjusted. For this reason, we will proceed in the following in such a way that we start from the data provided by the provider and use this as a basis for the values on the local side.
- Provider database connection data:
- Hostname: db52339873412.hosting-data.io
- Database name: dbs8723017
- Username: dbu4919316
- Password: 3dft6hfdgjz9hd3skw
Let’s first create a new MySQL database using MAMP PRO with the name, username and password just mentioned. To do this, we proceed as follows:
- Open MAMP PRO.
- Start the servers.
- In the Hosts list of MAMP PRO, select the host with which you want to use the database.
- Now switch to the “Databases” tab.
- Click on the plus button at the bottom of the list of databases. The “Create database” dialog will open.
- Enter the database name in the “Name” field. In our example this is the value “dbs8723017”.
- Below the input field for the name there is a pop-up element “After creating the new database”. If this is still closed, click on the text to open it.
- Activate the checkbox in front of the text “grant access to User” and enter the user name in the input field behind it. In our example this is the value “dbu4919316”.
- Enter the password in the input field labeled “with Password”. In our example this is the value “3dft6hfdgjz9hd3skw”.
- Now click on the button labeled “Create”. The newly created database will appear in the list of databases. The activated checkbox in front of the name indicates that this database is linked to the selected host.
By default, MAMP PRO provides the host “localhost” for a connection to the integrated MySQL database. As you can easily see, this is very different from the host name given to us by our provider (see above). In order to be able to use this name locally, we have to make sure that the MAMP PRO servers know this name and forward requests to it correctly. For this we proceed as follows:
- Now switch to the “General” tab.
- In the lower part of this view you will find the “Aliases” section. Click on the plus button at the bottom.
- In the newly created entry, enter the host name for the database specified by the provider. In our example this is the value “db52339873412.hosting-data.io”.
- Now click on the button labeled “Save” at the bottom of MAMP PRO. This will save the changes and restart the servers.
Done! You can now use the same database credentials on your local server environment and on your provider’s server.