使用二进制安装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,新密码必须结合复杂性要求。如果在测试环境中,可以禁用此插件。

2. 执行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> 

3. 退出“mysql>”操作环境

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

mysql> EXIT
Bye
[root@Centos7-02 ~]#

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值