summaryrefslogtreecommitdiff
path: root/doc/ci/services/mysql.md
blob: dcfd863709ec4cc9ff873d6d64fc7e0bf670722f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
type: reference
---

# Using MySQL

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.

## Use MySQL with the Docker executor

If you are using [GitLab Runner](../runners/README.md) with the Docker executor
you basically have everything set up already.

First, in your `.gitlab-ci.yml` add:

```yaml
services:
  - mysql:latest

variables:
  # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: "<your_mysql_database>"
  MYSQL_ROOT_PASSWORD: "<your_mysql_password>"
```

NOTE: **Note:**
The `MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` variables can't be set in the GitLab UI.
To set them, assign them to a variable [in the UI](../variables/README.md#create-a-custom-variable-in-the-ui),
and then assign that variable to the
`MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` variables in your `.gitlab-ci.yml`.

And then configure your application to use the database, for example:

```yaml
Host: mysql
User: root
Password: <your_mysql_password>
Database: <your_mysql_database>
```

If you are wondering why we used `mysql` for the `Host`, read more at
[How services are linked to the job](../docker/using_docker_images.md#how-services-are-linked-to-the-job).

You can also use any other docker image available on [Docker Hub](https://hub.docker.com/_/mysql/).
For example, to use MySQL 5.5 the service becomes `mysql:5.5`.

The `mysql` image can accept some environment variables. For more details
check the documentation on [Docker Hub](https://hub.docker.com/_/mysql/).

## Use MySQL with the Shell executor

You can also use MySQL on manually configured servers that are using
GitLab Runner with the Shell executor.

First install the MySQL server:

```shell
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
```

Pick a MySQL root password (can be anything), and type it twice when asked.

*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.*

The next step is to create a user, so login to MySQL as root:

```shell
mysql -u root -p
```

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.

*Note: Do not type `mysql>`, this is part of the MySQL prompt.*

```shell
mysql> CREATE USER 'runner'@'localhost' IDENTIFIED BY '$password';
```

Create the database:

```shell
mysql> CREATE DATABASE IF NOT EXISTS `<your_mysql_database>` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
```

Grant the necessary permissions on the database:

```shell
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES ON `<your_mysql_database>`.* TO 'runner'@'localhost';
```

If all went well you can now quit the database session:

```shell
mysql> \q
```

Now, try to connect to the newly created database to check that everything is
in place:

```shell
mysql -u runner -p -D <your_mysql_database>
```

As a final step, configure your application to use the database, for example:

```shell
Host: localhost
User: runner
Password: $password
Database: <your_mysql_database>
```

## Example project

We have set up an [Example MySQL Project](https://gitlab.com/gitlab-examples/mysql) 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.