2019-05-30 06:21:27 +00:00
---
2020-05-29 18:08:26 +00:00
stage: Verify
group: Runner
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-05-30 06:21:27 +00:00
type: reference
---
2015-12-08 19:11:11 +00:00
# Using MySQL
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
Many applications depend on MySQL as their database, and you may
need it for your tests to run.
2015-11-25 13:41:14 +00:00
2015-12-08 19:11:11 +00:00
## Use MySQL with the Docker executor
2015-11-25 13:41:14 +00:00
2021-06-28 12:38:12 +00:00
If you want to use a MySQL container, you can use [GitLab Runner ](../runners/index.md ) with the Docker executor.
2015-11-25 13:41:14 +00:00
2021-05-10 03:10:06 +00:00
This example shows you how to set a username and password that GitLab uses to access the MySQL container. If you do not set a username and password, you must use `root` .
2021-06-28 15:08:03 +00:00
1. [Create CI/CD variables ](../variables/index.md#custom-cicd-variables ) for your
2020-09-14 03:09:21 +00:00
MySQL database and password by going to **Settings > CI/CD** , expanding **Variables** ,
and clicking **Add Variable** .
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
This example uses `$MYSQL_DB` and `$MYSQL_PASS` as the keys.
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. To specify a MySQL image, add the following to your `.gitlab-ci.yml` file:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```yaml
services:
- mysql:latest
```
2019-07-24 06:11:14 +00:00
2020-09-14 03:09:21 +00:00
- You can use any Docker image available on [Docker Hub ](https://hub.docker.com/_/mysql/ ).
For example, to use MySQL 5.5, use `mysql:5.5` .
- The `mysql` image can accept environment variables. For more information, view
the [Docker Hub documentation ](https://hub.docker.com/_/mysql/ ).
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. To include the database name and password, add the following to your `.gitlab-ci.yml` file:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```yaml
variables:
# Configure mysql environment variables (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: $MYSQL_DB
MYSQL_ROOT_PASSWORD: $MYSQL_PASS
```
2015-12-08 21:49:05 +00:00
2020-09-14 03:09:21 +00:00
The MySQL container uses `MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` to connect to the database.
Pass these values by using variables (`$MYSQL_DB` and `$MYSQL_PASS` ),
[rather than calling them directly ](https://gitlab.com/gitlab-org/gitlab/-/issues/30178 ).
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Configure your application to use the database, for example:
```yaml
Host: mysql
User: runner
Password: < your_mysql_password >
Database: < your_mysql_database >
```
2015-11-25 13:41:14 +00:00
2021-05-10 03:10:06 +00:00
In this example, the user is `runner` . You should use a user that has permission to
access your database.
2015-12-08 19:11:11 +00:00
## Use MySQL with the Shell executor
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
You can also use MySQL on manually-configured servers that use
2015-12-08 19:11:11 +00:00
GitLab Runner with the Shell executor.
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Install the MySQL server:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```shell
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
```
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Choose a MySQL root password and type it twice when asked.
2015-11-25 13:41:14 +00:00
2020-12-04 21:09:29 +00:00
NOTE:
2020-09-14 03:09:21 +00:00
As a security measure, you can run `mysql_secure_installation` to
remove anonymous users, drop the test database, and disable remote logins by
the root user.
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Create a user by logging in to MySQL as root:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```shell
mysql -u root -p
```
2015-11-25 13:41:14 +00:00
2020-11-23 21:09:19 +00:00
1. Create a user (in this case, `runner` ) that is used by your
2020-09-14 03:09:21 +00:00
application. Change `$password` in the command to a strong password.
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
At the `mysql>` prompt, type:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```sql
CREATE USER 'runner'@'localhost' IDENTIFIED BY '$password';
```
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Create the database:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```sql
CREATE DATABASE IF NOT EXISTS `<your_mysql_database>` DEFAULT CHARACTER SET `utf8` \
COLLATE `utf8_unicode_ci` ;
```
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
1. Grant the necessary permissions on the database:
2015-11-25 13:41:14 +00:00
2020-09-14 03:09:21 +00:00
```sql
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER, LOCK TABLES ON `<your_mysql_database>` .* TO 'runner'@'localhost';
```
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
1. If all went well, you can quit the database session:
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
```shell
\q
```
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
1. Connect to the newly-created database to check that everything is
in place:
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
```shell
mysql -u runner -p -D < your_mysql_database >
```
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
1. Configure your application to use the database, for example:
2015-12-08 19:11:11 +00:00
2020-09-14 03:09:21 +00:00
```shell
Host: localhost
User: runner
Password: $password
Database: < your_mysql_database >
```
2015-12-08 19:11:11 +00:00
## Example project
2020-09-14 03:09:21 +00:00
To view a MySQL example, create a fork of this [sample project ](https://gitlab.com/gitlab-examples/mysql ).
2021-06-28 12:38:12 +00:00
This project uses publicly-available [shared runners ](../runners/index.md ) on [GitLab.com ](https://gitlab.com ).
2020-09-14 03:09:21 +00:00
Update the README.md file, commit your changes, and view the CI/CD pipeline to see it in action.