ホーム > Laravel > How to Connect Laravel to a Database (Using MAMP)
Laravel

How to Connect Laravel to a Database (Using MAMP)

Thank you for your continued support.
This article contains advertisements that help fund our operations.

Connecting to a Database in Laravel (MAMP)

I wrote an article about how to connect to a database in Laravel. Although the article uses a MAMP database, the configuration in Laravel is the same for other databases, so it should be helpful.

⇨ Click here for the table of contents for Laravel articles

I have written an article about how to connect to a database in Laravel. Although the article uses a MAMP database, the configuration in Laravel is the same for other databases, so it should be helpful.

Also, if the database connection is not working properly,

SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

Such errors are expected to occur.

Premise

MacOS Big Sur

Laravel 5.5 ~ 8

Laravel environment is set up.

MAMP is used to create the database environment.

If you haven't set up your environment yet, please read this article.

⇨ Relatively easy way to set up Laravel and PHP 7.4

How to Connect to the Database

Edit only the .env file

In the Laravel project directory, there is a file named .env.

※If you have it open in VScode, you can view it without changing any settings. However, files starting with "." may be treated as hidden files and not visible to some people.

In that case,

ls -a

command to check or display hidden files in Finder to check.

Personally, I recommend opening the project in VScode.

When you open the .env file, you will see the following text around line 10.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3336
DB_DATABASE=
DB_USERNAME=root
DB_PASSWORD=

Editing these 6 lines in Laravel is enough.

How to edit it? ⇒ Start MAMP and check

So let's start MAMP and check.

MAMP opens a screen like the image below when you start it.

There is roughly information written on the MySQL tab.

There, Host, Port, Username, and Password are written.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=
DB_USERNAME=root
DB_PASSWORD=root

In my case, the settings became as shown in the image.

The DB_HOST field can be 127.0.0.1 or localhost in my environment, so I left it as is.

Then, the only remaining field is the DB_DATABASE field.

Open phpmyadmin.

Create a new database.

This time, we are creating a database named test.

So the remaining field

The DB_DATABASE field should be

DB_DATABASE=test

and that's it.

Final form

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root

Confirming the Connection

If you run the migration and tables are created in the database, you are good to go.

Related articles

⇨ Creating a Database in Laravel (Migration)

Since there are already some migration files in the Laravel project, there is no need to do any additional work.

If you run the command twice and it works, you are successful.

php artisan config:clear
php artisan migrate

If you run this command, you should see the following result:

ap_laravel % php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (73.96ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (44.82ms)

If you see this output, the connection is successful.

Testing with an Error

If the connection fails, you may get the following error

php artisan migrate

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [1049] Unknown database 'aaaa' (SQL: select * from information_schema.tables where table_schema = aaaa and table_name = migrations and table_type = 'BASE TABLE')

This error indicates that the database does not exist, so check the database name.

DB_DATABASE=aaaa

was the incorrect database name in this case.

Conclusion

I wrote an article focusing on the database connection, which is a common point where people often get stuck at first.

I remembered that I had written similar articles while writing this, so I apologize for that.

If you have any questions or feedback, please contact me via Twitter DM.

I would be happy if you share this article.

Popular articles

Deploying a PHP7.4 + Laravel6 project on AWS EC2

Implementing Breadcrumbs in Laravel with laravel-breadcrumbs

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!