0
0

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.

phpでmysqlに接続する時にアホハマりした話

Last updated at Posted at 2021-04-06

もうイヤほどやってるWordpressの構築にまたハマった。

正しい設定してるのにエラーが出るのである。

Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'wp_yakitori_user'@'localhost'
wp_config.php
define( 'DB_NAME', "wp_yakitori_db" );
define( 'DB_USER', "wp_yakitori_user" );
define( 'DB_PASSWORD', "s2wC4hPft$c" );

なぜ!?

問題の切り分け

apache?MySQL?php?SElinux?

こういうときは大体SELinux

getenforce 
Disabled

違った

MySQLのパスワード間違えてる疑惑

mysql -u wp_yakitori_user -p
Enter password: (s2wC4hPft$c)
Welcome to the MySQL monitor.  Commands end with ; or \g.

合ってる

localhostへのアクセスが禁止されている疑惑

mysql.user
SELECT host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
| localhost | wp_yakitori_user |
+-----------+------------------+

設定してある...

phpで繋げない疑惑

とりあえずphpをコマンドライン上で実行してみる

root@htdocs php -r '$dbh = new PDO("mysql:host=localhost;dbname=wp_yakitori_db;charset=utf8mb4", "root", "9XIJK39MGCG");'
root@htdocs

いける

root@htdocs php -r '$dbh = new PDO("mysql:host=localhost;dbname=wp_yakitori_db;charset=utf8mb4", "wp_yakitori_user", "s2wC4hPft$c");'
root@htdocs PHP Notice:  Undefined variable: c in Command line code on line 1
PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'wp_skrmc_user'@'localhost' (using password: YES) in Command line code:1
Stack trace:
# 0 Command line code(1): PDO->__construct('mysql:host=loca...', 'wp_yakitori_user', 's2wC4hPft')
# 1 {main}
  thrown in Command line code on line 1

wp_yakitori_userだとAccess deniedエラー、何故?
Undefined variable cって何?
というか何かコンストラクタがおかしい

よく見たら第三引数が変なところで切れている
ドルマークが悪さしているようである

なんとなく見えてきたがパスワードを変更する

set password for 'wp_yakitori_user'@'localhost' = 's2wC4hPft&c';

確認

root@htdocs php -r '$dbh = new PDO("mysql:host=localhost;dbname=wp_yakitori_db;charset=utf8mb4", "wp_yakitori_user", "s2wC4hPft&c");'
root@htdocs

いけた

そもそもphpで文字列に$マーク使うの注意しろよって話

おわり。

0
0
3

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?