安装mysql5.7

安装mysql5.7

  1. 在aliyun机器上安装和配置mysql

  2. 在安装MySQL服务的时候

    [root@localhost ~]# yum -y install mysql-community-server
    
  3. 遇到以下报错

    Downloading packages:
    warning: /var/cache/yum/x86_64/2.1903/mysql57-community/packages/mysql-community-common-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
    Public key for mysql-community-common-5.7.37-1.el7.x86_64.rpm is not installed
    (1/4): mysql-community-common-5.7.37-1.el7.x86_64.rpm                                                  | 311 kB  00:00:01
    (2/4): mysql-community-libs-5.7.37-1.el7.x86_64.rpm                                                    | 2.4 MB  00:00:01
    (3/4): mysql-community-client-5.7.37-1.el7.x86_64.rpm                                                  |  25 MB  00:00:08
    (4/4): mysql-community-server-5.7.37-1.el7.x86_64.rpm                                                  | 174 MB  00:00:42
    ------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                         4.4 MB/s | 202 MB  00:00:45
    Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    Importing GPG key 0x5072E1F5:
     Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
     Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
     Package    : mysql57-community-release-el7-10.noarch (@/mysql57-community-release-el7-10.noarch)
     From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
    
     Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
     GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
  4. 需要需要以下yum文件,指定某个版本

    vi /etc/yum.repos.d/mysql-community.repo
    
    # Enable to use MySQL 5.6
    [mysql56-community]
    name=MySQL 5.6 Community Server
    
    baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
    enabled=1
    gpgcheck=1
    # gpgcheck=0表示默认此版本
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    

启动MySQL

  1. 启动MySQL服务systemctl start mysqld.service

  2. 重启systemctl restart mysqld.service

  3. 停止systemctl stop mysqld.service

  4. 查看状态systemctl status mysqld.service

  5. 配置MySQL的开机启动systemctl enable mysqld

  6. 刷新配置生效systemctl daemon-reload


登陆MySQL

  1. 第一次登陆,先查看内置密码

    grep "password" /var/log/mysqld.log
    

    image.png

  2. root登陆mysql -u root -p

    image.png

    • 需要进行修改密码
    SET PASSWORD = PASSWORD('密码');
    
    mysql> set global validate_password_policy=0;  //改变密码等级
    mysql> set global validate_password_length=4;   //改变密码最小长度
    
  3. 配置远程登陆

    MySQL默认root用户只能本地登录,如果要远程连接,要简单设置下,这里直接用root来远程登录不添加其他角色。

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1234' WITH GRANT OPTION;
    
    1. *.*的意思是所有库的所有表
    2. To后面跟的是用户名
    3. @后面跟的是ip地址
    4. %代表所有ip地址
    5. identified by后面的是密码
    flush privileges;
    

    需要注意mysql的配置文件中的bindaddress 的参数和skip-networking配置

    bindaddress : 设定哪些ip地址被配置,使得mysql服务器只回应哪些ip地址的请求),最好注释掉该参数或设置成为127.0.0.1以外的值

    skip-networking : 如果设置了该参数项,将导致所有TCP/IP端口没有被监听,也就是说出了本机,其他客户端都无法用网络连接到本mysql服务器,所以应该注释掉该参数


添加端口

  1. 添加3306端口

    firewall-cmd --zone=public --add-port=3306/tcp --permanent;
    

    image.png

  2. 查看防火墙状态systemctl status firewalld

  3. 打开防火墙systemctl start firewalld

    image.png

  4. 重新启用3306端口

    firewall-cmd --zone=public --add-port=3306/tcp --permanent;
    
  5. 重启防火墙firewall-cmd --reload


收尾动作

  1. 查看MySQL的配置文件cat /etc/my.cnf

    [root@blue-ocean tmp]# cat /etc/my.cnf
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    
    [mysqld]
    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    
  2. vim /etc/my.cnf

    [mysqld] 
    
    character_set_server=utf8
    init_connect='SET NAMES utf8'
    
  3. 默认配置文件路径

    /etc/my.cnf #这是mysql的主配置文件
    /var/lib/mysql #mysql数据库的数据库文件存放位置
    /var/log mysql #数据库的日志输出存放位置
    
posted @ 2022-03-20 11:49  蔚蓝的海洋  阅读(94)  评论(0编辑  收藏  举报