All Collections
Linux / SSH / Bash
Mass find WordPress installs, create database, assign database user and password via wp-config.php
Mass find WordPress installs, create database, assign database user and password via wp-config.php

This one liner will find ALL WordPress installs and create the database, assign the user / password to that database automatically.

Justin Catello avatar
Written by Justin Catello
Updated over a week ago

A lot of times when we migrate a user from a cPanel based server over to a LEMP stack, it can be very time consuming to create all new databases along with assigning a database user / password to access that database.  This one liner will do all the work for you.

Simply run this in the parent directory of the WordPress installs, it will search for all wp-config.php scripts, pull the database, user and password, create the database, and lastly assign the user / password to the database.

*It does require mysqladmin which most will have available by default.

for wpinstall in $(find "$(pwd)"/ -type f -name wp-config.php | sed 's/wp-config.php//g') 
   do
echo "$wpinstall"

dbname=$(grep DB_NAME "$wpinstall"/wp-config.php | grep -v WP_CACHE_KEY_SALT | cut -d \' -f 4)
echo "Database Name: $dbname"

dbuser=$(grep DB_USER "$wpinstall"/wp-config.php | grep -v WP_CACHE_KEY_SALT | cut -d \' -f 4)
echo "Database User: $dbuser"

dbpass=$(grep DB_PASSWORD "$wpinstall"/wp-config.php | grep -v WP_CACHE_KEY_SALT | cut -d \' -f 4)
echo "Database Password: $dbpass"

echo "Creating database: $dbname"
mysql -e "CREATE DATABASE $dbname;"

echo "Assigning Database User: $dbuser to Database: $dbname using Password: $dbpass"
/usr/bin/mysql -e "grant all privileges on $dbname.* to $dbuser@'localhost' identified by '$dbpass';"

done
Did this answer your question?