Skip to content

Commit da872a0

Browse files
authored
Add MySQL docs, update example (#1243)
1 parent ab2e8a7 commit da872a0

File tree

8 files changed

+137
-6
lines changed

8 files changed

+137
-6
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: MySQL
3+
---
4+
MySQL can be automatically configured for your dev environment by Devbox via the built-in MySQL Plugin. This plugin will activate automatically when you install MySQL using `devbox add mysql80` or `devbox add mysql57`.
5+
6+
[**Example Repo**](https://github.com/jetpack-io/devbox/tree/main/examples/databases/mysql)
7+
8+
[![Open In Devbox.sh](https://jetpack.io/img/devbox/open-in-devbox.svg)](https://devbox.sh/github.com/jetpack-io/devbox/?folder=examples/databases/mysql)
9+
10+
## Adding MySQL to your Shell
11+
12+
`devbox add mysql80`, or in your `devbox.json` add
13+
14+
```json
15+
"packages": [
16+
"mysql80@latest"
17+
]
18+
```
19+
20+
You can also install Mysql 5.7 by using `devbox add mysql57`
21+
22+
You can manually add the MySQL Plugin to your `devbox.json` by adding it to your `include` list:
23+
24+
```json
25+
"include": [
26+
"plugin:mysql"
27+
]
28+
```
29+
30+
## MySQL Plugin Support
31+
32+
Devbox will automatically create the following configuration when you run `devbox add mysql80` or `devbox add mysql57`. You can view the full configuration by running `devbox info mysql`
33+
34+
35+
### Services
36+
* mysql
37+
38+
You can use `devbox services up|stop mysql` to start or stop the MySQL Server.
39+
40+
### Environment Variables
41+
42+
```bash
43+
MYSQL_BASEDIR=.devbox/nix/profile/default
44+
MYSQL_HOME=./.devbox/virtenv/mysql/run
45+
MYSQL_DATADIR=./.devbox/virtenv/mysql/data
46+
MYSQL_UNIX_PORT=./.devbox/virtenv/mysql/run/mysql.sock
47+
MYSQL_PID_FILE=./.devbox/mysql/run/mysql.pid
48+
```
49+
50+
### Files
51+
52+
The plugin will also create the following helper files in your project's `.devbox/virtenv` folder:
53+
54+
* mysql/flake.nix
55+
* mysql/setup_db.sh
56+
* mysql/process-compose.yaml
57+
58+
These files are used to setup your database and service, and should not be modified
59+
60+
### Notes
61+
62+
* This plugin wraps mysqld to work in your local project. For more information, see the `flake.nix` created in your `.devbox/virtenv/mysql` folder.
63+
* This plugin will create a new database for your project in `MYSQL_DATADIR` if one doesn't exist on shell init.
64+
* You can use `mysqld` to manually start the server, and `mysqladmin -u root shutdown` to manually stop it
65+
* `.sock` filepath can only be maximum 100 characters long. You can point to a different path by setting the `MYSQL_UNIX_PORT` env variable in your `devbox.json` as follows:
66+
67+
```json
68+
"env": {
69+
"MYSQL_UNIX_PORT": "/<some-other-path>/mysql.sock"
70+
}
71+
```

docs/app/docs/devbox_examples/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You can view the full list of examples in our [Example Repo](https://github.com/
2222

2323
## Databases
2424
* [MariaDB](databases/mariadb.md)
25+
* [MySQL](databases/mysql.md)
2526
* [PostgreSQL](databases/postgres.md)
2627
* [Redis](databases/redis.md)
2728

docs/app/docs/guides/plugins.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Plugins are available for the following packages. You can activate the plugins f
1212
* [Caddy](../devbox_examples/servers/caddy.md) (caddy)
1313
* [Nginx](../devbox_examples/servers/nginx.md) (nginx)
1414
* [MariaDB](../devbox_examples/databases/mariadb.md) (mariadb, mariadb_10_6...)
15+
* [MySQL](../devbox_examples/databases/mysql.md) (mysql80, mysql57)
1516
* [PostgreSQL](../devbox_examples/databases/postgres.md) (postgresql)
1617
* [Redis](../devbox_examples/databases/redis.md) (redis)
1718
* [PHP](../devbox_examples/languages/php.md) (php, php80, php81, php82...)

examples/databases/mysql/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# mysql
2+
3+
## mysql Notes
4+
5+
1. Start the mysql server using `devbox services up`
6+
1. Create a database using `"mysql -u root < setup_db.sql"`
7+
1. You can now connect to the database from the command line by running `devbox run connect_db`
8+
9+
## Services
10+
11+
* mysql
12+
13+
Use `devbox services start|stop [service]` to interact with services
14+
15+
## This plugin sets the following environment variables
16+
17+
* MYSQL_BASEDIR=/<projectDir>/.devbox/nix/profile/default
18+
* MYSQL_HOME=/<projectDir>/.devbox/virtenv/mysql/run
19+
* MYSQL_DATADIR=/<projectDir>/.devbox/virtenv/mysql/data
20+
* MYSQL_UNIX_PORT=/<projectDir>/.devbox/virtenv/mysql/run/mysql.sock
21+
* MYSQL_PID_FILE=/<projectDir>/.devbox/virtenv/mysql/run/mysql.pid
22+
23+
To show this information, run `devbox info mysql`
24+
25+
Note that the `.sock` filepath can only be maximum 100 characters long. You can point to a different path by setting the `MYSQL_UNIX_PORT` env variable in your `devbox.json` as follows:
26+
27+
```
28+
"env": {
29+
"MYSQL_UNIX_PORT": "/<some-other-path>/mysql.sock"
30+
}
31+
```

examples/databases/mysql/devbox.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
"mysql80@latest"
44
],
55
"shell": {
6-
"init_hook": [
7-
"echo 'Welcome to devbox!' > /dev/null"
8-
],
6+
"init_hook": [],
97
"scripts": {
10-
"test": [
11-
"echo \"Error: no test specified\" && exit 1"
8+
"connect_db": [
9+
"mysql -u devbox_user -p -D devbox_lamp"
10+
],
11+
"test_db_setup": [
12+
"mkdir -p /tmp/devbox/mariadb/run",
13+
"export MYSQL_UNIX_PORT=/tmp/devbox/mariadb/run/mysql.sock",
14+
"devbox services up -b",
15+
"sleep 5",
16+
"mysql -u root < setup_db.sql",
17+
"devbox services stop"
1218
]
1319
}
1420
}

examples/databases/mysql/devbox.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"packages": {
44
"mysql80@latest": {
55
"last_modified": "2023-05-01T16:53:22Z",
6-
"plugin_version": "0.0.2",
6+
"plugin_version": "0.0.1",
77
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#mysql80",
88
"version": "8.0.33"
99
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- You should run this query using `mysql -u root < setup_db.sql`
2+
3+
DROP DATABASE IF EXISTS devbox_lamp;
4+
CREATE DATABASE devbox_lamp;
5+
6+
USE devbox_lamp;
7+
8+
CREATE USER 'devbox_user'@'localhost' IDENTIFIED BY 'password';
9+
GRANT ALL ON devbox_lamp.* TO 'devbox_user'@'localhost';
10+
11+
DROP TABLE IF EXISTS colors;
12+
CREATE TABLE colors (
13+
id INT NOT NULL AUTO_INCREMENT,
14+
name VARCHAR(100) NOT NULL,
15+
hex VARCHAR(7) NOT NULL,
16+
PRIMARY KEY (id));
17+
18+
INSERT INTO colors (name, hex) VALUES ('red', '#FF0000'), ('blue', '#0000FF'), ('green', '#00FF00');
19+
20+

internal/templates/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var templates = map[string]string{
3939
"maelstrom": "examples/cloud_development/maelstrom/",
4040
"minikube": "examples/cloud_development/minikube/",
4141
"mariadb": "examples/databases/mariadb/",
42+
"mysql": "examples/databases/mysql/",
4243
"nginx": "examples/servers/nginx/",
4344
"nim": "examples/development/nim/spinnytest/",
4445
"node-npm": "examples/development/nodejs/nodejs-npm/",

0 commit comments

Comments
 (0)