# `mysql2` gem + `mariadb` + Alpine Linux

**tl;dr** Use `mariadb-dev`. In case of Alpine Linux 3.8, 3.9,
use `mysql2 >= 0.4.10`.

Starting with Alpine Linux 3.8 there are basically 2 options to install
the `mysql2` gem:

`1.sh`:

```sh
#!/bin/sh
set -eu
apk add build-base "$1"
gem install mysql2 -v "$2"
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.11 ./1.sh mariadb-dev 0.5.3
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.3
1 gem installed
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.11 ./1.sh mariadb-connector-c-dev 0.5.3
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.3
1 gem installed
```

But with `mysql2 < 0.4.10` one of them doesn't work:

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.11 ./1.sh mariadb-dev 0.4.9
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.9
1 gem installed
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.11 ./1.sh mariadb-connector-c-dev 0.4.9
...
compiling client.c
In file included from client.c:1:
./mysql2_ext.h:14:10: fatal error: mysql_com.h: No such file or directory
   14 | #include <mysql_com.h>
      |          ^~~~~~~~~~~~~
...
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.11 ./1.sh mariadb-connector-c-dev 0.4.10
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.10
1 gem installed
```

That happens because `mysql2 < 0.4.10` [requires `mysql_com.h`][1]
for some reason (leftover `#include`?).
But most of the time `mysql_com.h` comes with `mariadb-dev`, not with `mariadb-connector-c-dev`.

MariaDB Connector/C renamed `mysql_com.h` to `mariadb_com.h` in [`3.0-cc-server-integ-0`][10].
[MariaDB Connector/C `3.0.1-beta`][11] replaced [`libmysqlclient`][12] in [MariaDB 10.2.2][13].
But MariaDB itself (server) still had the `mysql_com.h` file, so nothing changed in this regard.

Before MariaDB `10.2.8` `mysql_com.h` was a part of the `mariadb` header files, that
were used to build both clients, and server plugins. But in `10.2.8` those
were [split][4], server headers were moved to [`/usr/include/mysql/server`][5].
Then, in `10.2.9` there were added some client [compatibility][6] header
[files][7] (with warnings), particularly `mysql_com.h`.
That means `mysql_com.h` didn't exist in MariaDB `10.2.8`.

So, either upgrade to `mysql2 >= 0.4.10`, or use `mariadb-dev`.

[1]: https://github.com/brianmario/mysql2/commit/8d8fbf94c3fc783a1becafb9efa6db8f4d0985f1
[4]: https://jira.mariadb.org/browse/MDEV-13370
[5]: https://github.com/MariaDB/server/commit/3ec96c1824
[6]: https://jira.mariadb.org/browse/MDEV-13773
[7]: https://github.com/MariaDB/server/commit/3878baddf13
[10]: https://github.com/mariadb-corporation/mariadb-connector-c/commit/4ca933b
[11]: https://github.com/mariadb-corporation/mariadb-connector-c/commit/3e924b3
[12]: https://mariadb.com/kb/en/about-mariadb-connector-c/#integration-with-mariadb-server
[13]: https://github.com/MariaDB/server/commit/79fa256eb2e5b5bacf4b15ef9dae1d738b2c4669

But that's not the only issue that `mysql2-0.4.10` solves.
MariaDB Connector/C doesn't define `MYSQL_SERVER_VERSION` [until `3.0.9`][2].
That affects Alpine Linux 3.8 (`3.0.4`), 3.9 (`3.0.8`). `mysql2 < 0.4.10` doesn't work on these releases.

[2]: https://github.com/mariadb-corporation/mariadb-connector-c/commit/f4ad58c965ec0022cf10eb07b8a4f8face7649dd

So again, either upgrade `mysql2` to >= `0.4.10`, or use Alpine Linux >= `3.10`.

[3]: https://github.com/brianmario/mysql2/commit/f987eec1cbfe77bb6b30f17d4454cd2a5db114d4

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.8 ./1.sh mariadb-dev 0.4.9
...
compiling client.c
In file included from /usr/local/include/ruby-2.6.0/ruby/ruby.h:29:0,
                 from /usr/local/include/ruby-2.6.0/ruby.h:33,
                 from ./mysql2_ext.h:10,
                 from client.c:1:
client.c: In function 'rb_mysql_client_info':
client.c:59:30: error: 'MYSQL_SERVER_VERSION' undeclared (first use in this function)
   #define MYSQL_LINK_VERSION MYSQL_SERVER_VERSION
                              ^
...
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.8 ./1.sh mariadb-dev 0.4.10
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.10
1 gem installed
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.9 ./1.sh mariadb-dev 0.4.9
...
compiling client.c
In file included from /usr/local/include/ruby-2.6.0/ruby/ruby.h:29,
                 from /usr/local/include/ruby-2.6.0/ruby.h:33,
                 from ./mysql2_ext.h:10,
                 from client.c:1:
client.c: In function 'rb_mysql_client_info':
client.c:59:30: error: 'MYSQL_SERVER_VERSION' undeclared (first use in this function); did you mean 'MYSQL_LINK_VERSION'?
   #define MYSQL_LINK_VERSION MYSQL_SERVER_VERSION
                              ^~~~~~~~~~~~~~~~~~~~
...
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.9 ./1.sh mariadb-dev 0.4.10
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.10
1 gem installed
```

```
$ docker run --rm -v $PWD/1.sh:/1.sh ruby:2.6-alpine3.10 ./1.sh mariadb-dev 0.4.9
...
Gem 'mysql2' is not installed
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.9
1 gem installed
```

[MDEV-9055 replace libmysqlclient with Connector/C](https://jira.mariadb.org/browse/MDEV-9055)<br>
[MDEV-9293 change clients to use Connector/C](https://jira.mariadb.org/browse/MDEV-9293)<br>
[MDEV-9267 pull in Connector/C sources into the server tree on builds](https://jira.mariadb.org/browse/MDEV-9267)<br>
[56c4cfe0bea MDEV-9293 - Use MariaDB's Connector/C in server](https://github.com/MariaDB/server/commit/56c4cfe0bea)<br>
[ed0b84a0270 remove libmysql/](https://github.com/MariaDB/server/commit/ed0b84a0270)<br>
[79fa256eb2e really add a submodule](https://github.com/MariaDB/server/commit/79fa256eb2e)<br>
[main/mariadb: upgrade to 10.2.13, modernize](https://git.alpinelinux.org/aports/commit/main/mariadb/APKBUILD?id=6556a505d52c020d0bf835a0f1e12afceb303674)<br>
[main/mariadb: split embedded, embedded-dev, static and server-utils](https://git.alpinelinux.org/aports/commit/main/mariadb/APKBUILD?id=9f6c5c776a20c9ff350aa839795d1c2bf65fb0ac)<br>

Judging from the following commit:

[main/mariadb-connector-c: move from testing](https://github.com/alpinelinux/aports/commit/86472cf416d488df0ea4c9044accd77ff2c583c8)

Connector/C is needed for `mariadb-10.2.x` to work (at least for `mariadb >= 10.2.2`).