Skip to content

Instantly share code, notes, and snippets.

@ajcwebdev
Last active April 4, 2024 02:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajcwebdev/8599295373c092f30fce9968eaf48635 to your computer and use it in GitHub Desktop.
Save ajcwebdev/8599295373c092f30fce9968eaf48635 to your computer and use it in GitHub Desktop.
Getting Started with MySQL on Railway

Getting Started with MySQL on Railway

You can provision a MySQL database through either the Railway Dashboard or the Railway CLI.

Outline

Railway Dashboard

Open dev.new and choose "Provision MySQL."

01-railway-mysql

After the database is setup click "MySQL" on the left and then choose "Connect".

02-connect-to-database

Copy and Paste MySQL Client Command

Download the MySQL command line client. If you're on a Mac you can use brew.

brew install mysql

Your own password will be included in place of xxxx.

mysql -hcontainers-us-west-8.railway.app -uroot -xxxx --port 7551 --protocol=TCP railway

Run the following SQL commands to create a test table with seed data.

CREATE TABLE test_table (Test text);
INSERT INTO test_table VALUES ('Hello'), ('Goodbye');
show tables;

Output:

+-------------------+
| Tables_in_railway |
+-------------------+
| test_table        |
+-------------------+
1 row in set (0.04 sec)

Railway CLI

First you need to install the Railway CLI.

curl -fsSL cli.new | sh

Check Railway CLI version.

railway -V

Login to Railway

If you do not have a Railway account you will be prompted to create one.

railway login

Add --browserless if you can't use a browser for logging in.

Initialize Project

Run the following command to create a new Railway project called mysql-railway.

railway init -n mysql-railway

Provision MySQL Plugin

Run the following command to add the MySQL plugin to your Railway project.

railway add -p mysql

Link Your Project

railway link

Select your newly created mysql-railway project from the list.

Connect to Database

railway connect MySQL

Run the following SQL commands to create a test table with seed data.

CREATE TABLE Post (title text, body text);
INSERT INTO Post VALUES ('This is a blog post', 'Wooooooo');
INSERT INTO Post VALUES ('Another blog post', 'Even better than the other!');

List Tables in Database

SHOW TABLES;
+-------------------+
| Tables_in_railway |
+-------------------+
| Post              |
+-------------------+
1 row in set (0.04 sec)

Quit mysql

quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment