目录
(1).下载、解压mysql软件包,做软链接mysql到/usr/local
1.MySQL安装方式及选择
1.yum 简单 自动解决依赖(默认源没有数据库). 没法定制
2.二进制 比较简单,没办法定制,软件大. ***** 中小企业(推荐)
3.rpm 复杂 下载 手工解决依赖 没法定制
4.源码编译 特别复杂 编译软件gcc都需要升级,可以定制任何东西. 百度\阿里===>改动源码===>编译==>rpm==>yum
2.MySQL软件获取
官方:
MySQL 官网资源目录https://downloads.mysql.com/archives/community/
国内镜像:
阿里云开源镜像站资源目录https://mirrors.aliyun.com/mysql/
3.MySQL安装前环境检查
(1).MySQL安装硬件环境准备
如果读者没有物理服务器环境,则可以搭建vmware等虚拟机环境学习,相应地则需准备如下内容:
1)VM虚拟机内存最好设置4GB以上。
2)提前下载好要安装的MySQL8.0软件包。
生产物理服务器:
SSD固态+Raid10(Raid5)
内存:64G/128G/256G
(2).安装MySQL前软件环境准备
#1)查看系统环境
[root@mysql8.0 ~]# cat /etc/redhat-release #<==操作系统版本。
CentOS Linux release 7.9.2009 (Core)
#2)关闭selinux
setenforce 0
getenforce
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
#3)卸载mariadb依赖包
yum remove mariadb-libs -y #<==卸载系统已经安装的mariadb依赖包。
#4)安装相关依赖包
yum install ncurses ncurses-devel libaio-devel openssl openssl-devel -y
#5)关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
4.二进制安装MySQL 8.0.26
(1).下载、解压mysql软件包,做软链接mysql到/usr/local
#上传软件包到/opt目录下
[root@mysql8.0 ~]# ll -h /opt/
total 873M
-rw-r--r-- 1 root root 873M Apr 16 10:55 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
#进入到/opt下,解压软件包
[root@mysql8.0 ~]# cd /opt/
[root@mysql8.0 /opt]# tar xfv mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
#创建软链接
[root@mysql8.0 ~]# ln -s /opt/mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
#检查
[root@mysql8.0 ~]# ll /usr/local/mysql
lrwxrwxrwx 1 root root 40 Apr 16 11:05 /usr/local/mysql -> /opt/mysql-8.0.26-linux-glibc2.12-x86_64
#正式环境将mysql的目录移动到方便备份的目录,opt目录下容易被遗忘
### mv /opt/mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
(2). 配置文件(/etc/my.cnf)并授权
#创建简易的配置文件,后面根据需求优化配置文件
cat>/etc/my.cnf<<'EOF'
[mysqld]
user=mysql ##指定用户
basedir=/usr/local/mysql/ ##指数据库软件目录
datadir=/data/3306/data ##初始化后数据存放目录
port=3306 ##端口
socket=/tmp/mysql.sock ##socket目录
[client]
socket=/tmp/mysql.sock
EOF
#2)查看配置文件内容
[root@mysql8.0 ~]# cat /etc/my.cnf
#3)创建mysql用户
#首先检查系统中是没有mysql用户的
[root@mysql8.0 ~]# id mysql
id: mysql: no such user
#以root身份登录到Linux系统中,然后执行如下命令创建mysql用户账号:
[root@mysql8.0 ~]# useradd mysql -s /sbin/nologin -M #创建mysql虚拟用户
[root@mysql8.0 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)
#4)修改所属者
[root@mysql8.0 ~]# chown mysql.mysql /etc/my.cnf
[root@mysql8.0 ~]# ll /etc/my.cnf
-rw-r--r-- 1 mysql mysql 261 Apr 16 11:13 /etc/my.cnf
(3).创建配置文件中所指定的内容(用户、目录、环境变量)
-------用户(上面已创建)-------
#创建mysql用户
#首先检查系统中是没有mysql用户的
[root@mysql8.0 ~]# id mysql
id: mysql: no such user
#以root身份登录到Linux系统中,然后执行如下命令创建mysql用户账号:
[root@mysql8.0 ~]# useradd mysql -s /sbin/nologin -M #创建mysql虚拟用户
[root@mysql8.0 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)
-------目录-------
#创建数据目录
[root@mysql8.0 ~]# mkdir -p /data/3306/data
#修改目录的所属者
[root@mysql8.0 ~]# chown -R mysql:mysql /data/
[root@mysql8.0 ~]# ll -d /data/
drwxr-xr-x 3 mysql mysql 18 Apr 16 11:24 /data/
-------PATH环境变量-------
#添加环境变量
[root@mysql8.0 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
#检查
[root@mysql8.0 ~]# tail -n 1 /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
#引用
[root@mysql8.0 ~]# . /etc/profile
(4).初始化MySQL数据库
#8.0初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
#5.6初始化
/usr/local/mysql56/scripts/mysql_install_db --skip-grant-tables --user=mysql --basedir=/usr/local/mysql56 --datadir=/data/3309/data
#5.7初始化
/usr/local/mysql57/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql57 --datadir=/data/3310/data
温馨提示1 : 不同版本的初始化命令异同
/usr/local/mysql/scripts/mysql_install_db #5.7之前
/usr/local/mysql/bin/mysqld #5.7和8.0初始化
温馨提示2: --initialize和--initialize-insecure区别
--initialize:
会生成一个12位,4种密码复杂度临时管理员密码,第一次登陆需要修改,创建默认数据库并退出。创建一个超级用户,用一个随机的过期密码,并将其存入日志
--initialize-insecure:创建默认数据库并退出。创建一个超级用户并使用空密码
温馨提示3:单词含义解释
--initialize-insecure:不安全的初始化
--initialize:安全的初始化
温馨提示4:初始化数据库原理
初始化数据库的实质就是在数据目录下(/data/3306/data)创建基础的数据库系统的库文件等信息
(5).启动MySQL
#启动方式1:
[root@mysql8.0 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
#启动方式2:
[root@mysql8.0 ~]# sh /usr/local/mysql/support-files/mysql.server start
Starting MySQL.. SUCCESS!
#检查
[root@mysql8.0 ~]# ss -lntup
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1433,fd=3))
tcp LISTEN 0 70 [::]:33060 [::]:* users:(("mysqld",pid=21258,fd=21))
tcp LISTEN 0 128 [::]:3306 [::]:* users:(("mysqld",pid=21258,fd=24))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1433,fd=4))
(6).登录
[root@mysql8.0 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 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.
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> exit
Bye