In this article:

How to Import Data from a MySQL Database to Google Sheets

>Click here to Import a MYSQL Database to a Lido Spreadsheet

Imagine that you really want to maximize the spreadsheet formulas in Google Sheets, but the data is from MySQL. Worry not, as here is a way you can do so! If you are wary of third-party add-ons, you can use this code in order to import data from MySQL to Google Sheets. Are you ready?

Prepare your MySQL server

Importing data from MySQL server to Google Sheets is done via the JDBC. A list of IP addresses must be whitelisted so that the JDB service will work. The list is available here. Additionally, the port number should be 1025 and above. 


To learn more about the requirements, check the documentation page of JDBC


Add the script to Google Apps Script

Step 1: Access the Script editor by clicking Tools on the main menu:

Tools option from toolbar, Script editor tool highlighted.

The Google Apps Script now looks as follows:

Google Apps Script interface.



Step 2: Paste the following code, taken from Actiondesk:


var server = 'server_url';

var port = port_number;

var dbName = 'database_name';

var username = 'username';

var password = 'password';

var url = 'jdbc:mysql://'+server+':'+port+'/'+dbName;

 

function readData() {

 var conn = Jdbc.getConnection(url, username, password);

 var stmt = conn.createStatement();

 var results = stmt.executeQuery('SELECT * FROM table_title’);

 var metaData=results.getMetaData();

 var numCols = metaData.getColumnCount();

 var spreadsheet = SpreadsheetApp.getActive();

 var sheet = spreadsheet.getSheetByName('Sheet1');

 sheet.clearContents();

 var arr=[];

 

 for (var col = 0; col < numCols; col++) {

   arr.push(metaData.getColumnName(col + 1));

 }

 

 sheet.appendRow(arr);

 

while (results.next()) {

 arr=[];

 for (var col = 0; col < numCols; col++) {

   arr.push(results.getString(col + 1));

 }

 sheet.appendRow(arr);

}

 

results.close();

stmt.close();

sheet.autoResizeColumns(1, numCols+1);


Copy-paste this code, changing the following:

  • server_url is for the url of the MySQL server
  • port_number is the port number for the MySQL server
  • database_name is the name of the MySQL database
  • username is the username for access
  • password is the corresponding password for the given username
  • The url has the following syntax: jdbc:mysql://server_url+port/database_name, but in the case of the example used here, there is no need to add the port number, so the following syntax may work: jdbc:mysql://server_url/database_name
  • table_title is the name of the table you want to load


The listed information will help you connect to the MySQL server. 


Step 3: Save the script by clicking this button.

Google Apps Script, Save button


Step 4: Run the script by clicking the Run button adjacent to the save button. 

Google Apps Script, Run button


An authorization request will pop up the first time you run it. Simply allow the script.


If everything goes well, you should get the following messages in the box below the code:

Execution log messages when the code runs successfully


Once you check the sheet, you will see that the data is now loaded:

Output. The data is loaded from MySQL to Google Sheets.


Extra: check the table names

The script is perfectly designed to allow you to run single-line SQL queries. If you want to check all the table names in the database, replace the query inside stmt.executeQuery with the following:


SELECT * FROM INFORMATION_SCHEMA.TABLES


This will retrieve the list of tables and their associated databases, and you can check the table name there.

An Easier Option

Do you find this requiring too much of your time? Lido provides a simple and easy solution to importing data from your e-Commerce and marketing platforms. Automatically import data from your favorite e-Commerce and marketing platforms such as Shopify, Facebook, and Google Analytics and apply Lido’s software to extract meaningful metrics from them. Get started here

Related Articles

-Filter SQL Data Before Importing to Google Sheets

-Import PostgreSQL Database to Google Sheets

-Export SQL Data to Google Sheets

-SQL Basics

-Most Useful SQL Functions

Automate repetitive tasks with Lido

Save hours on repetitive and tedious work. Lido is a new spreadsheet that connects your spreadsheets, forms, PDFs, and email inbox.

Trigger
Action
Select trigger
When a cell value in Google Sheets changes
Check Google Sheets for today's date
When a new row is added to Google Sheets
On new Google Form submission
Send me a daily reminder
On new Typeform submission
When a cell value in database changes
Check database for today's date
When a new row is added in database
When a new HubSpot customer is created
Select action
ADDCALENDAREVENT
Creates a one- or multiple-day calendar event with optional attendees
ADDHUBSPOT
Adds an object to Hubspot
ADDSLACKCHANNEL
Create a Slack channel, and optionally add a topic or members
CALLURL
Makes any HTTP request
CREATEGOOGLEDOC
Replaces [@column_name] values in a Google Doc with the corresponding table row's values
CREATEPDF
Replaces [@column_name] values in a Google Doc with the corresponding table row's values, then export it as a PDF
FETCH
Makes any HTTP request and returns the response
INSERTROWS
Inserts given array below defined values in given worksheet
SENDGMAIL
Sends an email using your Google account
SENDOUTLOOK
Sends an email using your Microsoft Outlook account
SENDSLACK
Sends a Slack message
SENDSMS
Sends an SMS message using your Twilio account
UPDATECELL
Updates cells with given values
UPDATEHUBSPOT
Updates a property of a Hubspot object
Trigger is required
Action is required
Get started