我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!


喜讯:国内、香港、海外云服务器租用特惠活动,2核/4G/10M仅需31元每月,点击抢购>>>

点击这里点击这里进入阿里云ECS官方页面,立即免费体验云服务器ECS>>>

点击这里阿里云老客户点击这里申请VIP账号,享终身8折优惠(续费也打折)>>>

MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言—结构化查询语言(SQL)进行数据库管理。本文介绍如何在Linux实例中部署MySQL数据库。

手动部署MySQL数据库(Alibaba Cloud Linux 3/2、CentOS 7.x)

手动部署MySQL时,已有ECS实例必须满足以下条件:

  • 实例已分配公网IP地址或绑定弹性公网IP(EIP)。

  • 操作系统:CentOS 7.x、CentOS 8.x、Alibaba Cloud Linux 2、Alibaba Cloud Linux 3。

  • 实例安全组的入方向规则已放行22、80、443、3306端口。具体操作,请参见添加安全组规则。

步骤一:安装MySQL

  1. 远程连接ECS实例。

    具体操作,请参见通过密码或密钥认证登录Linux实例。

  2. 运行以下命令,更新YUM源。

     

    sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
  3. (可选)当操作系统为Alibaba Cloud Linux 3时,请执行如下命令,安装MySQl所需库文件。

     

    sudo rpm -Uvh https://mirrors.aliyun.com/alinux/3/updates/x86_64/Packages/compat-openssl10-1.0.2o-4.0.1.al8.x86_64.rpm


  4. 运行以下命令,安装MySQL。

     

    sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
  5. 运行以下命令,查看MySQL版本号。

     

    mysql -V

    返回结果如下,表示MySQL安装成功。

     

    mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

步骤二:配置MySQL

  1. 运行以下命令,启动并设置开机自启动MySQL服务。

     

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  2. 运行以下命令,获取并记录root用户的初始密码。

     

    sudo grep 'temporary password' /var/log/mysqld.log

    执行命令结果示例如下。

     

    2022-02-14T09:27:18.470008Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r_V&f2wyu_vI

    说明

    示例末尾的r_V&f2wyu_vI为初始密码,后续在对MySQL进行安全性配置时,需要使用该初始密码。

  3. 运行以下命令,对MySQL进行安全性配置。

     

    sudo mysql_secure_installation

    安全性配置的更多信息,请参见MySQL官方文档。

    1. 根据提示信息,重置MySQL数据库root用户的密码。

      说明

      在输入密码时,系统为了最大限度地保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。

       

      Enter password for user root: #输入已获取的root用户初始密码
      
      The existing password for the user account root has expired. Please set a new password.
      
      New password: #输入新的MySQL密码
      
      Re-enter new password: #重复输入新的MySQL密码
      The 'validate_password' component is installed on the server.
      The subsequent steps will run with the existing configuration
      of the component.
      Using existing password for root.
      Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。
      
      New password: #输入新的MySQL密码
      
      Re-enter new password: #重复输入新的MySQL密码
      
      Estimated strength of the password: 100
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #输入Y确认使用已设置的密码。
    2. 根据提示信息,删除匿名用户。

       

      By default, a MySQL installation has an anonymous user,
      allowing anyone to log into MySQL without having to have
      a user account created for them. This is intended only for
      testing, and to make the installation go a bit smoother.
      You should remove them before moving into a production
      environment.
      
      Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y #输入Y删除MySQL默认的匿名用户。
      Success.
    3. 禁止root账号远程登录。

       

      Normally, root should only be allowed to connect from
      'localhost'. This ensures that someone cannot guess at
      the root password from the network.
      
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y #输入Y禁止root远程登录。
      Success.
    4. 删除test库以及对test库的访问权限。

       

      By default, MySQL comes with a database named 'test' that
      anyone can access. This is also intended only for testing,
      and should be removed before moving into a production
      environment.
      
      
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y #输入Y删除test库以及对test库的访问权限。
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
    5. 重新加载授权表。

       

      Reloading the privilege tables will ensure that all changes
      made so far will take effect immediately.
      
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y #输入Y重新加载授权表。
      Success.
      
      All done!

步骤三:远程访问MySQL数据库

建议您使用非root账号远程登录MySQL数据库。下文示例中,将创建新的MySQL用户账户,用于远程访问MySQL。

  1. 远程连接ECS实例。

    关于连接方式的介绍,请参见连接方式概述。

  2. 运行以下命令后,输入root用户的密码登录MySQL。

     

    sudo mysql -uroot -p
  3. 依次运行以下命令,创建远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。

    本示例账号为dmsTest、密码为Ecs@123****

    重要

    实际创建账号时,需将示例密码Ecs@123****更换为符合要求的密码,并妥善保存。密码要求:长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:

    ()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/

     

    #创建数据库用户dmsTest,并授予远程连接权限。create user 'dmsTest'@'%' identified by 'Ecs@123****'; 
    #为dmsTest用户授权数据库所有权限。grant all privileges on *.* to 'dmsTest'@'%'; 
    #刷新权限。flush privileges;
  4. 执行以下命令,退出数据库。

     

    exit
  5. 使用dmsTest账号远程登录MySQL。

    • (推荐)您可以通过阿里云提供的数据管理服务DMS(Data Management Service)来远程访问MySQL数据库。具体操作,请参见云数据库录入。

    • 您可以通过MySQL客户端远程登录MySQL进行测试。例如:MySQL Workbench、Navicat。

使用云市场镜像部署MySQL数据库

  1. 登录ECS管理控制台。

  2. 在左侧导航栏,选择实例与镜像 > 实例

  3. 在页面左侧顶部,选择目标资源所在的资源组和地域。地域

  4. 实例列表页面,单击创建实例

  5. 自定义购买页面的镜像区域,单击云市场镜像,然后单击从云市场获取更多选择(含操作系统)

    image.png

  6. 镜像市场对话框,选择MySQL镜像。

    1. 在左侧导航栏单击推荐:TOP云智能建站优惠活动,仅880元即可搭建一个后台管理五端合一的智能网站(PC网站、手机网站、百度智能小程序、微信小程序、支付宝小程序),独享百度搜索SEO优势资源,让你的网站不仅有颜值有排面,更有排名,可以实实在在为您带来效益,请点击进入TOP云智能建站>>>,或咨询在线客服了解详情。



我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!

在线客服
联系方式

热线电话

131-3501-0006

上班时间

周一到周五

公司电话

130-0743-9476

二维码
线