Install MySQL Server0:00
Most Laravel applications need to communicate with a database to store, retrieve, and work on data. So in this lesson, we are going to cover how you may install MySQL 8 on your VBS and connect to it from the Laravel application and from your local machine. So let's get started. I am now inside the VBS as the ubuntu user. And what I'll do is install mysql-server through apt. So sudo apt install mysql-server. Verify the installation. And wait for the process to finish.
Enable Root Password0:37
Verify the installation. And wait for the process to finish. All good. And by default, the root user that comes with every new MySQL 8 installation doesn't allow password authentication. We will change that so we can run a special shell script that will take care of many of the installation steps for us. So let's log into mysql server by running sudo mysql. And let's alter the root user. So ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your_password';
And let's alter the root user. So alter user root at localhost identified with caching_sha2_password by and provide the password 12345678. I'm using a weak password just for the sake of this tutorial. This caching_sha2_password plugin is only available in MySQL 8. Older Laravel applications may not work with this plugin. So make sure your application runs on Laravel 8 or higher. Now let's exit the MySQL session and clear the terminal. Then we run the shell script. So sudo mysql_secure_installation.
Run Secure Installation2:34
Then MySQL asks if we want to remove the anonymous user. This user is created by default so anyone with access to the VBS can access MySQL easily for testing stuff and looking around. We will tell the script to remove this user. And next, we are asked if we want to prevent connections as the root user from outside our server. We will answer with yes. Now we are asked if we want to remove the test database that MySQL creates by default. And we are going to answer yes. And finally, the script asks if we want to reload the privileges on the MySQL server.
Create Database and User3:11
And we are going to answer yes. And finally, the script asks if we want to reload the privileges on the MySQL server. We will answer with yes so our changes reflect. And all done. Now that we have MySQL installed, let's create a database for our Laracast application and create a MySQL user that has access to this database. Let's then connect to the MySQL server as root, so mysql -u root -p, and provide the -p option to enter a password, then enter the password. And we're in. Let's now create the database.
And we're in. Let's now create the database. Create database laracasts, set the character set to utf8, and the collation to utf8_unicode_ci. Now let's create a MySQL user dedicated to the laracasts application, create user laracasts at localhost, identified by, and provide a password, 12345678. Then we grant this user access to a database called laracasts. So grant all privileges on laracasts database, the asterisk here means all tables of that database, to laracasts at localhost. Now we flush the privileges to clear the MySQL server cache, and exit MySQL. Next step for us would be installing the MySQL php extension so our Laravel application can
Configure Laravel Database4:50
Now we flush the privileges to clear the MySQL server cache, and exit MySQL. Next step for us would be installing the MySQL php extension so our Laravel application can connect to the MySQL database. So let's run sudo apt install php8.1 mysql. So again, sudo apt install php8.1 mysql. And all done. Let's go to the home directory, switch to the laracasts system user, get into the laracasts directory, and edit the .env file, oops, we are in the laracasts directory, so let's go into the www directory, open the .env file, and update the database credentials. The username would be laracasts, and the password would be 12345678.
into the www directory, open the .env file, and update the database credentials. The username would be laracasts, and the password would be 12345678. Let's save the changes, and run php artisan migrate. And here we go, the migration is complete. That means the Laravel application was able to connect to the MySQL server and execute the SQL queries needed to build the database schema. This .env file indicates that it is connecting using a MySQL user named laracasts. For each app we are going to create on this server, we should create a dedicated MySQL user. That way, if the app was compromised and the database credentials were leaked, only the
Connect from Local Machine6:33
user. That way, if the app was compromised and the database credentials were leaked, only the app database will be impacted, because the MySQL user has access to only this database. Let's go connect to our database from our machine, and inspect the schema. I'm here using SQL Ace, but you can select any software that you want. I'll select the SSH connection option, provide the MySQL host, localhost, username laracasts, that's the MySQL user, and provide the password, for the database it's laracasts, then for the SSH host, I'll go copy it from the AWS console, and paste it. For the user, I'll use Ubuntu. And for the SSH password, I'll select the identity file since we connect to the VBS.
With this setup, we have all Laravel applications run under a dedicated system user and use a dedicated MySQL user to access the database. The root MySQL user is protected by a password, and connections to it from outside the server are blocked. This setup gives us the security we need to run production level applications. That's it for this lesson. I'll see you in the next one.
