DjangoのデータストアにMariaDBを使う準備



## MraiDB Sever

### 本体インストール
Homebrewでインストールする。（その前に、ローカルに古いMySQLがインストールされていたので諸々削除した。）

```
$ brew list | grep mysql
$ brew list | grep mariadb
$ brew install mariadb

... (中略) ...
==> Pouring mariadb-10.2.6.el_capitan.bottle.tar.gz
==> Using the sandbox
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To connect:
    mysql -uroot

To have launchd start mariadb now and restart at login:
  brew services start mariadb
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mariadb/10.2.6: 620 files, 145.6MB

```

### DB設定

#### DBサーバ起動
```
$ mysql.server start
Starting MySQL
.170616 23:05:14 mysqld_safe Logging to '/usr/local/var/mysql/shoota.local.err'.
170616 23:05:14 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
 SUCCESS! 
```

#### mysqlにログインして操作

```
$ mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.6-MariaDB Homebrew

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

```

#### DB/user作成

```
MariaDB [(none)]> CREATE DATABASE mysite_db CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite_db.* TO mysite_user@localhost IDENTIFIED BY '********';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> exit
Bye
```

確認する。

```
$ mysql -u mysite_user -p********
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.6-MariaDB Homebrew

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysite_db          |
| test               |
+--------------------+
3 rows in set (0.00 sec)

```



## クライアントドライバ

とりあえずpip3でインストールしてみる

```
$ pip3 install mysqlclient

... (中略)  ...

_mysql.c:1911:42: error: no member named 'reconnect' in 'struct st_mysql'
            if ( reconnect != -1 ) self->connection.reconnect = reconnect;
                                   ~~~~~~~~~~~~~~~~ ^
    1 error generated.
    error: command 'clang' failed with exit status 1
    
    ----------------------------------------
Command "/usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/d2/gv7h2z9j3l924fwbmx65sztw0000gn/T/pip-build-8pji4qs8/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/d2/gv7h2z9j3l924fwbmx65sztw0000gn/T/pip-09g524al-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/d2/gv7h2z9j3l924fwbmx65sztw0000gn/T/pip-build-8pji4qs8/mysqlclient/
```
めっちゃエラーでた。

[公式のREADME](https://github.com/PyMySQL/mysqlclient-python)にナンカ書いてた。

> Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
>
> `sudo apt-get install python3-dev` # debian / Ubuntu
> 
> `sudo yum install python3-devel` # Red Hat / CentOS
> 
> `brew install mysql-connector-c` # macOS (Homebrew)

python3だと、`brew install mysql-connector-c`が必要でした。


### うまくいかない

`brew install mysql-connector-c`が必要だと書いていたが、実際うまくいかなかった。
よくよく調べてみると、MariaDB10.2.x以降はこれだけだとうまく行かないらしく、[追加の手順](https://stackoverflow.com/questions/44239393/installing-mysqlclient-for-mariadb-on-mac-os-for-python3)が必要とのこと。

```
$ brew unlink mariadb
Unlinking /usr/local/Cellar/mariadb/10.2.6... 124 symlinks removed
shoota-2:mysite[master]$ brew install mariadb-connector-c
==> Downloading https://homebrew.bintray.com/bottles/mariadb-connector-c-2.2.2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mariadb-connector-c-2.2.2.el_capitan.bottle.tar.gz
==> Using the sandbox
🍺  /usr/local/Cellar/mariadb-connector-c/2.2.2: 58 files, 1MB

$ ln -s /usr/local/opt/mariadb-connector-c/bin/mariadb_config /usr/local/bin/mysql_config

$ pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /Users/kumanoshuta/Library/Caches/pip/wheels/32/50/86/c7be3383279812efb2378c7b393567569a8ab1307c75d40c5a
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.10

$ rm /usr/local/bin/mysql_config

$ brew unlink mariadb-connector-c
Unlinking /usr/local/Cellar/mariadb-connector-c/2.2.2... 4 symlinks removed

$ brew link mariadb
Linking /usr/local/Cellar/mariadb/10.2.6... 124 symlinks created
```


これでサーバ・クライアントの準備ができたはず。

## 開発中に使用しそうなコマンドをメモ

- サーバ側
  - 起動：`mysql.server start`
  - 確認：`mysql.server status`
  - 停止：`mysql.server stop`
- クライアント側
  - ログイン：`mysql -u mysite_user -p********`


次は[Adminの設定](http://qiita.com/shoota_github/items/4985f6a3639a3cc674ae)をする