1
2

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.

MySQLメモ

Last updated at Posted at 2018-11-01

1つのページに複数のDBについての情報をまとめたかったが、煩雑になってわかりにくくなった・・・

1.インストールとアンインストール

1-1.インストールの準備

yumリポジトリをインストールしておく
https://dev.mysql.com/downloads/repo/yum/

最新バージョン以外をインストールしたい場合は、

yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community

1-2.インストール

yum install mysql-community-server

依存関係のある以下もインストールされる。
mysql-community-client
mysql-community-common
mysql-community-libs

 1-2-1.Windows版のインストールメモ

Windows版でMySQL8をインストールすると、インストール先を選択できないことがあった。
インストールするモジュールを選択する画面の次で、インストール先を選択する画面が出ずに
【Execute】ボタンの画面が表示された場合は、エクスプローラで隠しファイルを表示する設定にして、「C:\ProgramData\MySQL\MySQL Server 8.0\Data」フォルダを作成すると解決する。

1-3.アンインストール

yum remove mysql-community-server

これだけだとデータは削除されない。
データを削除するには

rm -rf /var/lib/mysql

を実行する

1-4.インストールされているものの確認

yum list installed |grep mysql

2.起動と停止

2-1.サービス起動

RHEL7(MySQL8 or MySQL5.6)

systemctl start mysqld.service

RHEL6(MySQL8 or MySQL5.6)

service mysqld start

2-2.初回ログイン

MySQL8(RHEL7 or RHEL6)

grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p

MySQL5.6(RHEL7 or RHEL6)

mysql -uroot

2-3.停止

RHEL7(MySQL8 or MySQL5.6)

systemctl stop mysqld.service

RHEL6(MySQL8 or MySQL5.6)

service mysqld stop

2-4.起動状況の確認

port番号の確認

netstat -tlpn

3306がlistenしていれば動いている
オプションの意味は
t:tcpを表示
l:接続待ち(listen)でデフォルトは省略される
p:プログラムのPIDと名前を表示
n:名前解決せずに数字で表示

3.ユーザー登録

3-1.rootパスワード変更

MySQL5.6

update mysql.user set password=password('hogepasswd') where user = 'root';
flush privileges;

MySQL8

SET GLOBAL validate_password.length=4;
SET GLOBAL validate_password.policy=LOW;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'hogepasswd';

3-2.一般ユーザー登録

MySQL5.6

grant all privileges on *.* to hogeuser@'%' identified by 'hogepasswd';
grant all privileges on *.* to hogeuser@localhost identified by 'hogepasswd';

「'%'」だけでは自端末から接続できない(※)ので「localhost」も指定する
※「-h 」なら接続できる

MySQL8

CREATE USER  hogeuser@localhost IDENTIFIED WITH mysql_native_password BY 'hogepasswd';
GRANT ALL PRIVILEGES on *.* to hogeuser@localhost;
CREATE USER  hogeuser@'%'IDENTIFIED WITH mysql_native_password BY 'hogepasswd';
GRANT ALL PRIVILEGES on *.* to hogeuser@'%';

4.接続

4-1.接続コマンド

mysql -u <ユーザ名> -p<パスワード> -h <ホスト名> <DB名>

-pとパスワードの値はスペースを空けない
その他はスペースを空けても、空けなくてもどちらでも問題ない

4-2.接続権限の確認

MySQL5.6

select user , host from mysql.user;

5.ODBC

5-1.ODBCインストール

yum install mysql-connector-odbc

5-2.odbc.ini

[MySQL]
Driver = /usr/lib64/libmyodbc8a.so
SERVER = localhost
PORT = 3306
USER = hogeuser
Password = hogepasswd
Database = hogedb
Charset = CP932
Option = 2

6.文字コード

6-1./etc/my.cnf

[mysqld]
character_set_server=sjis
default_authentication_plugin=mysql_native_password

[mysql]
default_character_set=sjis

6-2.確認方法

show variables like "chara%";

7.エラー対応

7-1.ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

「/etc/my.ini」の[mysqld]に以下を追加する

/etc/my.cnf
default_authentication_plugin=mysql_native_password
validate_password.length=3
validate_password.mixed_case_count=0
validate_password.policy=LOW

my.cnfはrootのパスワード変更後に変えないとエラーになる。
またはmy.cnfを変えなくても、以下でも大丈夫そう。(以下もrootのパスワードの変更は必要)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MySQL_80';
uninstall component 'file://component_validate_password';

7-2.ERROR 1290 (HY000): The MySQL server is running with the –secure-file-priv option so it cannot execute this statement

設定が正しいか、以下のコマンドで確認する。
select @@secure_file_priv;

8.その他

8-1.IPv6で起動するのをIPv4に変更

image.png
/etc/my.cnfの[mysqld]に「bind-address=0.0.0.0」を指定する。
https://dev.mysql.com/doc/refman/5.6/ja/ipv6-server-config.html

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?