If your role is to deploy an application to Digital Ocean, but you don’t know how to connect it to your MySQL database server, then you've come to the right place. This tutorial will walk you through how to connect MySQL to a droplet on Digital Ocean.
If you don’t already have MySQL installed on your Digital Ocean droplet, you can do so using the command below:
For Centos:
sudo yum install mysql-server
/etc/init.d/mysqld start
For Ubuntu:
sudo apt-get install mysql-server
To create a database, run the follow the following command in your terminal:
$ CREATE DATABASE database name;
In this tutorial, we will call our database mydata.
Check the database
$SHOW DATABASES;
The result should look like this.
Access the database. You can do this with the following command:
$USE mydata;
Create a MySQL table with the commands shown below.
$ CREATE TABLE hotpot (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
food VARCHAR(30),
confirmed CHAR(1),
signup_date DATE);
The result should look like this.
Show the MySQL table with the following command.
$ SHOW tables;
The result should look like this.
To connect using the flags format with MySQL using the doadmin
user, paste the entire command from the control panel into your terminal:
$mysql -u doadmin -p your_password -h mysql-test-do-user-4915853-0.db.ondigitalocean.com -P 25060 -D defaultdb;
You can also leave the password out when using the above commands, but you will be prompted to enter it before you can connect.
When you successfully connect, the shell displays some information about the MySQL server and your changes. Successful MySQL connections look like this:
To test your connection, input the following information in a SQL client like Arctype