5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laradockで構築したMySQLの日本語化けを解決

Last updated at Posted at 2020-03-24

#はじめに
LaradockでLaravelの環境構築をしました
ところが、マイグレーションを実行しRegisterを実行しDBを確認したところ
日本語が???と文字化けしてしまっていました
色んな記事を参考に試したものの、どれもうまく行かなかったので
備忘録として残しておきます

#初期のDBの設定
まずは初期状態のDBの文字コードの設定は下記でした

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

これをutf8mb4に変更していきます

#my.cnfの編集
laradock/mysql/my.cnfを下記に書き換えます

my.cnf
 # The MySQL  Client configuration file.
 #
 # For explanations see
 # http://dev.mysql.com/doc/mysql/en/server-system-variables.html

 [mysql]

[mysqld]
skip-character-set-client-handshake
character-set-server = utf8mb4
# collation-server = utf8mb4_general_ci
collation-server = utf8mb4_unicode_ci
init-connect = SET NAMES utf8mb4

[client]
default-character-set=utf8mb4

#database.phpの編集
config/database.phpを下記のように変更します

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',    //変更
            'collation' => 'utf8mb4_unicode_ci',    //変更
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]

編集はこれで終了

#buildする
--buildオプションをつけてコンテナを起動します
下記コマンドを実行

$ docker-compose up -d --build workspace nginx mysql

mysqlコンテンに入り文字コードを確認する

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

utf8mb4に変更することができました

DBの文字化けしていた部分を確認すると、日本語で表示されていることが確認できました

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?