Skip to main content

Lab 006: Install and Configure DB Server



Requirements

We recently migrated one of our WordPress websites from an old server to a new infrastructure in Stratos Datacenter. We have already setup LAMP, except for the database. We have also restored website code; however, we need to restore the database to make it work on the new infra. Please perform the below given steps on DB host:

a. Install/Configure MariaDB server.

b. Create a database with name kodekloud_db2.

c. There is a DB dump on jump_host under location /home/thor/db.sql. Restore this database in newly created database.

d. Create a user kodekloud_aim and set any password you like.

e. Grant full permissions to user kodekloud_aim on database kodekloud_db2.

f. Update database-related details in /data/wp-config.php file on storage server, which is our NFS server having a share /data mounted on each app server on location /var/www/html. (for more details about how to update WordPress config file please visit [https://wordpress.org/support/article/editing-wp-config-php/])

g. You can access the website on LBR link; to do so click on the + button on top of your terminal, select option Select port to view on Host 1, and after adding port 80 click on Display Port.

Note

This lab guides through MariaDB installation and WordPress database restoration. Ensure you have proper backups before starting.

Prerequisites

  • Access to the database server with root or sudo privileges
  • Database dump file available at /home/thor/db.sql on jump host
  • WordPress configuration file at /data/wp-config.php on storage server
  • SSH access to app servers for configuration updates

Steps

# 1. Install MariaDB server
sudo yum install -y mariadb-server mariadb-client

# 2. Start and enable MariaDB
sudo systemctl enable mariadb
sudo systemctl start mariadb

# 3. Create the database
mysql -u root << EOF
CREATE DATABASE kodekloud_db2;
EOF

# 4. Create database user with appropriate permissions
mysql -u root << EOF
CREATE USER 'kodekloud_aim'@'localhost' IDENTIFIED BY 'your_secure_password';
CREATE USER 'kodekloud_aim'@'%' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON kodekloud_db2.* TO 'kodekloud_aim'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON kodekloud_db2.* TO 'kodekloud_aim'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF

# 5. Restore the database from dump
mysql -u kodekloud_aim -p kodekloud_db2 < /tmp/db.sql

# 6. Update WordPress configuration on storage server
# (Run on storage server)
sudo sed -i 's/dbname/kodekloud_db2/g' /data/wp-config.php
sudo sed -i 's/dbuser/kodekloud_aim/g' /data/wp-config.php
sudo sed -i 's/dbpass/your_secure_password/g' /data/wp-config.php
sudo sed -i 's/dbhost/stdb01/g' /data/wp-config.php

# 7. Verify configuration
grep -E "DB_NAME|DB_USER|DB_PASSWORD|DB_HOST" /data/wp-config.php

Verification

Test the database connection and verify restoration:

mysql -u kodekloud_aim -p -h localhost kodekloud_db2
# Check database tables
SHOW TABLES;
\q

# Test from app server
mysql -u kodekloud_aim -p -h stdb01 kodekloud_db2
SHOW TABLES;
\q

Resources