Yum/rpm包安装,简单、快速、无法定制化、新手推荐使用
二进制安装,解压缩后直接简单配置即可使用,速度较快,专业DBA常用
源码编译安装,特点是可以定制化安装需求,缺点过程较为复杂
二进制安装
- 创建mysql用户
[root@localhost ~]# useradd -s /sbin/nologin mysql
因为这个用户只是用来进行管理,为了系统安全,配置无登录权限
- 下载mysql二进制软件包(本案例为mysql5.7.33版本)
[root@localhost ~]# mkdir /tools
[root@localhost ~]# cd /tools/
[root@localhost tools]# wget https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
创建tools目录是将下载的文件进行存放和解压
- 解压二进制文件并移动到/opt目录下
[root@localhost tools]# tar -zxf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@localhost tools]# mv mysql-5.7.33-linux-glibc2.12-x86_64 /opt/mysql-5.7.33
为了后续方便操作,可以生成一个软连接
[root@localhost opt]# ln -s /opt/mysql-5.7.33/ /opt/mysql
- 卸载centos7自带的mariadb库,防止冲突
[root@localhost opt]# rpm -e --nodeps mariadb-libs
- 创建mysql配置文件
[root@localhost opt]# vim /etc/my.cnf
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
server_id=1
port=3306
log_error=/opt/mysql/data/mysql_err.log
[mysql]
socket=/tmp/mysql.sock
初始化MySQL
- 查看系统自带的centos7 mariadb-libs
[root@localhost ~]# rpm -qa mariadb-libs
- 安装mysql的依赖环境
[root@localhost ~]# yum install libaio-devel -y
- 创建mysql数据文件夹且授权给mysql用户及用户组
[root@localhost ~]# mkdir -p /opt/mysql/data
[root@localhost ~]# chown -R mysql.mysql /opt/mysql/
- 初始化mysql数据库
[root@localhost ~]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/
- –user=mysql 指定用户
- –basedir 指定mysql安装目录
- –datadir=/opt/mysql/data 指定数据文件夹
- –initialize-insecure 关闭mysql安全策略
- –initialize 开启mysql安全模式
配置MySQL客户端
- 注册系统服务,添加环境变量
[root@localhost ~]# vi /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
[root@localhost ~]# vi /etc/profile.d/mysql.sh
export PATH=/opt/mysql-5.7.33/bin:$PATH
- 登出,重新进入系统,检查环境变量
[root@localhost ~]# exit
登出
[root@localhost ~]# echo $PATH
/root/perl5/bin:/opt/Nginx1.22/sbin:/opt/mysql-5.7.33/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
使用MySQL
- 设置root密码
[root@localhost ~]# mysqladmin -uroot password '设置的密码'
- 登录
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/5125