gitlab-org--gitlab-foss/doc/ci/services/mysql.md

119 lines
3.3 KiB
Markdown
Raw Normal View History

2015-12-08 14:11:11 -05:00
# Using MySQL
2015-12-08 14:11:11 -05:00
As many applications depend on MySQL as their database, you will eventually
need it in order for your tests to run. Below you are guided how to do this
with the Docker and Shell executors of GitLab Runner.
2015-12-08 14:11:11 -05:00
## Use MySQL with the Docker executor
2015-12-08 14:11:11 -05:00
If you are using [GitLab Runner](../runners/README.md) with the Docker executor
you basically have everything set up already.
2015-12-08 14:11:11 -05:00
First, in your `.gitlab-ci.yml` add:
2015-12-08 14:11:11 -05:00
```yaml
services:
- mysql:latest
2015-12-08 14:11:11 -05:00
variables:
2016-05-07 00:29:24 -04:00
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
2015-12-08 14:11:11 -05:00
MYSQL_DATABASE: el_duderino
MYSQL_ROOT_PASSWORD: mysql_strong_password
```
2015-12-08 14:11:11 -05:00
And then configure your application to use the database, for example:
2015-12-08 14:11:11 -05:00
```yaml
Host: mysql
2015-12-08 14:11:11 -05:00
User: root
Password: mysql_strong_password
Database: el_duderino
```
If you are wondering why we used `mysql` for the `Host`, read more at
2018-11-02 03:33:18 -04:00
[How services are linked to the job](../docker/using_docker_images.md#how-services-are-linked-to-the-job).
2015-12-08 14:11:11 -05:00
You can also use any other docker image available on [Docker Hub][hub-mysql].
For example, to use MySQL 5.5 the service becomes `mysql:5.5`.
2015-12-08 14:11:11 -05:00
The `mysql` image can accept some environment variables. For more details
check the documentation on [Docker Hub][hub-mysql].
2015-12-08 14:11:11 -05:00
## Use MySQL with the Shell executor
2015-12-08 14:11:11 -05:00
You can also use MySQL on manually configured servers that are using
GitLab Runner with the Shell executor.
2015-12-08 14:11:11 -05:00
First install the MySQL server:
2015-12-08 14:11:11 -05:00
```bash
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
```
2015-12-08 14:11:11 -05:00
Pick a MySQL root password (can be anything), and type it twice when asked.
2015-12-08 14:11:11 -05:00
*Note: As a security measure you can run `mysql_secure_installation` to
remove anonymous users, drop the test database and disable remote logins with
the root user.*
2015-12-08 14:11:11 -05:00
The next step is to create a user, so login to MySQL as root:
2015-12-08 14:11:11 -05:00
```bash
mysql -u root -p
```
2015-12-08 14:11:11 -05:00
Then create a user (in our case `runner`) which will be used by your
application. Change `$password` in the command below to a real strong password.
2015-12-08 14:11:11 -05:00
*Note: Do not type `mysql>`, this is part of the MySQL prompt.*
2015-12-08 14:11:11 -05:00
```bash
mysql> CREATE USER 'runner'@'localhost' IDENTIFIED BY '$password';
```
2015-12-08 14:11:11 -05:00
Create the database:
2015-12-08 14:11:11 -05:00
```bash
mysql> CREATE DATABASE IF NOT EXISTS `el_duderino` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
```
2015-12-08 14:11:11 -05:00
Grant the necessary permissions on the database:
2015-12-08 14:11:11 -05:00
```bash
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES ON `el_duderino`.* TO 'runner'@'localhost';
```
If all went well you can now quit the database session:
```bash
mysql> \q
```
Now, try to connect to the newly created database to check that everything is
in place:
```bash
mysql -u runner -p -D el_duderino
```
As a final step, configure your application to use the database, for example:
```bash
Host: localhost
User: runner
Password: $password
Database: el_duderino
```
## Example project
We have set up an [Example MySQL Project][mysql-example-repo] for your
convenience that runs on [GitLab.com](https://gitlab.com) using our publicly
available [shared runners](../runners/README.md).
Want to hack on it? Simply fork it, commit and push your changes. Within a few
moments the changes will be picked by a public runner and the job will begin.
2015-12-08 14:11:11 -05:00
2016-05-07 00:29:24 -04:00
[hub-mysql]: https://hub.docker.com/r/_/mysql/
2015-12-08 14:11:11 -05:00
[mysql-example-repo]: https://gitlab.com/gitlab-examples/mysql