How to use Astra Website Protection with a MySQL database

how-to-use-astra-website-protection-with-a-mysql-database

Pradheep BS
Written by Pradheep BSLast update 4 months ago

This article will help you configure Astra Website Protection with a MySQL database, instead of the default SQLite database.

1. Step 1 - Select/Create MySQL database

You can either use an existing MySQL database, or create a new database (recommended) for use with Astra. You can refer to one of these guides to create the new database:

cPanel - HostGator, GoDaddy, Namecheap, Hostinger etc.
Plesk - Vultr, A2, Liquid Web etc.
Self-hosted MySQL

Once you've created a new database & user, we will need to set the following environment variables:

Database Driver

ASTRA_DB_CONNECTION

mysql

Database Hostname

ASTRA_DB_HOST

localhost is the default hostname used by cPanel

Database Port

ASTRA_DB_PORT

3306 is the default MySQL post

Database Name

ASTRA_DB_DATABASE

astra_security is the recommended for easy identification

Table Prefix

ASTRA_DB_PREFIX

astra_gk_ is the default table prefix

Database Username

ASTRA_DB_USERNAME

Enter the value generated

Database Password

ASTRA_DB_PASSWORD

Enter the value generated

Step 2 - Update the environment variables


We need to set the database connection details in the environment variables, so that the Astra Website Protection plugin can use them. There are multiple ways to set the environment variables on your server as shown below.

Method 1 - Add code to your website config file (recommended)


This is the easiest way to set environment variables. Open the config file of your CMS such as wp-config.php or config.php and add the following PHP code to it (after setting the correct database credentials):

//Begin Astra Security

putenv('ASTRA_DB_CONNECTION=mysql');
putenv('ASTRA_DB_PORT=3306');
putenv('ASTRA_DB_PREFIX=astra_gk_');
putenv('ASTRA_DB_DATABASE=astra_security');
putenv('ASTRA_DB_HOST=localhost');
putenv('ASTRA_DB_USERNAME=astra_security');
putenv('ASTRA_DB_PASSWORD=<database_password>');

//END Astra Security

Some of the common paths of the config file:

WordPress

wp-config.php

OpenCart

config.php

Method 2 - Update your existing .env file


If you're using a framework such as Laravel, Symfony etc. and already use an environment file, you can simply add these variables to it. Add the following code to the .env file after updating the values:

ASTRA_DB_CONNECTION=mysql
ASTRA_DB_PORT=3306
ASTRA_DB_PREFIX=astra_gk_
ASTRA_DB_DATABASE=astra_security
ASTRA_DB_HOST=localhost
ASTRA_DB_USERNAME=astra_security
ASTRA_DB_PASSWORD=<database_password>

Method 3 - Set environment variables via server config


If you are using Apache, you can add the following code to your .htaccess file set the environment variables. Before making any changes in your live server, please make a copy of the .htaccess file.

SetEnv ASTRA_DB_CONNECTION mysql
SetEnv ASTRA_DB_PORT 3306
SetEnv ASTRA_DB_PREFIX astra_gk_
SetEnv ASTRA_DB_DATABASE astra_security
SetEnv ASTRA_DB_HOST localhost
SetEnv ASTRA_DB_USERNAME astra_security
SetEnv ASTRA_DB_PASSWORD <database_password>

Step 3 - Verify Changes

Once the above changes are made, you can click on Verify Installation in the Getting Started screen, or simply run a new Malware Scan to verify that these changes have taken effect. If you face any issues with this process, please feel free to reach out to us by creating a support ticket from the Astra Dashboard.

Did this answer your question?