Install MySQL on Compute Engine

To manually install MySQL on a Compute Engine instance that you'vecreated, complete the following steps.

For an overview of several options for setting up MySQL onCompute Engine, seeMySQL on Compute Engine.To learn which Compute Engine offerings are appropriate for MySQL, seeConfigure MySQL on Compute Engine.

Before you begin

  • Use the Google Cloud console toturn on the Compute Engine API.
  • Install theGoogle Cloud CLI
  • Configure your workspace to make commands less verbose. Substitute yourproject's values forPROJECT_ID,REGION, andZONE in thefollowing commands. For the full list of zones, seeAvailable regions & zones.

    gcloud config set projectPROJECT_IDgcloud config set compute/regionREGIONgcloud config set compute/zoneZONE
Important: This tutorial uses Compute Engine, which is a billablecomponent of Google Cloud. Use thePricing Calculator to generate a costestimate based on your projected usage. New Google Cloud users may beeligible for afree trial.

Create a Compute Engine instance and establish an SSH connection

Create a Compute Engine instance for MySQL and establish an SSHconnection to the newly created instance. The default operating system is Debianversion 10. If you prefer to use a different operating system for this tutorial,you can choose from the options described on thepublic images page in the Compute Engine documentation.

Console

Create the Compute Engine instance

To create the Compute Engine instance in theGoogle Cloud console, dothe following:

  1. In the Google Cloud console, go to theCompute Engine VMinstances page.

    Go to Compute Engine VM instances.

  2. Select your newly created project and clickContinue.

  3. ClickCreate instance (New instance if you have existinginstances). Name the instancemysql-test.

  4. Select your performance level and size your disk.

  5. To specify an operating system other than the default value,in theBoot disk section, clickChange to configure theproperties for the boot disk. In thePublic images tab, selectan operating system and then clickSave.

  6. To conform with security best practices, create your instance withoutan external IP address.

    ExpandAdvance options and expandNetworking. UnderNetworkinterfaces, expand thedefault interface, and in theExternalIPv4 address menu, selectNone.

    Note: Your instance will need to reach public package repositoriesduring the MySQL installation. To allow internet access, configureCloud NAT to manage network address translation.
  7. ClickCreate.

Set up network address translation

To set up and manage network address translation with Cloud NAT, do thefollowing:

  1. In the Google Cloud console, go to theCloud NAT page.

    Go to Cloud NAT

  2. ClickGet started orCreate Cloud NAT gateway.

  3. ForGateway name, enternat-simple-configuration.

  4. To select the Cloud Router, in theSelect Cloud Router section,do the following:

    1. ForNetwork, selectdefault.
    2. ForRegion, select the region that you specified for your VM.
    3. ForCloud Router, selectCreate new router.

      In theCreate a router dialog that appears, provide aNamefor the router—for example,nat-router-simple-configuration—andclickCreate.

  5. ClickAdvanced configurations.

  6. In theLogging section, selectTranslation and errors. Thissetting configures Cloud NAT to send all logs to Cloud Logging.

  7. ClickCreate.

Establish an SSH connection to the VM

To establish an SSH connection, do the following:

  1. On theVM instances page, find your new VM instance in the list.

  2. In theConnect column for that VM, clickSSH. The SSH terminalopens in a browser window.

gcloud

  1. To create a Compute Engine instance, use thegcloud computeinstances create command. To specify the operating system, add the--image-family parameter followed by theimage family or the--image parameter followed by the image name for a specificimage version. For example, to use the most recent image from theDebian 10 family, add--image-family debian-10. When using publicimages, theimage project must be provided using the--image-project parameter.

    gcloud compute instances create \    --image-family debian-10 \    --image-project debian-cloud \    --no-address \  mysql-test
    Note: To conform with security best practices, the--no-address optioncreates this instance without an external IP address. However, yourinstance will need to reach public package repositories during theMySQL installation. To allow internet access, configure Cloud NAT tomanage network address translation.
  2. Create a Cloud Router.

    gcloud compute routers create nat-router-simple-configuration \    --network=default
  3. Set up and manage network address translation with Cloud NAT.

    gcloud compute routers nats create nat-simple-configuration \    --router=nat-router-simple-configuration \    --auto-allocate-nat-external-ips \    --nat-all-subnet-ip-ranges \    --enable-logging
  4. Connect to the instance usingssh.

    gcloud compute ssh \    --tunnel-through-iap \    mysql-test

    The SSH connection is made using TCP forwarding that is handled byIdentity-Aware Proxy (IAP).

    Note: Depending on your VPC network configuration or organizationpolicies, it may be necessary toprepare your project for IAP TCP forwarding.

