- 投稿日:2021-02-25T23:50:21+09:00
【メモ】Docker環境構築 マイグレーション時のエラーとmysql8.0でのエラー
マイグレーション時のエラー
root@**********:/work# php artisan migrate Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES) (SQL: select * from information_schema.tables where table_schema = laravel_local and table_name = migrations and table_type = 'BASE TABLE') at /work/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669 665| // If an exception occurs when attempting to run a query, we'll format the error 666| // message to include the bindings with SQL, which will make this exception a 667| // lot more helpful to the developer instead of just the database's errors. 668| catch (Exception $e) { > 669| throw new QueryException( 670| $query, $this->prepareBindings($bindings), $e 671| ); 672| } 673| Exception trace: 1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES)") /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 2 PDO::__construct("mysql:host=db;port=3306;dbname=laravel_local", "ogge", "secret", []) /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details..env
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel_local DB_USERNAME=***** DB_PASSWORD=*****.env.example
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel_local DB_USERNAME=***** DB_PASSWORD=*****database.php'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', 'db'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel_local'), 追記 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''),以上3つを修正してマイグレーション
root@:*********:/work# php artisan migrate Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.14 seconds) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (0.06 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.03 seconds)成功だね!!!
その後コンテナに入ってmysqlを立ち上げ?
C:\Users\****\*****\****>docker-compose exec db bash root@*******:/# mysql -u ***** -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.どんなユーザーがあるか確認したところエラー
mysql> select user, host from mysql.user; ERROR 1142 (42000): SELECT command denied to user '****'@'localhost' for table 'user'rootで入り直して
root@*******:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.どのユーザーがいるか確認。
hostに%が付いていると,User項目が使えなくなる為,さっきの場合だとコマンドが受け付けてくれなかった。
因みに%はワイルドカードと言うらしく,どんなホストでもサーバーに繋げれるらしい。
※間違ってたらごめんね。【参照】https://rfs.jp/server/mysql/m02/04-11.html
mysql> select host,user from mysql.user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | ***** | | % | root | | localhost | mysql.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 6 rows in set (0.01 sec)権限の確認を行って
mysql> SHOW GRANTS for '*****'@'%'; +-----------------------------------------------------------+ | Grants for *****@% | +-----------------------------------------------------------+ | GRANT USAGE ON *.* TO `*****`@`%` | | GRANT ALL PRIVILEGES ON `laravel\_local`.* TO `*****`@`%` | +-----------------------------------------------------------+ 2 rows in set (0.01 sec)終了!!
- 投稿日:2021-02-25T23:50:21+09:00
【メモ】Laravel Docker環境構築 マイグレーション時のエラーとmysql8.0でのエラー
マイグレーション時のエラー
root@**********:/work# php artisan migrate Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES) (SQL: select * from information_schema.tables where table_schema = laravel_local and table_name = migrations and table_type = 'BASE TABLE') at /work/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669 665| // If an exception occurs when attempting to run a query, we'll format the error 666| // message to include the bindings with SQL, which will make this exception a 667| // lot more helpful to the developer instead of just the database's errors. 668| catch (Exception $e) { > 669| throw new QueryException( 670| $query, $this->prepareBindings($bindings), $e 671| ); 672| } 673| Exception trace: 1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES)") /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 2 PDO::__construct("mysql:host=db;port=3306;dbname=laravel_local", "ogge", "secret", []) /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details..env
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel_local DB_USERNAME=***** DB_PASSWORD=*****.env.example
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel_local DB_USERNAME=***** DB_PASSWORD=*****database.php'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', 'db'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel_local'), 追記 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''),以上3つを修正してマイグレーション
root@:*********:/work# php artisan migrate Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.14 seconds) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (0.06 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.03 seconds)成功だね!!!
その後コンテナに入ってmysqlを立ち上げ?
C:\Users\****\*****\****>docker-compose exec db bash root@*******:/# mysql -u ***** -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.どんなユーザーがあるか確認したところエラー
mysql> select user, host from mysql.user; ERROR 1142 (42000): SELECT command denied to user '****'@'localhost' for table 'user'rootで入り直して
root@*******:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.どのユーザーがいるか確認。
hostに%が付いていると,User項目が使えなくなる為,さっきの場合だとコマンドが受け付けてくれなかった。
因みに%はワイルドカードと言うらしく,どんなホストでもサーバーに繋げれるらしい。
※間違ってたらごめんね。【参照】https://rfs.jp/server/mysql/m02/04-11.html
mysql> select host,user from mysql.user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | ***** | | % | root | | localhost | mysql.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 6 rows in set (0.01 sec)権限の確認を行って
mysql> SHOW GRANTS for '*****'@'%'; +-----------------------------------------------------------+ | Grants for *****@% | +-----------------------------------------------------------+ | GRANT USAGE ON *.* TO `*****`@`%` | | GRANT ALL PRIVILEGES ON `laravel\_local`.* TO `*****`@`%` | +-----------------------------------------------------------+ 2 rows in set (0.01 sec)終了!!
- 投稿日:2021-02-25T17:05:05+09:00
PHPでMySQLの接続確認をする
MySQLのクライアントが入っていないサーバーでMySQLアカウントの確認をする方法です。
ssh 対象のサーバ vi test.php ------------------------------------------------------------------------------------------------- <?php define('HOSTNAME', 'hoge01'); define('DATABASE', 'db1'); define('USERNAME', 'test'); define('PASSWORD', 'hogehoge'); try { $db = new PDO('mysql:host=' . HOSTNAME . ';dbname=' . DATABASE, USERNAME, PASSWORD); echo "OK\n"; } catch (PDOException $e) { echo "NG\n"; echo $e->getMessage()."\n"; } ------------------------------------------------------------------------------------------------- php test.php
- 投稿日:2021-02-25T16:21:41+09:00
Laravel DBレコードの存在判定
LaravelのクエリビルダでDBレコードの存在チェック
記事内容
Laravelでオリジナルアプリのバックエンドを実装していく際に、DBにレコードが存在すればtrueを返し(ページ遷移)、レコードが存在しなければfalseを返す(エラーメッセージを表示する)という機能を実装したので、備忘録として残したいと思います。
開発環境
Laravel 8.22.1
PHP:8.0.1
MySQL:8.0.23
MacOS:11.1 ( Big Sur )
DBレコードの存在チェック
○○Controller.phpDB::table('table名')->where(column, $data)->exists();クエリビルダの基本的な書き方に、existsメソッドを繋げて記述することで簡単にレコードの存在を判定できます。
実際の記述
今回僕は、下記のように記述しました。
内容としては、ユーザーによるフォームからのリクエスト内容と一致するレコードが、存在するかどうかで条件分岐しています。
○○Controller.php$user = User::all()->where('id', $request->id)->first(); $msg = ['msg' => '入力されたIDは存在しません']; if (DB::table('users')->where('id', $request->id)->exists() && [$request->id === $users->id]) { return redirect()->route('home'); } else { return view('login', $msg);ifのカッコ内の記述により、DB内にレコードが存在していて且つ、フォームから送信された内容と一致する場合は、homeビューが返され、DBにレコードが存在しない場合は$msg変数の内容とともにloginビューが返される。
あとがき
今回のアプリを作っていく過程で、フォームからDBにレコードが存在しない値を入力された際、
「Attempt to read property "id" on null」というエラーにつまづき、なかなか前へ進めなかったので、その解決策を模索していたところ、本記事に記載した対処法に至りました。他にも解決策はあると思いますので、laravelの公式ドキュメントや他の記事を参考にして、引き続きアプリの実装をしていきたいと思います。
- 投稿日:2021-02-25T08:26:53+09:00
Flask + Vue + MySQL 構成のDocker環境構築方法 メモ
- Vue.js(フロントエンド)、Flask(バックエンド)、MySQL(DB)構成のアプリをDockerで環境構築したので、構築方法を備忘録としてメモしておく。
全体フォルダ構成
your_app --- fe --- app # プロジェクトフォルダ | L_ Dockerfile | L_ be --- app # プロジェクトフォルダ | L_ Dockerfile | L_ requirements.txt # 依存パッケージリスト L_ db --- data # DBデータマウント用フォルダ | L_ my.cnf # 設定ファイル L_ docker-compose.ymldocker-compose.yml
- フロントエンド
- 8080ポートを使用する。
- プロジェクトフォルダ
app
をバインドマウントする。node_modules
をボリュームマウントすることで、ビルド速度低下を防ぐ。- バックエンド
- 5000ポートを使用する。
- プロジェクトフォルダ
app
をバインドマウントする。- データベース
- MySQL 5.7イメージを使用する。
- 3307ポートを使用する。
command
でポートを明示的に指定する必要あり。data
フォルダをバインドマウントする。version: "3" services: # フロントエンド fe: container_name: fe build: ./fe volumes: - ./fe/app:/app - node_modules_volume:/app/node_modules ports: - "8080:8080" links: - be # バックエンド be: container_name: be build: ./be volumes: - ./be/app:/app ports: - "5000:5000" command: flask run --host 0.0.0.0 --port 5000 links: - db # データベース db: image: mysql:5.7 container_name: db environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_DATABASE: sample_db MYSQL_USER: mysqluser MYSQL_PASSWORD: mysqlpass volumes: - ./db/data:/var/lib/mysql - ./db/my.cnf:/etc/mysql/conf.d/my.cnf ports: - 3307:3307 command: --port 3307 volumes: node_modules_volume:フロントエンド
node.jsのベースイメージからフロントエンドアプリ(Vue.js)用コンテナを作成する。
Dockerfile
# ベースイメージ FROM node:13.10.1-alpine3.11 WORKDIR /app # 依存関係のインストール COPY ./app/package*.json ./ RUN npm install # プロジェクトフォルダをカレントディレクトリ(appフォルダ)にコピー COPY ./app . EXPOSE 8080 CMD ["npm", "run", "serve"]
app
フォルダ
vue create app
コマンドで作成したプロジェクト一式を配置する。バックエンド
- python 3.7ベースイメージからバックエンドアプリ(Flask)用コンテナを作成する。
Dockerfile
FROM python:3.7 RUN mkdir /app ADD requirements.txt /app ENV PYTHONUNBUFFERED 1 EXPOSE 5000 WORKDIR /app RUN pip3 install -r requirements.txt
requirements.txt
- Flask/MySQL用パッケージを記述する。
SQLAlchemy Flask-SQLAlchemy Flask-Cors marshmallow marshmallow-sqlalchemy flask-marshmallow pymysql
app
フォルダ
- Flaskプロジェクトフォルダ一式を配置する。
データベース
コンテナ設定は、
docker-compose
のみに記述する。
data
フォルダ
- ホスト側マウント用フォルダ。
my.cnf
- 設定ファイル。
- 文字コードのみ指定する。
[mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci [client] default-character-set=utf8mb4参考情報