Open navigation

How to connect to a MySQL database

If you have a MySQL database hosted on an AWS EC2 instance, you can connect it to Google Sheets with Supermetrics. Set up a connection to your database and query your data directly in Google Sheets.


Before you begin

These instructions include general steps for enabling remote access to your database. They are not exhaustive, and your setup might require additional configuration.


Please be sure to consult your system administrator before making any changes to your database.


Instructions

Step 1: Network access

  1. Go to the AWS console.
  2. Navigate to Security Groups.
  3. Select the security group of the EC2 instance where you are hosting your MySQL database.
  4. Add a rule in the Inbound section by selecting MySQL/Aurora and add the relevant IP addresses.

See AWS documentation to learn more about DB security groups.


Step 2: MySQL binding address

  1. Edit the file /etc/mysql/my.cnf. This could also be /etc/mysql/conf.d/mysql.cnf for certain versions of MySQL.
  2. Change the binding address to 0.0.0.0:
    bind-address = 0.0.0.0
  3. Restart your MySQL server:
    sudo /etc/init.d/mysql restart


Step 3: User and privileges

  1. Create a new user for any host in MySQL. You could use a command like this:
    CREATE USER 'foo'@'%' IDENTIFIED BY 'your_strong_password';
  2. Grant the user privileges to the relevant tables:
    GRANT SELECT PRIVILEGES ON bar.* TO 'foo'@'%';
  3. Update the user privileges:
    FLUSH PRIVILEGES;
  • You might also consider limiting the user’s privileges on CREATE and GRANT calls by determining specific IP address(es) instead of using '%'.


Step 4: Supermetrics sidebar

  1. Open the Supermetrics sidebar in Google Sheets.
  2. Under Data source, select Database.
  3. Fill in the requested details.
    • The Request origin should be selected based on the IP addresses you allowlisted in step 1.
  4. Click Test connection to verify the configuration is correct and Save the data source.
  5. Write your SQL statement to the Query SQL input box. 
  6. Click Get data to create your query and pull your data.


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.