Install MySQL

The following steps describe how to install MySQL on your Compute Engineinstance.

Debian 10+

These instructions install MySQL 8 or 5.7.

Versions 10 and later of Debian contain MariaDB instead of MySQL as part of itspackage management system. MariaDB maintains compatibility with the MySQLprotocol, but has an independently evolving feature set. For more details,seeMariaDB versus MySQL.

To install MySQL, download the release package and manually install usingthedpkg command.

  1. Install thewget dependency.

    sudo apt-get install -y wget
  2. Download the MySQL Community Server release package.

    export DEB_FILE=mysql-apt-config_0.8.20-1_all.debcd /tmpcurl -L --output ${DEB_FILE} \     https://dev.mysql.com/get/${DEB_FILE}
  3. Verify the integrity of the release package file.

    cat >${DEB_FILE}.md5 <<EOL799bb0aefb93d30564fa47fc5d089aeb${DEB_FILE}EOLmd5sum--check${DEB_FILE}.md5

    The authenticity and integrity of the file are verified if you see thefollowing output:

    mysql-apt-config_0.8.20-1_all.deb:OK
  4. After you've verified the file, add the MySQL package to the localpackage repository.

    sudodpkg-i${DEB_FILE}

    You're prompted to confirm the installation options includingthe MySQL version.

  5. With the topMySQL Server & Cluster menu option selected, pressReturn and then use the arrow keys to choose a server version.

    This guide expects you to choose either MySQL 8.0 or 5.7. PressReturn on your keyboard after you have selected the version.

  6. When you are satisfied with the options selected in the configurationmenu, use the arrow keys to selectOk in the menu and presssReturn on your keyboard.

  7. Update the package cache.

    sudoapt-getupdate

    If you receive a GPG error similar toThe following signatures couldn'tbe verified because the public key is not available: NO_PUBKEY467B942D3A79BD29, import the missing GPG key(s) with a command thatfollows this pattern:

    sudo apt-key adv \    --keyserver keyserver.ubuntu.com \    --recv-keys 467B942D3A79BD29

    In the demonstrative error message, the missing public key is467B942D3A79BD29; provide this value with the--recv-keys option.

    Repeat the command to update the package cache.

  8. Install MySQL. The installation process starts the MySQL service for you.

    sudo apt-get -y install mysql-community-server

    You are prompted to provide some details for the installationsuch as the root password.

Debian 9

These instructions install MySQL 8 or 5.7.

By Default, some versions of Debian, such as Debian 9, install MariaDB as thedefault MySQL server. MariaDB is designed to be largely compatible withMySQL and can be invoked using the same commands. For details on howMariaDB differs from standard MySQL, seeMoving from MySQL to MariaDB in Debian 9.

To install MySQL, follow these instructions:

  1. Download the MySQL Community Server release package.

    export DEB_FILE=mysql-apt-config_0.8.17-1_all.debcd /tmpcurl -L --output ${DEB_FILE} \     https://dev.mysql.com/get/${DEB_FILE}
  2. Verify the integrity of the release package file.

    cat > ${DEB_FILE}.md5<< EOL9e393c991311ead61dcc8313aab8e230 ${DEB_FILE}EOLmd5sum --check ${DEB_FILE}.md5

    The authenticity and integrity of the file are verified if you see thefollowing output.

    mysql-apt-config_0.8.17-1_all.deb: OK
  3. After you have verified the file, add the MySQL package to the localpackage repository.

    sudo dpkg -i ${DEB_FILE}

    You are prompted to confirm the installation options includingthe MySQL version.

  4. With the topMySQL Server & Cluster menu option selected, pressReturn and then use the arrow keys to choose a server version.

    This guide expects you to choose either MySQL 8.0 or 5.7. PressReturn on your keyboard after you have selected the version.

  5. When you are satisfied with the options selected in the configurationmenu, use the arrow keys to selectOk in the menu and presssReturn on your keyboard.

  6. Update the package cache.

    sudo apt-get update

    If you receive a GPG error similar toThe following signatures couldn'tbe verified because the public key is not available: NO_PUBKEY467B942D3A79BD29, import the missing GPG key(s) with a command thatfollows this pattern:

    sudo apt-key adv \    --keyserver keyserver.ubuntu.com \    --recv-keys 467B942D3A79BD29

    In the demonstrative error message, the missing public key is467B942D3A79BD29; provide this value with the--recv-keys option.

    Repeat the command to update the package cache.

  7. Install MySQL. The installation process starts the MySQL service for you.

    sudo apt-get -y install mysql-community-server

