在 Azure 中运行 OpenSUSE Linux 的虚拟机上安装 MySQLInstall MySQL on a virtual machine running OpenSUSE Linux in Azure

MySQL 是一种常用的开源 SQL 数据库。MySQL is a popular, open-source SQL database. 本教程介绍如何创建运行 OpenSUSE Linux 的虚拟机,然后安装 MySQL。This tutorial shows you how to create a virtual machine running OpenSUSE Linux, then install MySQL.

Note

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

如果选择在本地安装并使用 CLI,则需使用 Azure CLI 2.0 或更高版本。If you choose to install and use the CLI locally, you need Azure CLI version 2.0 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建运行 OpenSUSE Linux 的虚拟机Create a virtual machine running OpenSUSE Linux

首先创建一个资源组。First, create a resource group. 在此示例中,资源组名为 mySQSUSEResourceGroup,并在“中国东部”区域创建。In this example, the resource group is named mySQSUSEResourceGroup and it is created in the China East region.

az group create --name mySQLSUSEResourceGroup --location chinaeast

创建 VM。Create the VM. 在此示例中,VM 名为 myVM,VM 大小为 Standard_D2s_v3,但应选择你认为最适合你的工作负荷的 VM 大小In this example, the VM is named myVM and the VM size is Standard_D2s_v3, but you should choose the VM size you think is most appropriate for your workload.

az vm create --resource-group mySQLSUSEResourceGroup \
   --name myVM \
   --image openSUSE-Leap \
   --size Standard_D2s_v3 \
   --generate-ssh-keys

还需要向网络安全组添加一条规则以允许流量通过 MySQL 的端口 3306。You also need to add a rule to the network security group to allow traffic over port 3306 for MySQL.

az vm open-port --port 3306 --resource-group mySQLSUSEResourceGroup --name myVM

连接到 VMConnect to the VM

将使用 SSH 连接到 VM。You'll use SSH to connect to the VM. 在本示例中,VM 的公共 IP 地址为 10.111.112.113In this example, the public IP address of the VM is 10.111.112.113. 创建 VM 时,可以在输出中看到 IP 地址。You can see the IP address in the output when you created the VM.

ssh 10.111.112.113

更新 VMUpdate the VM

连接到 VM 后,安装系统更新和修补程序。After you're connected to the VM, install system updates and patches.

sudo zypper update

按照提示操作以更新 VM。Follow the prompts to update your VM.

安装 MySQLInstall MySQL

通过 SSH 在 VM 中安装 MySQL。Install the MySQL in the VM over SSH. 根据需要回复提示。Reply to prompts as appropriate.

sudo zypper install mysql

将 MySQL 设置为在系统启动时启动。Set MySQL to start when the system boots.

sudo systemctl enable mysql

验证是否已启用 MySQL。Verify that MySQL is enabled.

systemctl is-enabled mysql

这应返回:已启用。This should return: enabled.

重新启动服务器。Restart the server.

sudo reboot

MySQL 密码MySQL password

在安装后,MySQL 根密码默认为空。After installation, the MySQL root password is empty by default. 运行 mysql_secure_installation 脚本来保护 MySQL。Run the mysql_secure_installation script to secure MySQL. 该脚本会提示更改 MySQL 根密码、删除匿名用户帐户、禁用远程根登录、删除测试数据库以及重新加载特权表。The script prompts you to change the MySQL root password, remove anonymous user accounts, disable remote root sign in, remove test databases, and reload the privileges table.

服务器重新启动后,重新通过 ssh 连接到 VM。Once the server reboots, ssh to the VM again.

ssh 10.111.112.113
mysql_secure_installation

登录到 MySQLSign in to MySQL

现在,可以登录并输入 MySQL 提示符。You can now sign in and enter the MySQL prompt.

mysql -u root -p

它可切换到 MySQL 提示符,你可以在其中发出用于与数据库交互的 SQL 语句。This switches you to the MySQL prompt where you can issue SQL statements to interact with the database.

现在,新建 MySQL 用户。Now, create a new MySQL user.

CREATE USER 'mysqluser'@'localhost' IDENTIFIED BY 'password';

行尾的分号 (;) 对于结束命令很重要。The semi-colon (;) at the end of the line is crucial for ending the command.

创建数据库Create a database

创建数据库,并授予 mysqluser 用户权限。Create a database and grant the mysqluser user permissions.

CREATE DATABASE testdatabase;
GRANT ALL ON testdatabase.* TO 'mysqluser'@'localhost' IDENTIFIED BY 'password';

数据库用户名和密码仅由连接到数据库的脚本使用。Database user names and passwords are only used by scripts connecting to the database. 数据库用户帐户名称不一定表示系统上的实际用户帐户。Database user account names do not necessarily represent actual user accounts on the system.

允许从另一台计算机登录。Enable sign in from another computer. 在此示例中,允许从其登录的计算机的 IP 地址是 10.112.113.114In this example, the IP address of the computer to allow sign in from is 10.112.113.114.

GRANT ALL ON testdatabase.* TO 'mysqluser'@'10.112.113.114' IDENTIFIED BY 'password';

若要退出 MySQL 数据库管理实用程序,请键入:To exit the MySQL database administration utility, type:

quit

后续步骤Next steps

有关 MySQL 的详细信息,请参阅 MySQL 文档For details about MySQL, see the MySQL Documentation.