1. MySQL5.7.36
  2. 配置文件:/etc/my.cnf
  3. 数据存储:/var/lib/mysql
  4. 命令文件:/usr/bin和/usr/sbin

一. 安装mysql

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

    1. rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  2. 运行以下命令安装MySQL。

    1. yum -y install mysql-community-server
  3. 运行以下命令查看MySQL版本号。 ```java mysql -V

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

mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using EditLine wrapper

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12457443/1636457349685-87d00712-6225-4d0d-97f1-5d684b2be19f.png#clientId=u9be99bce-4e32-4&from=paste&height=821&id=oShzt&margin=%5Bobject%20Object%5D&name=image.png&originHeight=821&originWidth=1663&originalType=binary&ratio=1&size=125988&status=done&style=none&taskId=uc00c0523-8143-409d-9d16-2c4e4d9fc50&width=1663)
  2. <a name="rPKSB"></a>
  3. ## 二.配置MySQL
  4. 1. 启动MySQL服务
  5. ```java
  6. systemctl start mysqld
  1. 设置MySQL服务开机自启动

    1. systemctl enable mysqld
  2. 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码 ```java grep ‘temporary password’ /var/log/mysqld.log

执行命令结果示例如下。 2021-11-09T11:30:57.823935Z 1 [Note] A temporary password is generated for root@localhost: +nLl4JF4Jdg1

  1. 4. 运行下列命令对MySQL进行安全性配置。
  2. ```java
  3. mysql_secure_installation
  1. 重置root用户密码

    1. Enter password for user root: #输入上一步获取的root用户初始密码
    2. The 'validate_password' plugin is installed on the server.
    3. The subsequent steps will run with the existing configuration of the plugin.
    4. Using existing password for root.
    5. Estimated strength of the password: 100
    6. Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
    7. New password: #输入新密码,长度为830个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
    8. Re-enter new password: #再次输入新密码
    9. Estimated strength of the password: 100
    10. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y #是否继续操作,输入Y
  2. 删除匿名用户账号。

    1. 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.
    2. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
    3. Success.
  3. 禁止root账号远程登录。

    1. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
    2. Success.
  4. 删除test库以及对test库的访问权限。

    1. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
    2. - Dropping test database...
    3. Success.
  5. 重新加载授权表。

    1. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
    2. Success.
    3. All done!

    三.远程访问mysql 数据库

  1. 创建远程登录mysql 账号

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

      1. mysql -uroot -p
    2. 依次运行以下命令创建远程登录MySQL的账号。示例账号为dms、密码为123456

      1. mysql> grant all on *.* to 'ml'@'%' IDENTIFIED BY 'Ml123456..'; #使用root替换ml,可设置为允许root账号远程登录。
      2. mysql> flush privileges;
  2. 登录数据库

阿里云数据库登录需要先设置安全组策略