• MySQL的二进制安装


    MySQL 的安装方式除了常规的源码编译安装之外,最常用的还包括 YUM 方式安装和二进 制方式安装。二进制安装方式中,包括 rpm 版本以及 glibc 版本。rpm 版本就是在特定 Linux 版本下编译的,如果你的 Linux 版本匹配,就可以安装。如下载 CentOS7 系统所对应编译好 的 rpm 包安装即可。另外一种二进制安装包是基于特定的 glibc 版本编译的,这里主要讲解 基于 glibc 方式安装 MySQL。

    1、基础环境准备 

    如果采用 CentOS 7 minimal 安装的系统,在使用前需要安装一些基础软件包工具。 

    [root@centos7-2 ~]# yum -y install gcc vim wget net-tools lrzsz
    

    安装 MySQL 依赖的软件包。

    [root@centos7-2 ~]# yum -y install libaio
    

    创建运行 MySQL 程序的用户。

    [root@centos7-2 ~]# useradd -M -s /sbin/nologin mysql

    关闭 SELinux 和防火墙。

    1. [root@centos7-2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    2. [root@centos7-2 ~]# setenforce 0
    3. [root@centos7-2 ~]# systemctl disable firewalld
    4. [root@centos7-2 ~]# systemctl stop firewalld

    2、二进制的安装 

    二进制安装的版本采用跟上面编译安装的版本一样 MySQL 5.7.28。首先需要下载该软件包或者提前上传,然后再解压进行配置。 

    1. [root@centos7-2 ~]# tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
    2. [root@centos7-2 ~]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
    3. [root@centos7-2 ~]# mkdir /usr/local/mysql/data
    4. [root@centos7-2 ~]# chown -R mysql:mysql /usr/local/mysql/data
    5. [root@centos7-2 ~]# cd /usr/local/mysql/bin/
    6. [root@centos7-2 bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
    7. 2024-03-01T06:04:39.963125Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    8. 2024-03-01T06:04:40.364895Z 0 [Warning] InnoDB: New log files created, LSN=45790
    9. 2024-03-01T06:04:40.463491Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    10. 2024-03-01T06:04:40.480757Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 975de679-d791-11ee-a727-000c29a65de7.
    11. 2024-03-01T06:04:40.482007Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    12. 2024-03-01T06:04:41.282835Z 0 [Warning] CA certificate ca.pem is self signed.
    13. 2024-03-01T06:04:41.482225Z 1 [Note] A temporary password is generated for root@localhost: _*G,q=kZo8f0

    3、设定配置文件

    MySQL 的配置文件跟编译安装的配置文件类似。 

    1. [root@centos7-2 ~]# vim /etc/my.cnf
    2. [client]
    3. socket=/usr/local/mysql/data/mysql.sock
    4. [mysqld]
    5. socket=/usr/local/mysql/data/mysql.sock
    6. bind-address = 0.0.0.0
    7. skip-name-resolve
    8. port = 3306
    9. basedir=/usr/local/mysql
    10. datadir=/usr/local/mysql/data
    11. max_connections=2048
    12. character-set-server=utf8
    13. default-storage-engine=INNODB
    14. lower_case_table_names=1
    15. max_allowed_packet=16M

    将 MySQL 的可执行文件写入环境变量中。

    1. [root@centos7-2 ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    2. [root@centos7-2 ~]# . /etc/profile //使配置在当前shell中生效

    4、配置 systemctl 方式启动。 

    将 MySQL 添加成为系统服务,通过使用 systemctl 来管理。 在 /usr/local/mysql/support-files 目 录下找到 mysql.server 文件,将其复制到 /etc/rc.d/init.d 目录下,改名为 mysqld 并赋予可执行权限。

    1. [root@centos7-2 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
    2. [root@centos7-2 ~]# chmod +x /etc/rc.d/init.d/mysqld

    编辑生成 mysqld.service 服务,通过 systemctl 方式来管理。

    1. [root@centos7-2 ~]# vim /lib/systemd/system/mysqld.service
    2. [Unit]
    3. Description=mysqld
    4. After=network.target
    5. [Service]
    6. Type=forking
    7. ExecStart=/etc/rc.d/init.d/mysqld start
    8. ExecReload=/etc/rc.d/init.d/mysqld restart
    9. ExecStop=/etc/rc.d/init.d/mysqld stop
    10. PrivateTmp=true
    11. [Install]
    12. WantedBy=multi-user.target
    13. [root@centos7-2 ~]# systemctl daemon-reload
    14. [root@centos7-2 ~]# systemctl enable mysqld
    15. Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
    16. [root@centos7-2 ~]# systemctl start mysqld
    17. [root@centos7-2 ~]# netstat -anpt | grep mysqld
    18. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 66686/mysqld

    5、访问 MySQL 数据库 

    MySQL 数据库系统也是一个典型的 C/S(客户端/服务器)架构的应用,要访问 MySQL 数据库需要使用专门的客户端软件。在 Linux 系统中,最简单、易用的 MySQL 客户端软件是其自带的 mysql 命令工具。 

    登录到 MySQL 服务器

    经过安装后的初始化过程,MySQL 数据库的默认管理员用户名为“root”,密码为给定 的随机密码。以 root 用户登录本机的 MySQL 数据库,可以执行以下操作 

    1. [root@centos7-2 ~]# mysql -u root -p
    2. Enter password: //根据提示输入上述的随机的密码,然后修改密码
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 8
    5. Server version: 5.7.28
    6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql>
    12. ysql>
    13. mysql> set password=password('Cisco@123'); //单引号内为新密码
    14. Query OK, 0 rows affected, 1 warning (0.01 sec)
    15. mysql>

    MySQL5.7 版本默认启用了密码增强插件 validate_password,新密码必须符合密码复杂性要求。如果在测试环境中,可以禁用此插件。 

    6、执行 MySQL 操作语句

    验证成功以后将会进入提示符为“mysql> ”的数据库操作环境,用户可以输入各种操 作语句对数据库进行管理。每一条 MySQL 操作语句以分号“;”表示结束,输入时可以不区分大小写,但习惯上将 MySQL 语句中的关键字部分大写。

    例如,以用户名 root 登录到“mysql>”环境后,执行“SHOW DATABASES;”语句可以查 看当前数据库中有哪些库。

    1. [root@centos7-2 ~]# mysql -u root -pCisco@123
    2. mysql> SHOW DATABASES;
    3. +--------------------+
    4. | Database |
    5. +--------------------+
    6. | information_schema |
    7. | mysql |
    8. | performance_schema |
    9. | sys |
    10. +--------------------+
    11. 4 rows in set (0.00 sec)
    12. mysql>

    退出“mysql>”操作环境。

    在“mysql> ”操作环境中,执行“EXIT”或“QUIT”命令便可以退出 mysql 命令工具, 返回原来的 Shell 环境。

    1. mysql> exit
    2. Bye
    3. [root@centos7-2 ~]#
  • 相关阅读:
    SPring Boot整合第三方框架
    Spring IoC容器初始化过程-BeanDefinition的解析和载入
    手把手教你语音识别(二)
    6.如何判断数据库搜索是否走索引?
    多目标水循环优化算法附Matlab代码
    HaaS506-HD1 (RTU) - 开发板介绍
    c# iot .net6 树莓派读取土壤湿度感应器 代码实例
    Hadoop版本演变、分布式集群搭建
    VSCode 的 Remote-SSH 免密登陆
    SpringBoot实现登录拦截器
  • 原文地址:https://blog.csdn.net/2301_77081516/article/details/136394255