使用二进制安装MySQL

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

1. 基础环境准备

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

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

安装MySQL依赖的安装包。

复制代码
[root@Centos7-02 ~]# yum -y install libaio

创建运行MySQL程序的用户。

复制代码
[root@Centos7-02 ~]# useradd -M -s /sbin/nologin mysql

关闭SELinux和防火墙。

复制代码
[root@Centos7-02 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
[root@Centos7-02 ~]# setenforce 0
setenforce: SELinux is disabled
[root@Centos7-02 ~]# systemctl disable firewalld
[root@Centos7-02 ~]# systemctl stop firewalld

2. 二进制安装

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

复制代码
[root@Centos7-02 ~]# tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 
[root@Centos7-02 ~]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
[root@Centos7-02 ~]# mkdir /usr/local/mysql/data
[root@Centos7-02 ~]# chown -R mysql.mysql /usr/local/mysql/data/
[root@Centos7-02 ~]# cd /usr/local/mysql/bin/
[root@Centos7-02 bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2024-03-05T02:01:49.768193Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-03-05T02:01:49.945168Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-03-05T02:01:49.971598Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-03-05T02:01:50.025255Z 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: 5459d53f-da94-11ee-990b-000c29e552f7.
2024-03-05T02:01:50.025804Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-03-05T02:01:50.625814Z 0 [Warning] CA certificate ca.pem is self signed.
2024-03-05T02:01:50.702974Z 1 [Note] A temporary password is generated for root@localhost: J6hX*jCZRprI       ##默认生成的随机数据库登录密码

3. 设定配置文件

MySQL的配置文件。

复制代码
[root@Centos7-02 ~]# 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的可执行文件写入环境变量中。

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

4. 配置systemctl方式启动

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

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

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

复制代码
[root@Centos7-02 ~]# 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-02 ~]# systemctl daemon-reload 
[root@Centos7-02 ~]# systemctl enable mysqld
[root@Centos7-02 ~]# systemctl start mysqld
[root@Centos7-02 ~]# netstat -tunlp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      8924/mysqld

5. 访问MySQL数据库

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

  1. 登录到MySQL服务器

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

复制代码
[root@Centos7-02 ~]# mysql -u root -p
Enter password:   ##输入刚刚上述随机生成的默认登录密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
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> set password=password('123.123');   ##修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

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

  1. 执行MySQL操作语句

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

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

复制代码
[root@Centos7-02 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.28 MySQL Community Server (GPL)

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

mysql> 
  1. 退出"mysql>"操作环境

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

复制代码
mysql> EXIT
Bye
[root@Centos7-02 ~]#
相关推荐
GJCTYU1 小时前
spring中@Transactional注解和事务的实战理解附代码
数据库·spring boot·后端·spring·oracle·mybatis
MicroTech20251 小时前
微算法科技(NASDAQ: MLGO)探索Grover量子搜索算法,利用量子叠加和干涉原理,实现在无序数据库中快速定位目标信息的效果。
数据库·科技·算法
Code季风1 小时前
SQL关键字快速入门:CASE 实现条件逻辑
javascript·数据库·sql
weixin_478689761 小时前
操作系统【2】【内存管理】【虚拟内存】【参考小林code】
数据库·nosql
九皇叔叔2 小时前
【7】PostgreSQL 事务
数据库·postgresql
kk在加油2 小时前
Mysql锁机制与优化实践以及MVCC底层原理剖析
数据库·sql·mysql
合作小小程序员小小店2 小时前
web网页开发,在线%ctf管理%系统,基于html,css,webform,asp.net mvc, sqlserver, mysql
mysql·sqlserver·性能优化·asp.net·mvc
Kookoos2 小时前
ABP VNext + Cosmos DB Change Feed:搭建实时数据变更流服务
数据库·分布式·后端·abp vnext·azure cosmos
JosieBook2 小时前
【Java编程动手学】Java常用工具类
java·python·mysql
hello 早上好3 小时前
MsSql 其他(2)
数据库·mysql