質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

2回答

3556閲覧

【docker+wordpress,+mysql 】localhostにアクセスすると「Error establishing a database connection」のエラー

pecchan

総合スコア555

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2021/11/23 23:02

編集2021/11/24 00:13

docker勉強中の者です。

質問は、dockerというよりmysqlに関する事かもしれません。

Docker入門(第一回)~Dockerとは何か、何が良いのか~
上記を参考に、wordpress起動までを確認しようとしてます。

以下のコマンドにてコンテナを作成、起動しました。

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7 docker run --name some-wordpress -e WORDPRESS_DB_PASSWORD=my-secret-pw --link some-mysql:mysql -d -p 8080:80 wordpress

そして
http://localhost:8080にアクセスするとブラウザに「Error establishing a database connection」とエラーメッセージが表示されました。

###試したこと
まずログを確認しました。

docker コンテナID logs

上記コマンドにてwp,mysql両方のログを確認しました。

するとmysqlの方でアクセス権限のエラーが出てました。

2021-11-23T22:39:27.414390Z 4 [Note] Access denied for user 'example username'@'172.17.0.3' (using password: YES)

次にmysqlにuserを確認してみました。

mysql> SELECT Host, User FROM mysql.user; +-----------+---------------+ | Host | User | +-----------+---------------+ | % | root | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 4 rows in set (0.00 sec)

ここまで確認しましたが、何が必要なのか分からずにいます。

参考先のチュートリアルには、ログインユーザを作成するような記載はありませんでしたが、何か必要なのでしょうか?

すいませんが分かる方教えていただけると幸いです。
宜しくお願い致します。

###追記

追記です。

【開発環境構築】DockerでWordpressの開発環境構築をして「Error establishing a database connection」で繋がらないときはwp-config.phpを確認!
上記によると、wp-config.php ファイルを編集する必要がありそうです。
dockerからこのファイルを編集する方法を調べてますがまだ見当たりません。

dockerから実ファイルを探す方法も分からずそこから調べている状況です。

###追記

dockerから実ファイルにアクセスする方法分かりました。
wp-config.phpと、 wp-config-docker.phpというファイルが見つかりました。

試しにこれを編集してみようと思います。

wp-config.php

php

1<?php 2/** 3 * The base configuration for WordPress 4 * 5 * The wp-config.php creation script uses this file during the installation. 6 * You don't have to use the web site, you can copy this file to "wp-config.php" 7 * and fill in the values. 8 * 9 * This file contains the following configurations: 10 * 11 * * MySQL settings 12 * * Secret keys 13 * * Database table prefix 14 * * ABSPATH 15 * 16 * This has been slightly modified (to read environment variables) for use in Docker. 17 * 18 * @link https://wordpress.org/support/article/editing-wp-config-php/ 19 * 20 * @package WordPress 21 */ 22 23// IMPORTANT: this file needs to stay in-sync with https://github.com/WordPress/WordPress/blob/master/wp-config-sample.php 24// (it gets parsed by the upstream wizard in https://github.com/WordPress/WordPress/blob/f27cb65e1ef25d11b535695a660e7282b98eb742/wp-admin/setup-config.php#L356-L392) 25 26// a helper function to lookup "env_FILE", "env", then fallback 27if (!function_exists('getenv_docker')) { 28 // https://github.com/docker-library/wordpress/issues/588 (WP-CLI will load this file 2x) 29 function getenv_docker($env, $default) { 30 if ($fileEnv = getenv($env . '_FILE')) { 31 return rtrim(file_get_contents($fileEnv), "\r\n"); 32 } 33 else if (($val = getenv($env)) !== false) { 34 return $val; 35 } 36 else { 37 return $default; 38 } 39 } 40} 41 42// ** MySQL settings - You can get this info from your web host ** // 43/** The name of the database for WordPress */ 44define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') ); 45 46/** MySQL database username */ 47define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'example username') ); 48 49/** MySQL database password */ 50define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'example password') ); 51 52/** 53 * Docker image fallback values above are sourced from the official WordPress installation wizard: 54 * https://github.com/WordPress/WordPress/blob/f9cc35ebad82753e9c86de322ea5c76a9001c7e2/wp-admin/setup-config.php#L216-L230 55 * (However, using "example username" and "example password" in your database is strongly discouraged. Please use strong, random credentials!) 56 */ 57 58/** MySQL hostname */ 59define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') ); 60 61/** Database charset to use in creating database tables. */ 62define( 'DB_CHARSET', getenv_docker('WORDPRESS_DB_CHARSET', 'utf8') ); 63 64/** The database collate type. Don't change this if in doubt. */ 65define( 'DB_COLLATE', getenv_docker('WORDPRESS_DB_COLLATE', '') ); 66 67/**#@+ 68 * Authentication unique keys and salts. 69 * 70 * Change these to different unique phrases! You can generate these using 71 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}. 72 * 73 * You can change these at any point in time to invalidate all existing cookies. 74 * This will force all users to have to log in again. 75 * 76 * @since 2.6.0 77 */ 78define( 'AUTH_KEY', getenv_docker('WORDPRESS_AUTH_KEY', '044ecaa71e6317408882a5431804edfa001074e1') ); 79define( 'SECURE_AUTH_KEY', getenv_docker('WORDPRESS_SECURE_AUTH_KEY', '2b7a91d10294784fb57b7e1620d764170d11557e') ); 80define( 'LOGGED_IN_KEY', getenv_docker('WORDPRESS_LOGGED_IN_KEY', '93490a2477f86c7d6ab8a23c3f1ac3f20f75cd6e') ); 81define( 'NONCE_KEY', getenv_docker('WORDPRESS_NONCE_KEY', 'ef24dff849eaea2b6ca8bf43a3edc1cb70c8d7cf') ); 82define( 'AUTH_SALT', getenv_docker('WORDPRESS_AUTH_SALT', '73f41d4cf8231be0ea8f6b6107af7e2e58fd413c') ); 83define( 'SECURE_AUTH_SALT', getenv_docker('WORDPRESS_SECURE_AUTH_SALT', '59bd4b20cd75bae02700a26acfcd7fde88be7a6d') ); 84define( 'LOGGED_IN_SALT', getenv_docker('WORDPRESS_LOGGED_IN_SALT', 'f4878dc7342eb0bdd7d7899c21cb2070e7262081') ); 85define( 'NONCE_SALT', getenv_docker('WORDPRESS_NONCE_SALT', 'a3f043707b4ea89e42afb1c8103257195c5c570f') ); 86// (See also https://wordpress.stackexchange.com/a/152905/199287) 87 88/**#@-*/ 89 90/** 91 * WordPress database table prefix. 92 * 93 * You can have multiple installations in one database if you give each 94 * a unique prefix. Only numbers, letters, and underscores please! 95 */ 96$table_prefix = getenv_docker('WORDPRESS_TABLE_PREFIX', 'wp_'); 97 98/** 99 * For developers: WordPress debugging mode. 100 * 101 * Change this to true to enable the display of notices during development. 102 * It is strongly recommended that plugin and theme developers use WP_DEBUG 103 * in their development environments. 104 * 105 * For information on other constants that can be used for debugging, 106 * visit the documentation. 107 * 108 * @link https://wordpress.org/support/article/debugging-in-wordpress/ 109 */ 110define( 'WP_DEBUG', !!getenv_docker('WORDPRESS_DEBUG', '') ); 111 112/* Add any custom values between this line and the "stop editing" line. */ 113 114// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact 115// see also https://wordpress.org/support/article/administration-over-ssl/#using-a-reverse-proxy 116if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) { 117 $_SERVER['HTTPS'] = 'on'; 118} 119// (we include this by default because reverse proxying is extremely common in container environments) 120 121if ($configExtra = getenv_docker('WORDPRESS_CONFIG_EXTRA', '')) { 122 eval($configExtra); 123} 124 125/* That's all, stop editing! Happy publishing. */ 126 127/** Absolute path to the WordPress directory. */ 128if ( ! defined( 'ABSPATH' ) ) { 129 define( 'ABSPATH', __DIR__ . '/' ); 130} 131 132/** Sets up WordPress vars and included files. */ 133require_once ABSPATH . 'wp-settings.php'; 134 135

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

