史上最全的MySQL安装指南(各种踩坑,疑难杂症)

Posted IT邦德

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了史上最全的MySQL安装指南(各种踩坑,疑难杂症)相关的知识,希望对你有一定的参考价值。

前言

mysql 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。 MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。 本文详细介绍了MySQL在Window及Linux环境下安装的各种方法

一、MySQL 安装简介

1.1 Linux 环境安装MySQL

在 Linux 下可以使用如下 3 种方式来安装:
a.RPM (Redhat Package Manage):安装简单,适合初学者学习使用,
一台服务器只能安装一个 MySQL,Red Hat Enterprise Linux / Oracle Linux
b.二进制( Binary Package):安装简单;可以安装到任何路径下,灵活性好;
一台服务器可以安装多个 MySQL,Linux - Generic
c.源码( Source Package):在实际安装的操作系统进行可根据需,要定制编译,最灵活;性能最好;
一台服务器可以安装多个 MySQL,Source Code

1.2 Windows环境安装MySQL

在 Linux 下可以使用如下2 种方式来安装:
a.迷你版安装MySQL,图形化界面引导,安装简单,部署便捷,一台服务器可以部署多个MySQL
b.二进制安装,安装包解压后,命令直接部署,一台服务器可以部署多个MySQL

二、Linux 环境安装各个版本MySQL

注:以下安装均来自客户真实环境Linux服务器部署
详细过程可观看CSDN发布的视频教程:
MySQL数据库安装实战

2.1 Linux环境RPM包安装MySQL

OS环境:CentOS7.3
MySQL版本:8.0.27

2.1.1 MySQL RPM包下载

官网下载地址:
https://dev.mysql.com/downloads/mysql/

2.1.2 yum安装依赖包

本地yum源配置请参考CSDN博客:
Linux配置本地yum源,高效便捷:https://blog.csdn.net/weixin_41645135/article/details/120557997

yum -y install lrzsz wget perl-Digest-MD5
yum -y install numactl
yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make

2.1.3 添加用户及组

[root@jeames ~]# groupadd mysql
删除组:groupdel users
--用户加到组并指定根目录
[root@jeames ~]# useradd -g mysql -d /home/mysql mysql
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
删除用户:userdel mysql
[root@jeames ~]# passwd mysql
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

2.1.4 安装Mysql

[root@jeames ~]# md5sum mysql-8.0.27-1.el8.x86_64.rpm-bundle.tar
edf4d0f95867f62cdcc97b7349bedc59  mysql-8.0.27-1.el8.x86_64.rpm-bundle.tar
[root@jeames ~]# tar -xvf mysql-8.0.27-1.el8.x86_64.rpm-bundle.tar -C /home/mysql/

## 必须安装的四个RPM包
mysql-community-common-8.0.27-1.el8.x86_64.rpm
mysql-community-libs-8.0.27-1.el8.x86_64.rpm
mysql-community-client-8.0.27-1.el8.x86_64.rpm 是客户端的安装包
mysql-community-server-8.0.27-1.el8.x86_64.rpm   是服务端的安装包

## 授权
root@jeames ~]# chown -R mysql:mysql /home/mysql/

## 安装
注:root用户下操作
[root@jeames ~]# cd /home/mysql/
装顺序:包之间相互依赖,所以必须注意安装顺序
✓ 先装 common 
✓ 再装 libs
确保 mariadb 已卸载,centos7 默认支持 mariadb,不支持 mysql,不卸载会出现冲突
✓  再装 client
✓ 最后装 server

[root@jeames mysql]# rpm -ivh  mysql-community-common-8.0.27-1.el8.x86_64.rpm
[root@jeames mysql]# rpm -ivh --force --nodeps mysql-community-libs-8.0.27-1.el8.x86_64.rpm
[root@jeames mysql]# rpm -ivh mysql-community-client-8.0.27-1.el8.x86_64.rpm --force --nodeps
[root@jeames mysql]# rpm -ivh mysql-community-server-8.0.27-1.el8.x86_64.rpm --force --nodeps

2.1.5 初始化

