MySQL的二进制安装

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

1、基础环境准备

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

cpp 复制代码
[root@centos7-2 ~]# yum -y install gcc vim wget net-tools lrzsz

安装 MySQL 依赖的软件包。

cpp 复制代码
[root@centos7-2 ~]# yum -y install libaio

创建运行 MySQL 程序的用户。

cpp 复制代码
[root@centos7-2 ~]# useradd -M -s /sbin/nologin mysql

关闭 SELinux 和防火墙。

cpp 复制代码
[root@centos7-2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@centos7-2 ~]# setenforce 0
[root@centos7-2 ~]# systemctl disable firewalld
[root@centos7-2 ~]# systemctl stop firewalld

2、二进制的安装

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

cpp 复制代码
[root@centos7-2 ~]# tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 
[root@centos7-2 ~]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
[root@centos7-2 ~]# mkdir /usr/local/mysql/data
[root@centos7-2 ~]# chown -R mysql:mysql /usr/local/mysql/data
[root@centos7-2 ~]# cd /usr/local/mysql/bin/
[root@centos7-2 bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
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).
2024-03-01T06:04:40.364895Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-03-01T06:04:40.463491Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
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.
2024-03-01T06:04:40.482007Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-03-01T06:04:41.282835Z 0 [Warning] CA certificate ca.pem is self signed.
2024-03-01T06:04:41.482225Z 1 [Note] A temporary password is generated for root@localhost: _*G,q=kZo8f0

3、设定配置文件

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

cpp 复制代码
[root@centos7-2 ~]# vim /etc/my.cnf
[client]
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
socket=/usr/local/mysql/data/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
port = 3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections=2048
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M

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

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

4、配置 systemctl 方式启动。

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

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

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

cpp 复制代码
[root@centos7-2 ~]# vim /lib/systemd/system/mysqld.service
[Unit]
Description=mysqld
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/mysqld start
ExecReload=/etc/rc.d/init.d/mysqld restart
ExecStop=/etc/rc.d/init.d/mysqld stop

PrivateTmp=true

[Install]
WantedBy=multi-user.target

[root@centos7-2 ~]# systemctl daemon-reload 
[root@centos7-2 ~]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@centos7-2 ~]# systemctl start mysqld
[root@centos7-2 ~]# netstat -anpt | grep mysqld
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 数据库,可以执行以下操作

cpp 复制代码
[root@centos7-2 ~]# mysql -u root -p
Enter password:   //根据提示输入上述的随机的密码,然后修改密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.28

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
ysql> 
mysql> set password=password('Cisco@123');  //单引号内为新密码
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 

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

6、执行 MySQL 操作语句

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

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

cpp 复制代码
[root@centos7-2 ~]# mysql -u root -pCisco@123


mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

退出"mysql>"操作环境。

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

cpp 复制代码
mysql> exit
Bye
[root@centos7-2 ~]# 
相关推荐
DONG91310 分钟前
《三驾马车:MySQL、MongoDB、Redis对比与融合实战》
数据库·redis·sql·mysql·mongodb·database
程序边界35 分钟前
从 Oracle 到 KingbaseES:企业信创改造的“抄作业”模板,直接套用!
数据库·oracle
funfan05171 小时前
奇怪的“bug”--数据库的“隐式转换”行为
数据库·bug
Jasonakeke1 小时前
【重学MySQL】八十八、8.0版本核心新特性全解析
android·数据库·mysql
comeoffbest1 小时前
PostgreSQL 能存万物:从安装到高级功能实战
数据库·postgresql
时序数据说1 小时前
IoTDB如何解决海量数据存储难题?
大数据·数据库·物联网·时序数据库·iotdb
小楓12013 小时前
MySQL數據庫開發教學(二) 核心概念、重要指令
开发语言·数据库·mysql
花果山总钻风3 小时前
MySQL奔溃,InnoDB文件损坏修复记录
数据库·mysql·adb
TDengine (老段)4 小时前
TDengine IDMP 运维指南(管理策略)
大数据·数据库·物联网·ai·时序数据库·tdengine·涛思数据
Full Stack Developme4 小时前
PostgreSQL interval 转换为 int4 (整数)
数据库·postgresql