MySQL-手动离线安装MySQL5.7(rpm)

说明

  • Linux下MySQL5.7手动离线安装(rpm)

  • 包括:安装MySQL5.7、登陆、远程访问、开机自启动、设置字符集、设置大小写规范等

  • MySQL版本:5.7.36

  • Linux版本:CentOS7.7.1908

  • 关于rpm安装的性能问题的讨论(来自网络)

    理论上源码静态编译方式安装效率会高一点,但具体比rpm方式安装高多少是得看具体情况的,一般情况下能超过5%就很不错了~~
    我感觉现在对于大多数环境,没有必要在这个环节过多纠缠~~
    一般数据库都不是在极限负载环境下长期运行,尤其像mysql这样的产品,如果DBA所管理的数据库性能已经被压榨到需要这3-5%,那我感觉架构设计可能是更大的问题,这种情况下别忙着优化数据库了,想想法子简化架构才是正途~~
    

操作步骤

》下载MySQL

  • 地址:https://downloads.mysql.com/archives/community/

  • 下载的安装包列表(rpm全量build包):

    [root@192 MySQL]# ll
    total 541024
    -rw-r--r--. 1 501 games 545863680 Jan 27 16:26 mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
    
  • 解压后rpm包列表:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# ll
    total 533076
    -rw-r--r--. 1 501 games  26664808 Sep  8 08:34 mysql-community-client-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games    317808 Sep  8 08:34 mysql-community-common-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games   4118740 Sep  8 08:34 mysql-community-devel-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games  47760636 Sep  8 08:34 mysql-community-embedded-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games  23316608 Sep  8 08:34 mysql-community-embedded-compat-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games 132191192 Sep  8 08:34 mysql-community-embedded-devel-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games   2473272 Sep  8 08:34 mysql-community-libs-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games   1263988 Sep  8 08:34 mysql-community-libs-compat-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games 182267476 Sep  8 08:34 mysql-community-server-5.7.36-1.el7.x86_64.rpm
    -rw-r--r--. 1 501 games 125479900 Sep  8 08:35 mysql-community-test-5.7.36-1.el7.x86_64.rpm
    

