Skip to main content

Databases

This page explains how to use databases and other services inside the CI environment.

Overview

Starting databases and service in Linux environments is easy with the sem-service tool. Once started, create users, and tables, and populate the database with data so you can run your test suite.

Postgres

You can start a PostgreSQL service with:

sem-service start postgres

To create users in PostgreSQL:

# normal user
psql -U postgres -h localhost -c "CREATE USER developer WITH PASSWORD 'developer';"

# admin user
psql -U postgres -h localhost -c "CREATE USER developer WITH PASSWORD 'developer';"
psql -U postgres -h localhost -c "ALTER USER developer WITH SUPERUSER;"

You can also create extensions to expand the capabilities of the database with:

psql -U postgres -h localhost -c "CREATE EXTENSION \"uuid-ossp\""

MySQL

To start a MySQL database use:

sem-service start mysql

To create users in MySQL:

# normal user
mysql -h 127.0.0.1 -P 3306 -u root -e "CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';"

# admin user
mysql -h 127.0.0.1 -P 3306 -u root -e "CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';"

If your repository contains a database dump, you can initialize the database in the CI environment like this:

checkout
sem-service start mysql
mysql -h 127.0.0.1 -P 3306 -u root < dump.sql

Redis

To start a Redis service:

sem-service start redis

To interact with the Redis service, use the following commands:

sem-service start redis
sudo apt install redis-tools -y
# Now you can use the redis-cli, e.g.
redis-cli incr mycounter

MongoDB

You can start a MongoDB database with:

sem-service start mongodb

Create users with:

# normal user
echo 'db.createUser( {user:"username", pwd:"password", roles:[], mechanisms:["SCRAM-SHA-1"] } )' | mongo s2

# admin user
echo 'db.createUser({user:"username", pwd:"password", roles:[{role:"userAdminAnyDatabase",db:"admin"}], mechanisms:["SCRAM-SHA-1"]})' | mongo admin

To interact with the MongoDB service use:

sem-service start mongodb
sudo apt install mongodb-clients -y
# create user
echo 'db.createUser( {user:"username", pwd:"password", roles:[], mechanisms:["SCRAM-SHA-1"] } )' | mongo s2

Memcached

You can start a Memcached service with:

sem-service start memcached

To interact with the Memcached instance use:

sem-service start memcached
sudo apt install libmemcached-tools -y
memcstat --servers="127.0.0.1"

Elasticsearch

To start an Elasticsearch service:

sem-service start elasticsearch

You can create users with:

# normal user
sudo /usr/share/elasticsearch/bin/elasticsearch-users useradd new_user -p password -r reporting_user

# admin user
sudo /usr/share/elasticsearch/bin/elasticsearch-users useradd new_user -p password -r superuser

To use an Elasticsearch instance, follow these steps:

sem-service start elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.deb
sudo dpkg -i elasticsearch-6.3.2.deb
sudo /usr/share/elasticsearch/bin/elasticsearch-users useradd new_user -p password -r reporting_user
sudo /usr/share/elasticsearch/bin/elasticsearch-users list

See also