2021-11-23T22:39:27.414390Z 4 [Note] Access denied for user 'example username'@'172.17.0.3' (using password: YES)

これはMySQLイメージは環境変数でユーザ名を指定しない場合、デフォルトでrootユーザのみが作成されますが、
WordPressの方ではユーザ名の指定をしていない為に、不一致となっていることが原因です。

また、MySQLの方に初期作成するDBの指定もありませんので、このままだとWordPress用のDBを別途作成しなければなりません。


WordPressやMySQLのイメージは直接ファイル操作しなくとも、
Dockerらしい使い方として環境変数だけである程度の設定は扱えるようになっています。

それぞれのコンテナに対して設定すべき項目は、DockerHubのWordPressドキュメントページに一通り載っております。
こちらを参考に環境変数を設定してみてください。

Wordpress - Official Image | Docker Hub

その他のMySQLの設定についても色々載っていますので、ぜひご参考ください。

Mysql - Official Image | Docker Hub

投稿2021/11/24 09:12

編集2021/11/24 09:15
surface_0

総合スコア497

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pecchan

2021/11/25 04:49

有難う御座います。 なるほど、理解できました。 私がやろうとした直接のファイル操作も一般的でなく、このような場合docker composeを使うという所まで行き着きました。 しかしながら、参考にしたサイト(さくらのクラウド)では、このコマンドだけで繋がるように記載してあるのは、何故なんでしょうね・・・(T_T)
surface_0

2021/11/25 06:03

複数のコンテナを連携させて管理する場合はdocker composeを使うと一つのプロジェクトとして扱えるので便利です。 もちろんそれを使わなくても、dockerコマンドだけでもコンテナの連携はできます。 > しかしながら、参考にしたサイト(さくらのクラウド)では、このコマンドだけで繋がるように記載してあるのは、何故なんでしょうね・・・(T_T) 3年以上も前の記事なのでWordPressのイメージが現在とは仕様が違っていた可能性もありますね。
pecchan

2021/11/25 18:24

有難う御座います。大変参考になりました。
guest

0

Access denied for user 'example username'@'172.17.0.3' (using password: YES)

もしかして「example username」と指定してログインしようとしているのではないですか?
mysql.userテーブルに登録されているユーザー(rootなど)を指定してください。

投稿2021/11/24 01:32

編集2021/11/24 01:44
technocore

総合スコア7200

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pecchan

2021/11/25 04:46

有難う御座います rootに変えてもログインできず色々調べておりました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問