MySQL データベース 環境構築

MySqlの文字コードをutf-8に設定する

概要

MySqlをインストールから利用しはじめて、文字コードで苦戦したことは無いだろうか?
私は今回大苦戦した。
このブログでは、MySqlの文字コードをutf-8に設定するにあたっての設定方法、注意点を記載する。

きっかけ

で、MySqlをインストールし簡単な動作検証を始めたのだが、すぐ以下のようなエラーで悩むことになった。
単純なinsert文がエラーとなる。文字コードが関係してそうなエラー内容だ。

insert into raceinfo
values('123456789012', '20230101' ,'あああ' , '01', 1,1,10,'shib',1600)

前提となる環境

■ホストOS
Windows11 バージョン22H2
■ゲストOS
Ubuntu 22.04.1 LTS
VirtualBox 6.1
■インストールするMySQL
MySQL 5.7.40
■外部接続確認アプリケーション
HeidSQL 12.3.0.6589(64bit)

現在の文字コードの確認

show variables like '%char%';

思ったとおり、character_set_database が、latin1 という私には見たことの無い文字コードだった。
(調査すると、ISO/IEC 8859-1で規定されているラテンアルファベットの文字コード標準)

文字コード変更の手順

/etc/mysql/my.cnf
を確認すると、以下のとおり
/etc/mysql/conf.d/
/etc/mysql/mysql.conf.d/
2カ所に分けて設定を記述するようだ。

  1 # Copyright (c) 2016, 2022, Oracle and/or its affiliates.
  2 #
  3 # This program is free software; you can redistribute it and/or modify
  4 # it under the terms of the GNU General Public License, version 2.0,
  5 # as published by the Free Software Foundation.
  6 #
  7 # This program is also distributed with certain software (including
  8 # but not limited to OpenSSL) that is licensed under separate terms,
  9 # as designated in a particular file or component or in included license
 10 # documentation.  The authors of MySQL hereby grant you an additional
 11 # permission to link the program and your derivative works with the
 12 # separately licensed software that they have included with MySQL.
 13 #
 14 # This program is distributed in the hope that it will be useful,
 15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 # GNU General Public License, version 2.0, for more details.
 18 #
 19 # You should have received a copy of the GNU General Public License
 20 # along with this program; if not, write to the Free Software
 21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 22
 23 !includedir /etc/mysql/conf.d/
 24 !includedir /etc/mysql/mysql.conf.d/

今回は、/etc/mysql/mysql.conf.d/mysqld.cnf に設定を追記することにする。
sudo vim mysqld.cnf


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

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
# bind-address  = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# 2023/01/13追加 文字コード指定
character-set-server=utf8

[client]
default-character-set=utf8

保存後、MySqlを再起動する。

sudo service mysql restart

文字コードが変更されたことの確認

既に作成しているデータベースの場合の注意点

前章で設定して反映される内容と、設定の意味は以下のとおりである。

[client]以下
default-character-set=utf8 を記載することによって設定変更される項目
・character_set_client
・character_set_connection
・character_set_results

[mysqld]以下
character-set-server=utf8 を記載することによって設定変更される項目
・character_set_database
・character_set_server
・character_set_system

以下は変更対象外
character_set_filesystem
character_sets_dir

ここで肝心の、character_set_databaseは、ユーザーが作成済みのデータベースには適用されない。
したがって、私の場合は
DROP DATABASE
CREATE DATABASE
でデータベースの再作成を実施した。
既に、多くのテーブルを作成している場合、個々のテーブル、カラムに対して作業が必要なので注意が必要である。

-MySQL, データベース, 環境構築
-