一、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发布的视频教程:
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
[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--------------