Connect Laravel to a managed MySQL on DigitalOcean2 min read
Last year DigitalOcean introduced their take on DBaaS, which is a managed database service (DataBase as a Service – DBaaS), and this year they started support for MySQL as well. For all Laravel developers this is great as some of the overhead now can be outsourced without being costly, starting at just $15 per month.
But what is it DigitalOcean offers? From their website it says:
Leave the complexity of database administration to us. We’ll handle setting up, backing up, and updating — so you can focus on building great apps.DigitalOcean
To create your managed database log into DigitealOcean and create a MySQL database
Fill in information as below or modify to your needs, you can always scale it later if it’s too small, you can even change location later as long as there are free slots available. You should pick a unique name for your database of course.
To create a Laravel user, navigate to Users & Databases for your new database and fill the Add new user with your desired username, you should also create a database in the table below.
Unfortunately at this time being DigitalOcean doesn’t allow for creating a user with access to only one database – at least not through their interface.
The database is installed and ready to run – almost. Before you can access it from Laravel you have to change how users are authenticated. This is unfortunately not a simple switch in the control panel of DigitalOcean, but I have previously requested this to their support, so hopefully in the future it will be easier.
Instead you must log into your database and for every user that will be accessed from Laravel change to
Navigate to Overview for your database in DigitalOceans dashboard and take a note of Connection parameters to the right. This is the information you need to connect to your database.
Here is how it looks in HeidiSQL
Go to the Query tab and execute the following. Remember to change myuser and mypassword.
ALTER USER myuser IDENTIFIED WITH mysql_native_password BY 'mypassword';
If you are using MacOS you should be able to use Sequel Pro to login and execute the command
If you are using Windows you can use HeidiSQL to execute the command.
Note from DigitalOcean
As mentionend I already requested a confirm from DigitalOcean whether this was the way to do it:
Happy to assist here!DigitalOcean support on email
That is exactly correct, you can do it manually with a command similar to:
ALTER USER myuser IDENTIFIED WITH mysql_native_password BY ‘mypassword’;
With the user and password inserted in.