If you've been struggling to connect your MySQL database with Google Sheets, you've come to the right place.
Establishing a connection from Google Sheets to MySQL allows you to avoid manually updating data in one source and importing/exporting data sets, thus reducing human errors.
Connecting Google Sheets with MySQL is also a way to limit access to a data source, while giving access to that data in another source. For example, if your company stores data about sales orders, but the sales representatives don't feel comfortable with querying the database, you can connect the database to Google Sheets, so they read and analyze the data in the spreadsheet.
In this tutorial, we'll show you how to sync Google spreadsheet with MySQL database in two ways: the hard code way using Apps Script, and the no-code way using n8n workflows.
Table of contents
Can you connect Google Sheets to a database?
Method 1: Google Apps Script to connect MySQL to Google Sheets
Method 2: n8n automated workflows to connect MySQL to Google Sheets
Prerequisites for building the workflows
Workflow to import data from Google Sheets into MySQL
Workflow to import data from MySQL into Google Sheets
What's next?
Can you connect Google Sheets to a database?
Yes, you can connect Google Sheets to a database like MySQL.There are at least two methods to do this: using Google Apps Script and using n8n workflows.
The first method involves using Google Apps Script to write JavaScript code, which you can then use for Google Sheets. This is an efficient way to connect your spreadsheets to your database. The downsides are that you need solid JavaScript skills and the script can be limiting.
The second method involves using n8n to build workflow automations – with no code! You just have to connect nodes (integrations) for MySQL and Google Sheets, and you’re all set. Moreover, you can extend the logic of your workflows by connecting more nodes. Though the learning curve might be steep, the workflows help you achieve more in the long run. For example, you can also synchronize data (one-way or two-way) between a spreadsheet and a database, or create database backups.
Method 1: Google Apps Script to connect MySQL to Google Sheets
Apps Script is the cloud-based JavaScript platform from Google. With Apps Script, you can integrate with and automate tasks across Google products. For example, you can create scripts to connect MySQL to Google Sheets.
- Set up your MySQL database and get the connectivity data:
- Server
- Database name
- Username
- Password
- Port Number
2. In your Google Sheet, select Extensions > Apps Script.
This opens a new untitled project in Apps Script in your browser. The project includes a place-holder function in the code editor.
3. In the code editor, replace the place-holder function with the JavaScript code for creating a connection with MySQL. For example, you can use this public code.
4. Save the project under a descriptive name (e.g. MySQL Connection).
5. Run the script by pressing the Run Script icon. Note: The first time you run the script you will be asked to provide authorization to Google.
Method 2: n8n automated workflows to connect MySQL to Google Sheets
If you want to avoid coding in JavaScript (or another language), you can use a no-code workflow automation tool like n8n.
Next, we'll show you two workflows that automatically import data from Google Sheets into MySQL, and the other way around.
Prerequisites for building the workflows
- n8n set up. The easiest way to get started is to sign up for n8n cloud. You can also self-host n8n.
- A MySQL database and credentials. You need these to use the MySQL integration to access the MySQL API.
- A Google account and credentials. You need these to use the Google Sheets integration to access the Google Sheets API. The API is free to use, but beware of usage quotas and limitations.
Workflow to import data from Google Sheets into MySQL
This workflow automatically imports data from Google Sheets into a MySQL database every week.
- Cron node executes the workflow every Monday at 5am. You can change the time interval by tweaking the following parameters:
- Mode: Every Week
- Hour: 5
- Minute: 0
- Weekday: Monday
2. Google Sheets node reads data from a spreadsheet.
3. MySQL node inserts the data from Google Sheets into a MySQL table.
Make sure that the column names in Google Sheets and the MySQL table match. In this example, the MySQL node has the following parameters:
- Operation: Insert
- Table: books
- Columns: title, price
Workflow to import data from MySQL into Google Sheets
This workflow automatically imports data from a MySQL database into Google Sheets.
The node configuration is similar to the previous workflow.
- Cron node executes the workflow at regular preset time intervals.
- MySQL node selects all book titles and prices from the books table in the database. You can change what data you want to extract from the table by tweaking the following parameters:
- Operation: Execute Query
- Query: SELECT * FROM books;
Google Sheets node appends the data from the MySQL table to a sheet.
Alternatively, you can update data or create new records in the sheet by selecting the operation Create or Update. Note that in this case, you need to have a key column (for example, a unique ID) based on which to compare the new and existing data.
What’s next?
In this post, you learned how to connect MySQL with Google Sheets in two different ways: using Google App Script and no-code workflows in n8n.
Now you have two workflow templates for connecting your database with spreadsheets, making your work more efficient.
Using n8n to automate your Google Sheets to MySQL connection opens up new possibilities and takes your productivity to the next level. You can automate processes in no time, with no code, by connecting the integrations you need.
If you want to extend the logic of your workflows or add custom JavaScript code to them, you can use core nodes like Function, Cron, or Set. And along the way, you’ll see your workflow unfold, one node at a time.
Here's what you can do next:
- Read more tutorials about importing data into Google Sheets, for example how to import JSON from an API and local files.
- Check out the workflow page for more automation ideas with Google Sheets and MySQL. For example, you can add MySQL nodes to power up these workflows that sync data between multiple Google Sheets or get data via a REST API.
- Join the community forum, follow us on Twitter to get the latest news around workflow automation with n8n.