2019-09-04 21:01:54 +05:30
|
|
|
---
|
|
|
|
type: reference
|
|
|
|
---
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
# 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:
|
2019-02-15 15:39:39 +05:30
|
|
|
# Configure mysql environment variables (https://hub.docker.com/_/mysql/)
|
2019-09-04 21:01:54 +05:30
|
|
|
MYSQL_DATABASE: "<your_mysql_database>"
|
|
|
|
MYSQL_ROOT_PASSWORD: "<your_mysql_password>"
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
And then configure your application to use the database, for example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
Host: mysql
|
|
|
|
User: root
|
2019-09-04 21:01:54 +05:30
|
|
|
Password: <your_mysql_password>
|
|
|
|
Database: <your_mysql_database>
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
If you are wondering why we used `mysql` for the `Host`, read more at
|
2018-12-13 13:39:08 +05:30
|
|
|
[How services are linked to the job](../docker/using_docker_images.md#how-services-are-linked-to-the-job).
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
You can also use any other docker image available on [Docker Hub](https://hub.docker.com/_/mysql/).
|
2015-12-23 02:04:40 +05:30
|
|
|
For example, to use MySQL 5.5 the service becomes `mysql:5.5`.
|
|
|
|
|
|
|
|
The `mysql` image can accept some environment variables. For more details
|
2019-09-04 21:01:54 +05:30
|
|
|
check the documentation on [Docker Hub](https://hub.docker.com/_/mysql/).
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
## 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:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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.*
|
|
|
|
|
|
|
|
```bash
|
|
|
|
mysql> CREATE USER 'runner'@'localhost' IDENTIFIED BY '$password';
|
|
|
|
```
|
|
|
|
|
|
|
|
Create the database:
|
|
|
|
|
|
|
|
```bash
|
2019-09-04 21:01:54 +05:30
|
|
|
mysql> CREATE DATABASE IF NOT EXISTS `<your_mysql_database>` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
Grant the necessary permissions on the database:
|
|
|
|
|
|
|
|
```bash
|
2019-09-04 21:01:54 +05:30
|
|
|
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES ON `<your_mysql_database>`.* TO 'runner'@'localhost';
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
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
|
2019-09-04 21:01:54 +05:30
|
|
|
mysql -u runner -p -D <your_mysql_database>
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
As a final step, configure your application to use the database, for example:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
Host: localhost
|
|
|
|
User: runner
|
|
|
|
Password: $password
|
2019-09-04 21:01:54 +05:30
|
|
|
Database: <your_mysql_database>
|
2015-12-23 02:04:40 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
## Example project
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
We have set up an [Example MySQL Project](https://gitlab.com/gitlab-examples/mysql) for your
|
2015-12-23 02:04:40 +05:30
|
|
|
convenience that runs on [GitLab.com](https://gitlab.com) using our publicly
|
|
|
|
available [shared runners](../runners/README.md).
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
Want to hack on it? Simply fork it, commit and push your changes. Within a few
|
2017-08-17 22:00:37 +05:30
|
|
|
moments the changes will be picked by a public runner and the job will begin.
|