详细的三个坑处理方法请参考以下CSDN博客:
https://blog.csdn.net/weixin_41645135/article/details/121479650
CentOS7.3部署Mysql8.0.27全过程,RPM包安装各种坑的处理```

初始化的时候会有三个坑
第一个抗:
[root@jeames mysql]# mysqld --initialize
mysqld: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: 
No such file or directory

第二个坑:
GLIBCXX版本太低报错

第三个坑:
mysqld: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by mysqld)
mysqld: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by /lib64/libstdc++.so.6)

注意:以上操作之后重启服务器后然后再初始化Mysql
[root@jeames mysql]# mysqld --initialize

2.1.6 启动MySQL

[root@jeames ~]# chown mysql:mysql /var/lib/mysql -R
[root@jeames ~]# systemctl start mysqld
[root@jeames ~]# systemctl status mysqld
[root@jeames ~]# systemctl enable mysqld
[root@jeames ~]# cat /var/log/mysqld.log | grep password

2.1.7 远程登陆

mysql> create user root@'%' identified with mysql_native_password by 'root';
mysql> grant all on *.* to root@'%' with grant option;
mysql> flush privileges;

2.2 Linux环境二进制安装MySQL

OS环境:RHEL7.6
MySQL版本:8.0.27

2.2.1 MySQL二进制包下载

官网下载地址:https://dev.mysql.com/downloads/mysql/

2.2.2 yum安装依赖包

本地yum源配置请参考CSDN博客:
https://blog.csdn.net/weixin_41645135/article/details/120557997
Linux配置本地yum源,高效便捷

[root@rhel76 ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make

2.2.3 卸载MariaDB

[root@rhel76 ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@rhel76 ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

2.2.4 安装 MySQL

## 用户及目录创建
注:可以部署多个实例,通过端口区分root 用户操作:
mkdir -p /mysql/data/mysql3306
mkdir -p /mysql/app/
mkdir -p /mysql/conf/
mkdir -p /mysql/data/mysql3306/pid/
mkdir -p /mysql/data/mysql3306/socket/
mkdir -p /mysql/data/mysql3306/log/
mkdir -p /mysql/data/mysql3306/binlog/
mkdir -p /mysql/data/mysql3306/errlog
mkdir -p /mysql/data/mysql3306/relaylog/
mkdir -p /mysql/data/mysql3306/slowlog/
mkdir -p /mysql/data/mysql3306/tmp/

## 用户及组
[root@rhel76 ~]# groupadd mysql
[root@rhel76 ~]# useradd -g mysql mysql
[root@rhel76 ~]# chown -R mysql:mysql /mysql
[root@rhel76 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.

[root@rhel76 ~]# cat /etc/group | grep mysql
mysql:x:1001:
[root@rhel76 ~]# cat /etc/passwd | grep mysql
mysql:x:1001:1001::/home/mysql:/bin/bash

## 上传软件包并解压
[root@rhel76 ~]# cp mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz /mysql/app
[root@rhel76 ~]# chown -R mysql:mysql /mysql

mysql用户操作
[root@rhel76 ~]# su - mysql
md5 值验证,保证下载到的软件包无破损无木马
[mysql@rhel76 ~]$ cd /mysql/app
[mysql@rhel76 app]$ ll
total 1168588
-rw-r--r-- 1 mysql mysql 1196633756 Nov 24 00:49 mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
[mysql@rhel76 app]$ md5sum mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
0bdd171cb8464ba32f65f7bf58bc9533  mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz

解压软件包并重命名
[mysql@rhel76 app]$ tar xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
[mysql@rhel76 app]$ mv mysql-8.0.27-linux-glibc2.12-x86_64 mysql8.0.27

[mysql@rhel76 app]$ ll
total 1168588
drwxrwxr-x 9 mysql mysql        129 Nov 24 00:54 mysql8.0.27
-rw-r--r-- 1 mysql mysql 1196633756 Nov 24 00:49 mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz

2.2.5 环境变量配置

cat >> /home/mysql/.bash_profile << "EOF"
MYSQL_HOME=/mysql/app/mysql8.0.27
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$MYSQL_HOME/bin
EOF
source ~/.bash_profile
which mysql

2.2.6 创建参数文件

由于是二进制文件安装,数据库参数文件需要自己配置

cat >> /mysql/conf/my3306.cnf << "EOF"
[mysqld]
server_id = 80273306
default-storage-engine= InnoDB
basedir=/mysql/app/mysql8.0.27
datadir=/mysql/data/mysql3306/data/
socket=/mysql/data/mysql3306/socket/mysql.sock
log-error=/mysql/data/mysql3306/log/mysqld.log
pid-file=/mysql/data/mysql3306/pid/mysqld.pid
port=3306
default-time_zone='+8:00'
default_authentication_plugin=mysql_native_password # 加此参数可远程登陆
transaction_isolation=READ-COMMITTED
max_connections=1500
back_log=500
wait_timeout=1800
max_user_connections=800
innodb_buffer_pool_size=1024M
innodb_log_file_size=512M
innodb_log_buffer_size=40M
slow_query_log=ON
long_query_time=5
# log settings #
slow_query_log = ON
slow_query_log_file = /mysql/data/mysql3306/slowlog/slow3306.log
log_error = /mysql/data/mysql3306/errlog/err3306.log
log_error_verbosity = 3
log_bin = /mysql/data/mysql3306/binlog/mysql_bin
log_bin_index = /mysql/data/mysql3306/binlog/mysql_binlog.index
general_log_file = /data/mysql/mysql3306/generallog/general.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
expire_logs_days = 90
binlog_expire_logs_seconds = 2592000      #30d
long_query_time = 2
min_examined_row_limit = 100
log_throttle_queries_not_using_indexes = 1000
innodb_flush_log_at_trx_commit=1
EOF

2.2.7 数据库初始化

mysql用户操作:
mysqld --defaults-file=/mysql/conf/my3306.cnf --initialize --user=mysql --basedir=/mysql/app/mysql8.0.27 --datadir=/mysql/data/mysql3306/data/
注意:以上同一行执行

2.2.8 启动MySQL

[mysql@rhel76 ~]$ mysqld_safe --defaults-file=/mysql/conf/my3306.cnf --user=mysql &
[1] 21406
[mysql@rhel76 ~]$ 2021-11-24T06:14:23.240372Z mysqld_safe Logging to '/mysql/data/mysql3306/errlog/err3306.log'.
2021-11-24T06:14:23.296992Z mysqld_safe Starting mysqld daemon with databases from /mysql/data/mysql3306/data

2.2.9 登陆MySQL

第一次登录 MySQL 时,需要到错误日志下找初始化密码,我这里的密码为:-(Qy5Aj9t,=a
使用 socket 加密码登录进去后,无法查询任何东西,提示先要修改 root 密码。

[mysql@rhel76 ~]$ cat /mysql/data/mysql3306/errlog/err3306.log | grep password

2021-11-24T06:12:14.610657Z 0 [Note] [MY-010309] [Server] Auto generated RSA key files through --sha256_password_auto_generate_rsa_keys are placed in data directory.
2021-11-24T06:12:14.610686Z 0 [Note] [MY-010308] [Server] Skipping generation of RSA key pair through --caching_sha2_password_auto_generate_rsa_keys as key files are present in data directory.
2021-11-24T06:12:14.615367Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: -(Qy5Aj9t,=a
2021-11-24T06:14:26.153958Z 0 [Note] [MY-010308] [Server] Skipping generation of RSA key pair through --sha256_password_auto_generate_rsa_keys as key files are present in data directory.
2021-11-24T06:14:26.153979Z 0 [Note] [MY-010308] [Server] Skipping generation of RSA key pair through --caching_sha2_password_auto_generate_rsa_keys as key files are present in data directory.

[mysql@rhel76 ~]$ mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 

此处通过sock登陆
[mysql@rhel76 ~]$ mysql -uroot -p  -P 3306 -S /mysql/data/mysql3306/socket/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 8
Server version: 8.0.27

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> status
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

2.2.10 修改密码

mysql> alter user root@'localhost' identified by 'root';
Query OK, 0 rows affected (0.01 sec)
mysql> status
--------------mysql  Ver 8.0.27 for Linux on x86_64
 (MySQL Community Server - GPL)
 Connection id:          8Current database:
 Current user:           
 root@localhostSSL:                    
 Not in useCurrent pager:          
 stdoutUsing outfile:          ''Using delimiter:        ;
 Server version:         8.0.27Protocol version:       
 10Connection:             Localhost via UNIX socketServer 
 characterset:    utf8mb4Db     
 characterset:    utf8mb4Client characterset:    utf8mb4Conn.  
 characterset:    utf8mb4UNIX 
 socket:            /mysql/data/mysql3306/socket/mysql.sockBinary data as:         HexadecimalUptime:                 8 min 0 
 secThreads: 2  Questions: 7  
 Slow queries: 0  Opens: 398  Flush tables: 3  
 Open tables: 36  Queries per second avg: 0.014--------------

2.3 Linxu环境源码安装MySQL

OS环境:RHEL7.6
MySQL版本:8.0.27

2.3.1 Mysql源码下载

官网下载地址:https://dev.mysql.com/downloads/mysql/

2.3.2 yum安装依赖包

本地yum源配置请参考CSDN博客:
https://blog.csdn.net/weixin_41645135/article/details/120557997
Linux配置本地yum源,高效便捷

[root@jeames007 ~]# yum -y install lrzsz wget perl-Digest-MD5
[root@jeames007 ~]# yum -y install ncurses-devel libaio-devel
[root@jeames007 ~]# yum -y install cmake gcc gcc-c++
[root@jeames007 ~]# yum install –y openssl openssl-devel ncurses ncurses-devel

2.3.3 系统包升级

详细的系统包升级方法请参考以下CSDN博客:
https://blog.csdn.net/weixin_41645135/article/details/121595959
Linux7.6源码安装Mysql8.0.27,历时1周整理

2.3.4 安装及配置

## 编译

[root@jeames007 ~]# mkdir /soft
[root@jeames007 ~]# mv mysql-boost-8.0.27.tar.gz /soft/
[root@jeames007 ~]# cd /soft

[root@jeames007 soft]# tar -zxf mysql-boost-8.0.27.tar.gz
[root@jeames007 soft]# ll
total 285340
drwxr-xr-x 31 7161 31415      4096 Sep 28 13:22 mysql-8.0.27
-rw-r--r--  1 root root  292184025 Nov 25 15:08 mysql-boost-8.0.27.tar.gz

[root@jeames007 soft]# cd mysql-8.0.27

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DSYSCONFDIR=/etc \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_PARTITION_STORAGE_ENGINE=1 \\
-DWITH_FEDERATED_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_MYISAM_STORAGE_ENGINE=1 \\
-DENABLED_LOCAL_INFILE=1 \\
-DENABLE_DTRACE=0 \\
-DDEFAULT_CHARSET=utf8mb4 \\
-DDEFAULT_COLLATION=utf8mb4_general_ci \\
-DWITH_EMBEDDED_SERVER=1 \\
-DDOWNLOAD_BOOST=1 \\
-DWITH_BOOST=/soft/mysql-8.0.27/boost/boost_1_73_0 \\
-DFORCE_INSOURCE_BUILD=1

[root@jeames007 ~]# make -j4  --此处编译需要1个小时,一定要耐心等待
[root@jeames007 ~]# make install

## 用户及组
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql

## 参数文件
cat > /etc/my.cnf <<"EOF"
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
server_id=80273306
log-bin
skip-name-resolve
character_set_server=utf8mb4
default-time-zone = '+8:00'
log_timestamps = SYSTEM
EOF

## 初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

## 环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /root/.bashrc
source /root/.bashrc

## 启动Mysql
#启动 MySQL
mysqld_safe &
#登录 mysql,默认密码为空
mysql -uroot -p
#关闭 MySQL
mysqladmin -uroot -p shutdown

三、Windows环境安装各个版本MySQL

3.1 Windows环境MSI图形安装MySQL

OS环境:Windows7旗舰版,Service Pack1 64w位操作系统
MySQL版本:8.0.23
安装详细过程请参考以下CSDN博客:
https://blog.csdn.net/weixin_41645135/article/details/115500795
Windows安装Mysql-msi 图形安装

3.1.1 MySQL MSI包下载

官网下载地址:https://downloads.mysql.com/archives/community/

3.1.2 安装MySQL


3.1.3 客户端工具连接

安装包:Navicat Premium 12 绿色破解版.zip ,解压即可用

3.2 Windows环境二进制安装MySQL

OS环境:Server2012操作系统
MySQL版本:8.0.23

3.2.1 MySQL二进制包下载

官网下载链接:https://downloads.mysql.com/archives/installer/

3.2.2 初始化

G:\\mysql-8.0.23-winx64\\bin\\mysqld --initialize --user=mysql --console --basedir=G:\\mysql-8.0.23-winx64 \\
--datadir=G:\\mysql-8.0.23-winx64\\data80323308
以上记住同一行执行

3.2.3 参数文件

[mysqld]
port=3306
basedir=C:\\Program Files\\MySQL
datadir=C:\\Program Files\\MySQL\\Data
max_connections=200
max_connect_errors=10
character-set-server=utf8mb4
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password

3.2.4 安装

G:\\mysql-8.0.23-winx64\\bin\\mysqld install mysql80323308 --defaults-file="G:\\mysql-8.0.23-winx64\\data80323308\\mysql80323308.ini"

安装好之后,Mysql8.0需要重置密码才可以
##密码修改
alter user root@'localhost' identified with mysql_native_password by 'root';
flush privileges;

安装后密码忘记需要重置Mysql8.0密码,请参考博客
https://blog.csdn.net/weixin_41645135/article/details/115782831

喜欢请多多哦点赞评论转发,关注笔者。后续笔者会再带来更丰富的更新,
希望能帮到大家更好的学习提升。你们的支持就是我最大的动力!!!

以上是关于史上最全的MySQL安装指南(各种踩坑,疑难杂症)的主要内容,如果未能解决你的问题,请参考以下文章

史上最全MySQL各种锁详解:一文搞懂MySQL的各种锁

可能是史上最全的weex踩坑攻略

理论 | 可能是史上最全的weex踩坑攻略

Vue 前端开发团队风格指南(史上最全)

史上最全面的Elasticsearch使用指南

史上最全MYSQL总结MySQL超详细笔记