Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

1. Railway Dashboard

Click dev.new and choose "Provision MySQL"

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

Copy and paste the MySQL client command.

Your own password will be included in place of xxxx.

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

(download MySQL with brew install mysql)

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)

2. Railway CLI

Railway CLI

First you need to install the Railway CLI.

Check Railway CLI version

railway version
railway version 0.2.40

Login with railway login

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

railway login

Initialize project with railway init

Run the following command, select “Empty Project,” and give your project a name.

railway init

Provision MySQL with railway add

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

railway add

Link to your project

railway link

Select your newly created project from the list.

Connect to database

railway connect mysql

Seed database

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!');

01-railway-seed-data

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