弹性云服务器 ECS-搭建网站:搭建数据库

时间:2024-10-17 19:43:35

搭建数据库

安装MySQL

本文档以CentOS 7.2操作系统为例安装MySQL。

  1. 远程登录云服务器discuz01,填写用户名和密码。
  2. 依次执行以下命令,安装MySQL。

    wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

    yum -y install mysql57-community-release-el7-10.noarch.rpm

    yum -y install mysql-community-server --nogpgcheck

配置MySQL

  1. 执行以下命令,启动MySQL服务。

    systemctl start mysqld

    systemctl enable mysqld

  2. 查看MySQL运行状态。

    systemctl status mysqld.service

    回显信息如下所示:

    # systemctl status mysqld.service
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2021-08-23 10:54:55  CS T; 7s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
     Main PID: 7873 (mysqld)
       CGroup: /system.slice/mysqld.service
               └─7873 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    
    Aug 23 10:54:49 ecs-adc3-420652-aed6 systemd[1]: Starting MySQL Server...
    Aug 23 10:54:55 ecs-adc3-420652-aed6 systemd[1]: Started MySQL Server.
  3. 执行以下命令,获取安装MySQL时自动设置的root用户密码。

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

    回显如下类似信息。
    2021-08-16T11:33:37.790533Z 1 [Note] A temporary password is generated for root@localhost: ;8nPd29lhs,k
  4. 执行以下命令,并按照回显提示信息进行操作,加固MySQL。

    mysql_secure_installation

    Securing the MySQL server deployment.
    
    Enter password for user root:    #输入上一步骤中获取的安装MySQL时自动设置的root用户密码
    The existing password for the user account root has expired. Please set a new password.
    
    New password:  #设置新的root用户密码
    
    Re-enter new password:   #再次输入密码
    The 'validate_password' plugin is installed on the server.
    The subsequent steps will run with the existing configuration of the plugin.
    Using existing password for root.
    
    Estimated strength of the password: 100
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : N   #是否更改root用户密码,输入N
    
     ... skipping.
    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
    Success.
    
    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   #禁止root远程登录,输入Y
    Success.
    
    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   #是否删除test库和对它的访问权限,输入Y
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    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!
  5. 执行以下命令,再根据提示输入数据库管理员root账号的密码进入数据库。

    mysql -u root -p

  6. 执行以下命令,使用MySQL数据库。

    use mysql;

  7. 执行以下命令,查看用户列表。

    select host,user from user;

    此命令及以下数据库语句均以分号结尾,请勿忽略。

  8. 执行以下命令,刷新用户列表并允许所有IP对数据库进行访问。

    update user set host='%' where user='root' LIMIT 1;

  9. 执行以下命令,强制刷新权限。允许同一子网中设置为允许访问的云服务器通过私有IP对MySQL数据库进行访问。

    flush privileges;

  10. 执行以下命令,退出数据库。

    quit

  11. 执行以下命令,重启MySQL服务。

    systemctl restart mysqld

  12. 执行以下命令,设置开机自动启动MySQL服务。

    systemctl enable mysqld

  13. 执行以下命令,关闭防火墙。

    systemctl stop firewalld.service

  14. 重新查看防火墙状态是否为关闭。

    systemctl status firewalld

support.huaweicloud.com/bestpractice-ecs/zh-cn_topic_0092817020.html