Ubuntu

ForUbuntu 1804 and earlier these instructions installMySQL 5.7.
ForUbuntu 2004 and later these instructions installMySQL 8.

  1. Update theapt-get package manager.

    sudo apt-get update
  2. Install MySQL. The installation process starts the MySQL service for you.

    sudo apt-get -y install mysql-server
  3. By default in Ubuntu distributions, mysql authenticates theroot userwith theauth_socket plugin. Run this query to verify this defaultconfiguration:

    echo "SELECT user, authentication_string, plugin, host        FROM mysql.user WHERE user='root' ;" \     | sudo mysql -t -u root

    The output shows that theroot user connecting fromlocalhost willuse theauth_socket plugin:

    +------+-----------------------+-------------+-----------+| user | authentication_string | plugin      | host      |+------+-----------------------+-------------+-----------+| root |                       | auth_socket | localhost |+------+-----------------------+-------------+-----------+
  4. Change this setting, and change the root password to one that isdifficult to guess:

    export PASSWORD=`uuidgen`; echo "Root password is : $PASSWORD"echo "ALTER USER 'root'@'localhost'      IDENTIFIED WITH mysql_native_password BY '$PASSWORD'" \      | sudo mysql -u root

    The output contains the new root password:

    Root password is : 25fe2177-778a-414f-b80e-5fb8c8671de3

CentOS or RHEL 7

These instructions install MySQL 8.

Version 7 of CentOS and RHEL contain MariaDB instead of MySQL as partof its package management system. To install MySQL, you mustfirst update the package manager.

  1. Download the MySQL Community Server release package.

    export RPM_FILE=mysql80-community-release-el7-4.noarch.rpmcd /tmpcurl -L --output ${RPM_FILE} \     https://dev.mysql.com/get/${RPM_FILE}
  2. Verify the integrity of the release package file.

    cat > ${RPM_FILE}.md5<< EOL8b55d5fc443660fab90f9dc328a4d9ad ${RPM_FILE}EOLmd5sum --check ${RPM_FILE}.md5

    The authenticity and integrity of the file are verified if you see thefollowing output.

    mysql80-community-release-el7-4.noarch.rpm: OK
  3. Update the package manager to include MySQL.

    sudo rpm -Uvh ${RPM_FILE}
  4. Install MySQL.

    sudo yum -y install mysql-community-server
  5. Start MySQL server.

    sudo /usr/bin/systemctl start mysqld
  6. Obtain the temporary root password from server logs

    sudo cat /var/log/mysqld.log | grep -i 'temporary password'

Rocky Linux or RHEL 8

These instructions install MySQL 8.

Version 8 of Rocky Linux, CentOS Stream, and RHEL contain MariaDB instead ofMySQL as part of its package management system. To install MySQL, youmust first update the package manager.

  1. Download the MySQL Community Server release package.

    export RPM_FILE=mysql80-community-release-el8-2.noarch.rpmcd /tmpcurl -L --output ${RPM_FILE} \     https://dev.mysql.com/get/${RPM_FILE}
  2. Verify the integrity of the release package file.

    cat > ${RPM_FILE}.md5<< EOL0ba3feb1c9ee35d30e5ae683accaf54b ${RPM_FILE}EOLmd5sum --check ${RPM_FILE}.md5

    The authenticity and integrity of the file are verified if you see thefollowing output.

    mysql80-community-release-el8-2.noarch.rpm: OK
  3. Update the package manager to include MySQL.

    sudo rpm -iUvh ${RPM_FILE}
  4. Install MySQL. During the installation, disable the AppStreamrepository, so that the installation will use the community server.

    sudo yum module disable -y mysqlsudo yum install -y \    --disablerepo=appstream \  mysql-community-server
  5. Start MySQL server.

    sudo /usr/bin/systemctl start mysqld
  6. Obtain the temporary root password from server logs

    sudo cat /var/log/mysqld.log | grep -i 'temporary password'

