MySQL は, オープンソースのリレーショナルデータベース管理システムである. このページでは, MySQL をインストールし,設定する手順を示す.
【目次】
MySQL のデータベース管理者は,データベースへのあらゆる操作を許されたユーザである. データベース管理者には,rootという名前が付いている(変更も可能). そのパスワードは,自由に設定できる. パスワードは,必ず,適切に設定すること.
既定(デフォルト)のキャラクタセット: cp932またはutf8
「MySQL Installer for Windows」をクリック.
「Windows(x86, 32-bit), MSI Installer」の右横にある 「Download」をクリック.
すでに「サインアップ」済みなら, 「ログイン」を選び, 電子メールアドレス(Email)と, パスワード(Password)を入力.
まだ「サインアップ」していないのなら, 「サインアップ」を選ぶ, 電子メールアドレスとパスワードを登録する.
「Execute」をクリック.
「Next」をクリック.
「Next」をクリック.
選択したら,「Next」をクリック.
問題がなければ,既定(デフォルト)のままでよい. 「Next」をクリック.
問題がなければ,既定(デフォルト)のままでよい. 「Next」をクリック.
設定したら,「Next」をクリック.
問題がなければ,既定(デフォルト)のままでよい. 「Next」をクリック.
問題がなければ, 「Execute」をクリック.
問題がなければ, 「Finish」をクリック.
「Next」をクリック.
問題がなければ,MySQL ルータは使わないことにする. 「Finish」をクリック.
「Next」をクリック.
MySQL データベース管理者のパスワードを入れ, 「Check」をクリック.
問題がなければ, 「Next」をクリック.
「Execute」をクリック.
「Finish」をクリック.
「Next」をクリック.
「Finish」をクリック.
「\quit」で終了.
システム環境変数 PATH に次を追加
C:\Program Files\MySQL\MySQL Server 8.0\bin
MySQL データベース管理者(root) でログインしたいので,次のように,-u のあとに「root」と書く.
mysql -u root -p
データベースを作成するために,SQL の create database コマンドを使用する.
「root」は MySQL データベース管理者のユーザ名
mysql -u root -p
プロンプトが出るので, ここでは,MySQL データベース管理者のパスワードを入れる.
画面にパスワードが表示されないのは正常動作.
実行後,エラーメッセージが出ないことを確認
utf8 の場合の生成例
create database testdb default character set utf8 collate utf8_unicode_ci;
cp932 の場合の生成例
create database testdb default character set cp932 collate cp932_japanese_ci;
show databases;
\quit
「root」は MySQL データベース管理者のユーザ名, 「testdb」はデータベース名.
mysql -u root -p -D testdb
プロンプトが出るので, ここでは,MySQL データベース管理者のパスワードを入れる.
画面にパスワードが表示されないのは正常動作.
実行後,エラーメッセージが出ないことを確認
create table order_records ( id integer primary key not null, year integer not null CHECK ( year > 2008 ), month integer not null CHECK ( month >= 1 AND month <= 12 ), day integer not null CHECK ( day >= 1 AND day <= 31 ), customer_name text not null, product_name text not null, unit_price real not null check ( unit_price > 0 ), qty integer not null default 1 check ( qty > 0 ), created_at timestamp not null default current_timestamp, updated_at timestamp not null default current_timestamp on update current_timestamp, check ( ( unit_price * qty ) < 200000 ) );
start transaction; insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2020, 7, 26, 'kaneko', 'orange A', 1.2, 10 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2020, 7, 26, 'miyamoto', 'Apple M', 2.5, 2 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2020, 7, 27, 'kaneko', 'orange B', 1.2, 8 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2020, 7, 28, 'miyamoto', 'Apple L', 3 ); commit;
start transaction; update order_records set unit_price = 11.2 where id = 1; commit; select * from order_records;
select * from order_records;
show tables;
\quit
次の手順でパスワードを変更できる.
mysqladmin --user=root --password=(古いパスワード) password "(新しいパスワード)"
既定(デフォルト)のキャラクタセットとして, cp932やutf8 などを以下の手順で設定しておく.
my.ini は, C:\ProgramData\MySQL\MySQL Server 8.0 のようなディレクトリにある.
■ 文字コードをcp932にしたい場合には,次のように my.ini を書きかえる.
もし,すでに,「default-character-set=latin」のような行が存在する場合には, 「cp932」のように書き換える
character-set-server=cp932
default-character-set=cp932
■ 文字コードをutf8にしたい場合には,次のように my.ini を書きかえる.
character-set-server=utf8
default-character-set=utf8