debian-mirror-gitlab/doc/ci/services/mysql.md

129 lines
3.9 KiB
Markdown
Raw Normal View History

2019-09-04 21:01:54 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Verify
group: Runner
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/#designated-technical-writers
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
```
2019-10-12 21:52:04 +05:30
NOTE: **Note:**
The `MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` variables can't be set in the GitLab UI.
2020-05-24 23:13:21 +05:30
To set them, assign them to a variable [in the UI](../variables/README.md#create-a-custom-variable-in-the-ui),
2019-12-04 20:38:33 +05:30
and then assign that variable to the
2019-10-12 21:52:04 +05:30
`MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` variables in your `.gitlab-ci.yml`.
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
2020-06-23 00:09:42 +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:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
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:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
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.*
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
mysql> CREATE USER 'runner'@'localhost' IDENTIFIED BY '$password';
```
Create the database:
2020-03-13 15:44:24 +05:30
```shell
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:
2020-03-13 15:44:24 +05:30
```shell
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:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
mysql> \q
```
Now, try to connect to the newly created database to check that everything is
in place:
2020-03-13 15:44:24 +05:30
```shell
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:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
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.