Setting up an userspace MariaDB for development
With multiple projects requiring different configuration of mysql and wanting an easy way to clean the database without becoming root, I decided to run MariaDB in a local directory in my home folder.
Set up is quite straight forward:
- Create a new database if it's not already there
- Import any scripts found locally
I've placed it all in a script with the basic premise: if there is no mariadb
directory, then initialize a new database. As a simple bash script you end up with:
#!/bin/bash
ROOT="`pwd`"
if [ ! -d "${ROOT}/mariadb" ]; then
mkdir -p "${ROOT}/mariadb"
mysql_install_db "--datadir=${ROOT}/mariadb" "--basedir=/usr/"
fi
mysqld "--datadir=${ROOT}/mariadb" "--socket=${ROOT}/mariadb/mariadb.socket" &
mysqlPid="$!"
sleep 5 #Should be a netcat test for the mariadb port, but I was to lazy to do that.
echo
for sql in *.sql;do
echo "Importing $sql"
mysql -u root "--socket=${ROOT}/mariadb/mariadb.socket" < "$sql"
done
mysql -u root "--socket=${ROOT}/mariadb/mariadb.socket"
echo "Pres return to stop $mysqlPid"
read
kill "$mysqlPid"
wait
The if
checks to see if there is a mariadb
directory where the script is called from, if not mysql_install_db
a new database. Then we start mysqld
and keep the pid ($!
) for later.
The sleep
is there because the server needs to start up, it should be a wait for the mysql socket to become available loop, but I was to lazy to set that up.
When the mysqld
has started, try to import all the .sql
files we can find. Lastly we try to open up a mysql terminal, so you can look at the current database state.