Install MySQL on Compute Engine Stay organized with collections Save and categorize content based on your preferences.
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
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:
In the Google Cloud console, go to theCompute Engine VMinstances page.
Select your newly created project and clickContinue.
ClickCreate instance (New instance if you have existinginstances). Name the instancemysql-test.
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.
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.ClickCreate.
Set up network address translation
To set up and manage network address translation with Cloud NAT, do thefollowing:
In the Google Cloud console, go to theCloud NAT page.
ClickGet started orCreate Cloud NAT gateway.
ForGateway name, enter
nat-simple-configuration.To select the Cloud Router, in theSelect Cloud Router section,do the following:
- ForNetwork, selectdefault.
- ForRegion, select the region that you specified for your VM.
ForCloud Router, selectCreate new router.
In theCreate a router dialog that appears, provide aNamefor the router—for example,
nat-router-simple-configuration—andclickCreate.
ClickAdvanced configurations.
In theLogging section, selectTranslation and errors. Thissetting configures Cloud NAT to send all logs to Cloud Logging.
ClickCreate.
Establish an SSH connection to the VM
To establish an SSH connection, do the following:
On theVM instances page, find your new VM instance in the list.
In theConnect column for that VM, clickSSH. The SSH terminalopens in a browser window.
gcloud
To create a Compute Engine instance, use the
gcloud computeinstances createcommand. To specify the operating system, add the--image-familyparameter followed by theimage family or the--imageparameter 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-projectparameter. Note: To conform with security best practices, thegcloud compute instances create \ --image-family debian-10 \ --image-project debian-cloud \ --no-address \ mysql-test
--no-addressoptioncreates 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.Create a Cloud Router.
gcloud compute routers create nat-router-simple-configuration \ --network=default
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
Connect to the instance using
ssh.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.
Install the
wgetdependency.sudo apt-get install -y wget
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}Verify the integrity of the release package file.
cat >${DEB_FILE}.md5 <<EOL799bb0aefb93d30564fa47fc5d089aeb${DEB_FILE}EOLmd5sum--check${DEB_FILE}.md5The authenticity and integrity of the file are verified if you see thefollowing output:
mysql-apt-config_0.8.20-1_all.deb:OKAfter 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.
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.
When you are satisfied with the options selected in the configurationmenu, use the arrow keys to select
Okin the menu and presssReturn on your keyboard.Update the package cache.
sudoapt-getupdateIf 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 is
467B942D3A79BD29; provide this value with the--recv-keysoption.Repeat the command to update the package cache.
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:
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}Verify the integrity of the release package file.
cat > ${DEB_FILE}.md5<< EOL9e393c991311ead61dcc8313aab8e230 ${DEB_FILE}EOLmd5sum --check ${DEB_FILE}.md5The authenticity and integrity of the file are verified if you see thefollowing output.
mysql-apt-config_0.8.17-1_all.deb: OK
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.
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.
When you are satisfied with the options selected in the configurationmenu, use the arrow keys to select
Okin the menu and presssReturn on your keyboard.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 is
467B942D3A79BD29; provide this value with the--recv-keysoption.Repeat the command to update the package cache.
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.
Update the
apt-getpackage manager.sudo apt-get update
Install MySQL. The installation process starts the MySQL service for you.
sudo apt-get -y install mysql-server
By default in Ubuntu distributions, mysql authenticates the
rootuserwith theauth_socketplugin. 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 the
rootuser connecting fromlocalhostwilluse theauth_socketplugin:+------+-----------------------+-------------+-----------+| user | authentication_string | plugin | host |+------+-----------------------+-------------+-----------+| root | | auth_socket | localhost |+------+-----------------------+-------------+-----------+
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.
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}Verify the integrity of the release package file.
cat > ${RPM_FILE}.md5<< EOL8b55d5fc443660fab90f9dc328a4d9ad ${RPM_FILE}EOLmd5sum --check ${RPM_FILE}.md5The authenticity and integrity of the file are verified if you see thefollowing output.
mysql80-community-release-el7-4.noarch.rpm: OK
Update the package manager to include MySQL.
sudo rpm -Uvh ${RPM_FILE}Install MySQL.
sudo yum -y install mysql-community-server
Start MySQL server.
sudo /usr/bin/systemctl start mysqld
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.
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}Verify the integrity of the release package file.
cat > ${RPM_FILE}.md5<< EOL0ba3feb1c9ee35d30e5ae683accaf54b ${RPM_FILE}EOLmd5sum --check ${RPM_FILE}.md5The authenticity and integrity of the file are verified if you see thefollowing output.
mysql80-community-release-el8-2.noarch.rpm: OK
Update the package manager to include MySQL.
sudo rpm -iUvh ${RPM_FILE}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
Start MySQL server.
sudo /usr/bin/systemctl start mysqld
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
Connect to MySQL using the MySQL client.
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.sudo mysql -u root -p
When you connect to MySQL, the prompt changes to
mysql>.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)When you are done running commands, use the
exitcommand to quit out of theMySQL client, and then useexitagain to sign out of theCompute Engine instance.mysql>exit
Bye
MySQL 5.7
Connect to MySQL using the MySQL client.
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.sudo mysql -u root -p
When you connect to MySQL, the prompt changes to
mysql>.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)
When you are done running commands, use the
exitcommand to quit out of theMySQL client, and then useexitagain 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.