Improve MySQL installation security

To improve the security of your MySQL installation, run themysql_secure_installation command. If you didn't set a password duringthe installation process, create a password in this step. For more informationabout this command, see the MySQL documentation formysql_secure_installation.

sudo mysql_secure_installation

Connect to MySQL

The following steps describe how to connect to MySQL from yourmysql-testinstance.

MySQL 8.0

  1. Connect to MySQL using the MySQL client.

    sudo mysql -u root -p
    Note: In most cases, you should use a non-root user account to access yourMySQL database. For details on adding a user account, seeAdding User Accounts in the MySQL documentation.

    When you connect to MySQL, the prompt changes tomysql>.

    You can then run MySQL commands. For example, the following command showsthe threads running, including the current connection.

    mysql>SHOWprocesslist;
    +----+-----------------+-----------+------+---------+------+------------------------+------------------+| Id | User            | Host      | db   | Command | Time | State                  | Info             |+----+-----------------+-----------+------+---------+------+------------------------+------------------+|  5 | event_scheduler | localhost | NULL | Daemon  | 1889 | Waiting on empty queue | NULL             || 14 | root            | localhost | NULL | Query   |    0 | init                   | show processlist |+----+-----------------+-----------+------+---------+------+------------------------+------------------+2 rows in set (0.00 sec)

    You can use the following command to generate a list of users.

    mysql>SELECTUser,Host,authentication_stringFROMmysql.user;
    +------------------+-----------+------------------------------------------------------------------------+| User             | Host      | authentication_string                                                  |+------------------+-----------+------------------------------------------------------------------------+| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.session    | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.sys        | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || root             | localhost | $A$005$,BS{G+*#cVYxb6x40q0aFS5dp2/Kz6u2vennR5qe0eBKVA/6VW5B            |+------------------+-----------+------------------------------------------------------------------------+4 rows in set (0.00 sec)
  2. When you are done running commands, use theexit command to quit out of theMySQL client, and then useexit again to sign out of theCompute Engine instance.

    mysql>exit
    Bye

MySQL 5.7

  1. Connect to MySQL using the MySQL client.

    sudo mysql -u root -p
    Note: In most cases, you should use a non-root user account to access yourMySQL database. For details on adding a user account, seeAdding User Accounts in the MySQL documentation.

    When you connect to MySQL, the prompt changes tomysql>.

    You can then run MySQL commands. For example, the following command showsthe threads running, including the current connection.

    mysql>SHOWprocesslist;
    +----+------+-----------+------+---------+------+-------+------------------+| Id | User | Host      | db   | Command | Time | State | Info             |+----+------+-----------+------+---------+------+-------+------------------+| 51 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |+----+------+-----------+------+---------+------+-------+------------------+1 row in set (0.00 sec)

    You can use the following command to generate a list of users.

    mysql>SELECTUser,Host,authentication_stringFROMmysql.user;
    +---------------+-----------+-------------------------------------------+| User          | Host      | authentication_string                     |+---------------+-----------+-------------------------------------------+| root          | localhost | *A047B05AAB007B33F8F2BD1FD404661D167D6348 || mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |+---------------+-----------+-------------------------------------------+3 rows in set (0.00 sec)
  2. When you are done running commands, use theexit command to quit out of theMySQL client, and then useexit again to sign out of theCompute Engine instance.

    mysql>exit
    Bye

What's next

  • For more information about MySQL, see theofficial MySQL documentation.
  • Explore thewide variety of development stacks on Cloud Marketplace that use MySQL.
  • If your requirements include high availability and scalability, consider thefollowing options:
    • InstallMySQL Cluster on Compute Engine for high availability and scalability throughshared-nothing clustering and auto-sharding.
    • Click to deployPercona,an open source solution for MySQL clustering, fromCloud Marketplace.
    • InstallVitess,the open source solution which has served all YouTube database trafficsince 2011. Vitess is well-suited for applications that run incontainers. For information about using Vitess in a containerizedenvironment, seeRunning Vitess on Kubernetes.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.