Warning: You are reading an outdated version of this guide. Unless you're here for a specific reason, you probably want the updated version instead.
This article covers the installation and basic set up a database management system.
I’ve included sections on three common DBMSs—use whichever you prefer out of:
If you’re using MySQL, you might also like Digital Ocean’s guide to setting up MySQL on an Ubuntu machine, which covers similar material.
First install MySQL. (Technically we’re installing MariaDB, which is a drop-in, open source replacement for MySQL. The two are interchangeable for our purposes).
# Install MariaDB (MariaDB is an open source, drop-in MySQL replacement).
# It might be preinstalled on your server, but it can't hurt to reinstall.
laravel@server$ sudo apt install mariadb-server
You then need to create a database to store your web app’s data and a database user to interact with the database. To do this, first log in to the MySQL shell using the root DB account:
# Log in to the MySQL shell using the root MySQL account
laravel@server$ sudo mysql
Then create a dedicated database and DB account for your web app.
I’m showing only a basic setup in this guide, encompassing:
caching_sha2_password
at the time of writing).GRANT ALL PRIVILEGES ON laraveldb.*
) and not granting privilege for all databases on the server (which would be GRANT ALL PRIVILEGES ON *.*
).This is all you need to run a Laravel web app and should work well for most users, but keep in mind that there are many more possible authentication methods and privilege grants to choose from. Consider reading through these two Digital Ocean guides if you’re new to managing MySQL databases.
-- Create a database for your web app.
-- (Update the database name as desired.)
CREATE DATABASE laraveldb;
-- Create a dedicated DB account to manage the app's DB.
-- (Update the account's username and password as desired.)
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'supersecretpassword';
-- Grant the DB user all privileges on the app's database
GRANT ALL PRIVILEGES ON laraveldb.* TO 'laravel'@'localhost';
mysql_secure_installation
TLDR: read the warning about mysql_secure_installation
in this Digital Ocean article, perform the suggested temporary change of root@localhost
’s authentication method to mysql_native_password
, then run:
# ASSUMING you've allowed password authentication for the root account, just
# follow the prompts and use common sense.
laravel@server$ sudo mysql_secure_installation
End TLDR
It’s best practice to secure your MySQL install using the mysql_secure_installation
tool.
But at the time of writing, this is complicated by a clash between mysql_secure_installation
and Ubuntu’s default MySQL settings: namely, mysql_secure_installation
attempts to set a password for the root database account, but by default this account is only allowed to authenticate using Unix socket authentication (i.e. MySQL’s auth_socket
method).
You can get around this by tweaking some MySQL config settings, but it takes manual intervention on your part. Digital Ocean has already covered this, so I refer to their guide (here you go) and see no reason to repeat the same information here.
Finally, enable and start the MySQL server:
# Enable and start the MySQL server
laravel@server$ sudo systemctl enable --now mariadb.service
You should be able to log in to a MySQL shell:
# Log in to MySQL shell; connect to just-created database with just-created account.
# Specify password when prompted.
laravel@server$ mysql -D laraveldb -u laravel -p
First install PostgreSQL:
# Install PostgreSQL
laravel@server$ sudo apt install postgresql
You then need to create a database to store your web app’s data and a database user to interact with the database. To do this, first log in to the PostgreSQL shell:
# Log in to the PostgreSQL shell using the `postgres` PostgreSQL account.
# We do this by first switching to the `postgres` Linux user,
# then running the `psql` command as the `postgres` Linux user
laravel@server$ sudo --login --user=postgres
postgres@server$ psql
# Note: you could also do that in one shot with:
laravel@server$ sudo -u postgres psql
postgres
user
Logging in to PostgreSQL can (understandably) be a bit confusing for new users.
Here’s everything involved in a PostgreSQL connection:
(Most of these are set implicitly, so beginner users are not aware of them—this causes confusion upon encountering problems that require knowing what’s going on under the hood.)
First some background: the PostgreSQL installation, among other things, creates:
postgres
(you can confirm this by checking the bottom of the /etc/passwd
file, which keeps track of Linux users, after installing PostgreSQL).postgres
.postgres
.psql
binary, often in /usr/bin/psql
.When you run psql
without explicitly specifying a database, PostgreSQL user, host, or port, psql
will try to log you into the PostgreSQL shell:
psql
command (using peer-based authentication, which relies precisely on the Linux user’s and PostgreSQL user’s names matching),psql
command,local
connection type in the pg_hba.conf
file) on the local host.(The last two bullet points fall beyond the scope of this tutorial and require some networking background, so don’t worry if you don’t understand them.)
My goal here is mainly for you to understand why we switch to the postgres
Linux user before running psql
.
Well, our goal is to log in to the PostgreSQL shell as the postgres
PostgreSQL user, connected to the default postgres
database.
We’ve mentioned that, by default, psql
will connect you to the database and PostgreSQL account matching the name of the Linux user who ran the psql
command, so running psql
as the postgres
Linux user ensures we connect to the postgres
database as the postgres
PostgreSQL user.
(And why connect with the postgres
PostgreSQL account in the first place? Answer: postgres
is the PostgreSQL superuser account (like the root
Linux account) that is used to create new databases and other users.)
psql
connection info by running \conninfo
after connecting to a PostgreSQL shell.psql
’s -U
option lets you log in to the PostgreSQL shell as a custom user (but will need appropriate permissions/authentication).psql
’s -d
option lets you connect to a custom databasepsql
’s -h
option lets you connect to a specific host, and can be used to attempt a host-based connection.pg_hba.conf
file nicely summarize practical use of various authentication methods and connection types (you can locate the file by running SHOW hba_file;
as a superuser from a PostgreSQL shell).
(The pg_hba.conf
file is where you configure PostgreSQL authentication, but you should know what you’re doing before editing it.)Then create a dedicated database and DB account for your web app (remember the semicolons—the PostgreSQL shell is picky).
-- Create a database for your web app.
-- (Update the database name as desired.)
postgres=# CREATE DATABASE laraveldb;
-- Create a dedicated DB account to manage the app's DB.
-- (Update the account's username and password as desired.)
postgres=# CREATE USER laravel WITH ENCRYPTED PASSWORD 'supersecretpassword';
-- A simple way of granting the DB user privileges on the app's database---we
-- just make the user the owner of the database.
postgres=# ALTER DATABASE laraveldb OWNER TO laravel;
I’m showing only a basic setup in this guide, encompassing:
-h
flag when using psql
.This is all you need to run a Laravel web app and should work well for most users, but keep in mind that there are many more possible authentication methods and privilege grants to choose from. If inspired consider reading through the PostgreSQL docs on client authentication and available privileges, as well as this StackOverflow answer and the references therein.
Finally, enable and start the PostgreSQL server:
# Enable and start the PostgreSQL server
laravel@server$ sudo systemctl enable --now postgresql.service
You should then be able to log in to a PostgreSQL shell as follows:
# Log in to PostgreSQL shell and connect to just-created DB.
# IMPORTANT: you need the `-h` flag to initiate a host-based connection.
# Specify password when prompted.
laravel@server$ psql -U laravel -d laraveldb -h localhost
--host
flag!
-h
flag (short for --host
) to initiate a local host-based connection;
psql
would otherwise attempt a socket-based connection using peer
authentication, which will fail because of how we’ve set up the PostgreSQL user combined with the default settings in pg_hba.conf
.
(Although explaining the details of PostgreSQL authentication modes falls beyond the scope of this article.)
SQLite is easy compared to MySQL and PostgreSQL—since SQLite doesn’t use a database server, we don’t have to deal with account and privilege management; all we have to do is install SQLite:
# Install SQLite
laravel@server$ sudo apt install sqlite3
That’s all you need for now—I’m assuming you’ll be bring along an existing *.sqlite
database file with a Laravel app.
Next: The next article covers server-side Git setup.
Finding this tutorial series useful? Consider saying thank you!
The original writing and media in this series is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.