LoginSignup
8
10

More than 5 years have passed since last update.

【頻出】個人的に困った時の MySQL trouble shooting

Last updated at Posted at 2015-10-16

ユーザ作りたい&権限付与したい

CREATE USER #{user_name} IDENTIFIED BY '#{password}'; 
-- or UPDATE mysql.user SET Password=PASSWORD('#{password}') WHERE User='#{user_name}';
GRANT ALL PRIVILEGES ON #{database_name or *}.#{table_name or *} TO #{user_name};
-- FLUSH PRIVILEGES; -- GRANT 時は不要

(レア)ユーザ作ったのに入れねぇんだけど(怒)

$ mysql -u hoge -p # さっき作ったユーザ hoge
Enter password: 
ERROR 1045 (28000): Access denied for user 'hoge'@'localhost' (using password: YES)
  1. 権限周りの確認
  2. 権限も正しい場合は、ワイルドカードユーザがいる可能性大
-- root でログイン後
SELECT * FROM mysql.user;
+-----------+--------------+-------------------------------------------+
| Host      | User         | Password                                  |
| localhost | root         | *81Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| localhost |              |                                           | -- ←コイツ 
| %         | hoge         | *46xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-----------+--------------+-------------------------------------------+

どうやら、先にこっちがひっかかって hoge ユーザまで辿りつけてないみたい。
ユーザを消して上げればOK。

-- root でログイン後
DROP USER ''@localhost;

MySQL 立ち上がらない\(^o^)/

よく見る光景↓

$ mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

その場合は、
1. MySQL が立ち上がってるか確認
2. 立ち上がってるのに出る場合は、/tmp/mysql.sock に symlink 貼ってやる↓

$ sudo ln -s /path/to/socket /tmp/mysql.sock

ユーザ一覧見たいんですけど

SELECT * FROM mysql.user;

ページャきれいにしたい

pager less -SinFX;

ポート何番やねん

SHOW variables like 'port';

dump でバックアップしたい

$ mysqldump -h $HOSTNAME -u $USER_NAME -p $DBNAME > mysqldump_`date +%Y%m%d`.sql  
# $DBNAME の前に -B を付けると "CREATE DATABSE ..." も出力される&複数DB指定可

my.cnf 何処の読み込んでる?

$ mysql --help | grep my.cnf
()
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
8
10
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
8
10