{"id":862,"date":"2025-07-10T09:41:01","date_gmt":"2025-07-10T13:41:01","guid":{"rendered":"https:\/\/waterloobae.com\/?p=862"},"modified":"2025-08-20T13:26:30","modified_gmt":"2025-08-20T17:26:30","slug":"laravel-initial-database-setup-and-debugging","status":"publish","type":"post","link":"https:\/\/waterloobae.com\/blog\/2025\/07\/10\/laravel-initial-database-setup-and-debugging\/","title":{"rendered":"[Laravel] Initial Database Setup and Debugging"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Initial Setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Database connection information is found in two files mainly. These values are created during application creation. However, it is often necessary to check and fix those values. Any sensitive values like credentials are supposed to go into <code>\/.env<\/code> file. It looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">DB_CONNECTION=mysql\nDB_HOST=mysql\nDB_PORT=3306\n\nDB_DATABASE=alpha\nDB_USERNAME=waterbae\nDB_PASSWORD=\"securepassword\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another file with database connection information is <code>\/config\/database.php<\/code>. It can have multiple configurations for many databases and it look like.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">       'mysql' => [\n            'driver' => 'mysql',\n            'url' => env('DB_URL'),\n            'host' => env('DB_HOST', '127.0.0.1'),\n            'port' => env('DB_PORT', '3306'),\n            'database' => env('DB_DATABASE', 'laravel'),\n            'username' => env('DB_USERNAME', 'root'),\n            'password' => env('DB_PASSWORD', ''),\n            ...\n            ]) : [],\n        ],\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">In case you are building Laravel application with existing table, make sure your migrations files, <code>\/database\/migrations\/*.php<\/code>  , for creating tables exists in migrations table without .php and batch field has &#8220;1&#8221; as value. <\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Debugging<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is not working! What do I do??? It is most common issue we encounter after setting the server up for the first time. It is time to talk to system admin and DBA, but there are some steps we can take before starting a war. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, check if your application server can talk to your db server using netcat. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">nc -vz db.server.com 3306<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the connection is rejected talk to your DBA and System admin. If it can be connected, you can try the next step by troubleshooting with Tinker by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php artisan tinker<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inside Tinker, you can double check if DB related environment variables and configurations are correct and run simple SQL to check.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">config('database.connections.mysql.host');\nDB::connection('mysql')->select('SELECT 1');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep double check with your setting and Tinker output to get desirable results for your server configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tip1: <a href=\"https:\/\/waterloobae.com\/2024\/08\/08\/laravel-starting-laravel-project-with-existing-db-schema\/\" data-type=\"link\" data-id=\"https:\/\/waterloobae.com\/2024\/08\/08\/laravel-starting-laravel-project-with-existing-db-schema\/\">If you are working with existing databases and using &#8220;kitloong\/laravel-migrations-generator&#8221;<\/a>, you may generate those migration files with batch value 1.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php artisan migrate:generate --connection=\"mydb2\"\nphp artisan migrate:generate --connection=\"mydb3\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Tip2: You may clear any configs and\/or cache to see the results of you changes right away.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">php artisan config:clear\nphp artisan cache:clear<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sail Configuration for multiple DB<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To use more than one DB with Sail, each DB docker containers must set to use each ports for your machine side. So, it will look like as below in <code>docker-compose.yml<\/code> file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">    mysql:\n        image: 'mysql\/mysql-server:8.0'\n        ports:\n            - '${FORWARD_DB_PORT:-3306}:3306'\n        ...\n        networks:\n            - sail\n        ...\n    mysql.db2:\n        image: 'mysql\/mysql-server:8.0'\n        ports:\n            - '${FORWARD_DB_PORT:-3307}:3306'\n        ...\n        networks:\n            - sail\n        ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, you need to set <code>\/config\/database.php<\/code> up to use port 3306 because that is the port Sail talk to inside your DB docker container. Do not get lost here \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Initial Setup Database connection information is found in two files mainly. These values are created during application creation. However, it is often necessary to check and fix those values. Any sensitive values like credentials are supposed to go into \/.env file. It looks like: Another file with database connection information is \/config\/database.php. It can have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,14],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-it-tips","category-laravel"],"_links":{"self":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/comments?post=862"}],"version-history":[{"count":7,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/862\/revisions\/870"}],"wp:attachment":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/media?parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/categories?post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/tags?post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}