》开始安装

  • 确认并赋予/tmp目录最高权限:chmod -R 777 /tmp

  • 检查是否有以下两个依赖:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -qa|grep libaio
    libaio-0.3.109-13.el7.x86_64
    
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -qa|grep net-tools
    net-tools-2.0-0.25.20131004git.el7.x86_64
    
  • 准备依次按顺序执行以下安装命令:

    rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-5.7.36-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-5.7.36-1.el7.x86_64.rpm
    rpm -ivh mysql-community-server-5.7.36-1.el7.x86_64.rpm
    
    • 发现安装报错(文件冲突)如下:
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
    warning: mysql-community-common-5.7.36-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
    Preparing...                          ################################# [100%]
    	file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.36-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.64-1.el7.x86_64
    	......
    
    • 错误原因是系统安装了mysql-libs,将其卸载:

      [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# yum remove mysql-libs
      
    • 再次进行安装成功:

      [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
      warning: mysql-community-common-5.7.36-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
      Preparing...                          ################################# [100%]
      Updating / installing...
         1:mysql-community-common-5.7.36-1.e################################# [100%]
      
      [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -ivh mysql-community-libs-5.7.36-1.el7.x86_64.rpm
      warning: mysql-community-libs-5.7.36-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
      Preparing...                          ################################# [100%]
      Updating / installing...
         1:mysql-community-libs-5.7.36-1.el7################################# [100%]
      
      [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -ivh mysql-community-client-5.7.36-1.el7.x86_64.rpm
      warning: mysql-community-client-5.7.36-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
      Preparing...                          ################################# [100%]
      Updating / installing...
         1:mysql-community-client-5.7.36-1.e################################# [100%]
      
      [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -ivh mysql-community-server-5.7.36-1.el7.x86_64.rpm
      warning: mysql-community-server-5.7.36-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
      Preparing...                          ################################# [100%]
      Updating / installing...
         1:mysql-community-server-5.7.36-1.e################################# [100%]
      
      
  • 检查是否安装成功:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# rpm -qa|grep -i mysql
    mysql-community-common-5.7.36-1.el7.x86_64
    mysql-community-client-5.7.36-1.el7.x86_64
    mysql-community-libs-5.7.36-1.el7.x86_64
    mysql-community-server-5.7.36-1.el7.x86_64
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]#
    
  • 查看MySQL版本:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# mysql --version
    mysql  Ver 14.14 Distrib 5.7.36, for Linux (x86_64) using  EditLine wrapper
    # 或
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# mysqladmin --version
    mysqladmin  Ver 8.42 Distrib 5.7.36, for Linux on x86_64
    
  • 服务初始化:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# mysqld --initialize --user=mysql
    
  • 查看密码:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# cat /var/log/mysqld.log
    ...
    2022-01-27T10:18:18.423719Z 1 [Note] A temporary password is generated for root@localhost: dri25jrZA0
  • 启动、停止、重启、查看MySQL服务:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl start mysqld
    
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl stop mysqld
    
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl restart mysqld
    
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl status mysqld
    
  • 查看进程:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# ps -ef |grep -i mysql
    
  • 查看是否开机自启动(默认安装完已经加入开机自启动):

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl list-unit-files|grep mysqld.service
    mysqld.service                                enabled
    
  • 手动关闭、开启MySQL开机自启动:

    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl disable mysqld.service
    
    [root@192 mysql-5.7.36-1.el7.x86_64.rpm-bundle]# systemctl enable mysqld.service
    

》登陆

  • 首次登陆(回车后输入刚才查看到的密码,比如本案例是“dri25jrZA0

    [root@192 ~]# mysql -uroot -p
    
  • 修改root默认密码,并设置允许root远程访问

    mysql> set password=password('rootroot');
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> grant all on *.* to root@"%" identified by "rootroot";
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select Host,User from user;
    +-----------+---------------+
    | Host      | User          |
    +-----------+---------------+
    | %         | root          |
    | localhost | mysql.session |
    | localhost | mysql.sys     |
    | localhost | root          |
    +-----------+---------------+
    4 rows in set (0.00 sec)
    
  • 远程连接成功。如果远程连接不上,请关闭防火墙或者开放端口,我这台虚拟机是关闭了防火墙的。相关命令:

    • 关闭防火墙:

      systemctl start firewalld.service 
      systemctl status firewalld.service 
      systemctl stop firewalld.service 
      systemctl enable firewalld.service
      systemctl disable firewalld.service
      
    • 开放端口:

      firewall-cmd --list-all
      firewall-cmd --add-service=http --permanent
      firewall-cmd --add-port=3306/tcp --permanent
      firewall-cmd --reload
      

》修改默认字符集

  • 查看默认字符集:

    mysql> show variables like 'character%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)
    
    • MySQL5.7以及以下版本默认字符集latin1 不支持中文,建表语句如果不设置字符集,默认就是latin1,保险起见还是手动改一下配置。而MySQL5.8默认已经是utf8mb4
  • 修改默认字符集为utf8mb4

    [root@192 ~]# vi /etc/my.cnf
    
    # 在[mysqld]节点下加入下边这一行配置
    character_set_server=utf8
    
  • 重启MySQL服务:

    [root@192 ~]# systemctl restart mysqld
    
  • 重新登录MySQL并查看字符集,已经全部改为utf8:

    mysql> show variables like "%char%";
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | utf8                       |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | utf8                       |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)
    

》设置大小写规范

  • 默认情况下,windows系统默认大小写不敏感 ,但是 linux系统是大小写敏感的 。

  • 查看状态:

    mysql> show variables like '%lower_case_table_names%';
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | lower_case_table_names | 0     |
    +------------------------+-------+
    1 row in set (0.01 sec)
    
  • 更改状态(可选,根据你的项目需要,比如带有大小写字母的订单号字段):

    [root@192 ~]# vi /etc/my.cnf
    
    # 在[mysqld]下加入下边这一行配置(0=大小写敏感,1=大小写不敏感)
    lower_case_table_names=1
    

你可能感兴趣的:(Linux,MySQL,mysql,数据库)