Laravel MySQL Connection Explained with Basic Auth
Hi, Friends welcome to TechBlog. In this tutorial, we are going to config the MySQL Database connection with your Laravel Application. In continuation Laravel tutorial Series we had installed Laravel with an apache web server in the previous tutorial.
Prerequisites you needed to build Database connectivity with laravel application:
- Ubuntu with LAMP/LNMP
- Laravel 8.x
Major Steps of Laravel MySQL Connection:
- Step1-Install MySQL server and PHP MySQL module.
- Step2-Creating User and Database for your application
- Step3-Making auth functionality.
- Step4-Config your application with database information and test the connection.
Step1-Install MySQL server and PHP MySQL module.
You need a MySQL server and php-mysql module. Go ahead and execute following command
sudo apt install mysql-server, php-mysql
After installation completed check the mysql service running or not by using
service mysql status
Yea it’s running. If you found it’s not running then start the service.
sudo systemctl start mysql
Now run the MySQL secure installation script to improve the security of your database and remove unwanted database and users that comes with by default.
Here I am going to choose the medium strength password and set the password. Then re-enter the password again. Accept it. And follow the question and answer it.
Step2-Creating User and Database for your application.
Now move into MySQL database and create a database for your application. and create a user for your application, use the following command.
CREATE DATABASE techblog; CREATE USER 'techblog'@'localhost' IDENTIFIED BY '[email protected]'; GRANT ALL PRIVILEGES ON *.* TO 'techblog'@'localhost' ; FLUSH PRIVILEGES; EXIT;
Step3-Making auth functionality.
Move to your application directory. Here we are going to create basic authentication functionality like login and register users to show database connectivity.For that we required UI library just type following command:
cd /home/techblog/myapp1 composer require Laravel/ui
php artisan ui vue --auth
You can see that authentication scaffolding generated. Also, you need to run the npm install and run dev in order to make it fully functional with compiling all js and CSS files. Npm stands for Node package manager It puts modules in place so that node can find them, and manages dependency conflicts intelligently. If npm not installed in your system then first install npm:
sudo apt install npm
Just go ahead and type npm install. It will install all required dependency script automatically.
After that use npm run dev command to compile all js and css file for your application.
npm run dev
Step4-Config your application with database information and test the connection.
So, in order to do this, open your application directory with any code editor. I am using Visual Studio Code to edit my application. You can check out this tutorial how to install Visual studio code on ubuntu here. Open your application directory inside the code editor. And then edit the “.env” file inside your application. Find the DB connection section. And put your database name, user name, and credentials. And then save the file.
Get back to the terminal and try to migrate the tables which we created at the time of creating auth functionality. Migrate the tables to database using following command
php artisan migrate
Test Laravel MySQL Connection
Now you can see the reflection on your application. Get back to the browser reload the page and here it is, log in and register functionality added to your application. Well, now you can able register yourself inside your new application just fill this form and click on the register button.
Latest posts by Subroto Mondal (see all)
- How to Install WordPress with Nginx on Ubuntu 22.04 - August 12, 2022
- Install React JS on Ubuntu 22.04 LTS - August 1, 2022
- How to Install Django on Ubuntu 22.04 LTS - May 28, 2022