CentOS7 安装mysql8.0 glibc版,tar.gz,tar.xz

本文详细介绍了在CentOS 8系统中通过tar.xz包安装MySQL 8.0的过程,包括解压、配置my.cnf、创建用户、初始化数据库、设置服务、启动与自启动,以及主从复制和MGR集群的基本设置。重点讲解了如何配置多实例和安全选项。

一、说明

个人感觉,glibc版的,好配置多版本、多实例。同一版本也方便配置多实例。

mysql安装方式很多:

1.glibc版,tar.gz,tar.xz

CentOS7.7下二进制部署MySQL多版本多实例实战

2.yum安装

CentOS 8 安装 MySQL 5.7 或 MySQL 8

A Quick Guide to Using the MySQL Yum Repository

CentOS 8 安装MySQL 8.0

3.rpm安装

4.源码安装

5.tar包

二、下载地址

MySQL :: Download MySQL Community Server

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

三、安装MySQL8.0

1.上传,解压,重命名

[root@dev1 opt]# tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
[root@dev1 opt]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0.20

2.创建配置文件my.cnf

[root@dev1 opt]# cd mysql-8.0.20/
[root@dev1 mysql-8.0.20]# vi my.cnf

[client]
port=3308

[mysql]
default-character-set=utf8mb4
 
[mysqld]
# MySQL启动用户
user=mysql

# 服务器侦听TCP/IP连接的端口号。
port=3308

# MySQL安装基础目录的路径。
basedir=/opt/mysql-8.0.20

# MySQL服务器数据目录的路径。最好将datadir值指定为绝对路径。
datadir=/opt/mysql-8.0.20/data

# 服务器默认字符集。如果设置此变量,还应设置collation_server以指定字符集的排序规则。
character-set-server=utf8mb4

# 服务器的默认排序规则。
collation_server=utf8mb4_0900_ai_ci

# 表的默认存储引擎。此变量仅为永久表设置存储引擎。
# 要为临时表设置存储引擎,请设置default_tmp_storage_engine系统变量。
default-storage-engine=InnoDB

# 允许的最大同时客户端连接数。(1-100000)
max_connections=151

# 在max_connect_errors之后,来自主机的连续连接请求在没有成功连接的情况下被中断,服务器将阻止该主机进行进一步的连接。
max_connect_errors=100

# 默认的身份验证插件。(mysql_native_password,sha256_password,caching_sha2_password)
default_authentication_plugin=caching_sha2_password

# soket文件,本地连接时使用
socket=/opt/mysql-8.0.20/data/mysql.sock

# 建议禁用符号链接以防止各种安全风险
symbolic-links=0

# 错误日志
log-error=/opt/mysql-8.0.20/data/mysqld.log

# pid文件
pid-file=/opt/mysql-8.0.20/data/mysqld.pid

# 表名不区分大小写(修改后需要重新初始化mysql)
lower_case_table_names=1

# 设置sql_mode(没有NO_AUTO_CREATE_USER了)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

3.创建用户

-s /sbin/nologin 指定用户登入后所使用的shell。默认值为/bin/bash。/sbin/nologin,无法使用shell

-M:不要自动建立用户的登入目录。

[root@dev1 mysql-8.0.20]# useradd -s /sbin/nologin -M mysql
useradd:用户“mysql”已存在
[root@dev1 mysql-8.0.20]# id mysql
uid=1001(mysql) gid=1001(mysql) 组=1001(mysql)

[root@dev1 mysql-8.0.20]# chown -R mysql.mysql /opt/mysql-8.0.20

4.初始化

--initialize-insecure 密码为空

[root@dev1 mysql-8.0.20]# /opt/mysql-8.0.20/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql-8.0.20 --datadir=/opt/mysql-8.0.20/data --lower_case_table_names=1
[Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

如果my.cnf配置了lower_case_table_names=1,初始化时也叫加上--lower_case_table_names=1

否则报错:

tail -f /opt/mysql-8.0.20/data/mysqld.log
[Server] Different lower_case_table_names settings for server ('1') and data dictionary ('0').

如果报错:

error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

安装libiao

yum install libaio

5.创建service文件

[root@dev1 mysql-8.0.20]# vi /usr/lib/systemd/system/mysqld80.service

或  vi /etc/systemd/system/mysqld80.service

[Unit]
Description=MySQL Server 8.0
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-8.0.20/bin/mysqld --defaults-file=/opt/mysql-8.0.20/my.cnf
LimitNOFILE = max_open_files

6.启动服务

[root@dev1 mysql-8.0.20]# systemctl start mysqld80
[root@dev1 mysql-8.0.20]# netstat -lntup|grep mysqld
tcp6       0      0 :::33060                :::*                    LISTEN      9129/mysqld         
tcp6       0      0 :::3305                 :::*                    LISTEN      7479/mysqld         
tcp6       0      0 :::3308                 :::*                    LISTEN      9129/mysqld

7.设置自启动

[root@bogon mysql-8.0.20]# systemctl enable mysqld80

8.客户端登录

[root@dev1 log]# /opt/mysql-8.0.20/bin/mysql -S /opt/mysql-8.0.20/data/mysql.sock
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.20    |
+-----------+
1 row in set (0.00 sec)

mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3308  |
+---------------+-------+
1 row in set (0.01 sec)

9.修改密码,允许远程登录

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select user,authentication_string,host from user;
+------------------+------------------------------------------------------------------------+-----------+
| user             | authentication_string                                                  | host      |
+------------------+------------------------------------------------------------------------+-----------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost |
| root             |                                                                        | localhost |
+------------------+------------------------------------------------------------------------+-----------+
4 rows in set (0.00 sec)

mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

四、主从复制

CentOS7 mysql8.0 主从复制

五、MGR集群

mysql8 MGR集群安装,组复制,单主模式,多主模式

六、文档

MySQL 8.0 Reference Manual

2.2 Installing MySQL on Unix/Linux Using Generic Binaries

2.3.4.2 Creating an Option File

4.2.2.2 Using Option Files

5.1.2 Server Configuration Defaults

5.1.7 Server Command Options

5.1.8 Server System Variables (my.cnf中的属性的解释及默认值)

10.4 Connection Character Sets and Collations

17.1.6 Replication and Binary Logging Options and Variables (主从复制)

七、yum安装mysq8.0默认配置文件(配置参考)

/etc/my.cnf

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

/etc/my.cnf.d/client.cnf

#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]

/etc/my.cnf.d/mysql-default-authentication-plugin.cnf

#
# MySQL 8.0.4 introduced 'caching_sha2_password' as its default authentication plugin.
# It is faster and provides better security then the previous default authentication plugin.
#
# Until now (09/2018), it does not work with some other software (eg. MariaDB client, MariaDB connectors,  ...)
#
# This configuration file changes MySQL default server configuration, so it behaves the same way as in MySQL 5.7.
#
# To change the behaviour back to the upstream default, comment out the following lines:

[mysqld]
default_authentication_plugin=mysql_native_password

/etc/my.cnf.d/mysql-server.cnf

#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html

# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid

<think> 我们参考引用[1]和引用[3]来提供安装方法。引用[1]提供了安装包的具体下载路径和基本步骤,引用[3]则详细说明了从官网下载.tar.gz包的方法。注意,虽然引用[2]提到Ubuntu下安装,但我们的安装方法应该通用。 安装步骤: 1. 下载安装包:可以从引用[1]提供的网盘链接下载(mysql-installer-community-8.0.21.0.tar.gz),或者按照引用[3]的方法从官网下载所需本的.tar.gz包(如mysql-5.6.34.tar.gz等)。 2. 将下载的安装包放在某个目录,例如~/Downloads。 3. 解压安装包:使用tar命令解压。 4. 安装前准备:创建mysql用户和组,创建安装目录和数据目录,设置权限等。 5. 初始化MySQL。 6. 启动MySQL服务器。 由于引用[1]和引用[3]都没有给出完整的安装步骤,我们结合通用方法补充完整。注意,不同本的MySQL安装步骤可能略有不同(例如5.78.0的初始化命令不同),这里以通用步骤为主,具体请参考官方文档。 详细步骤: 1. 下载安装包并解压 假设安装包mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz(注意:官网下载的可能是.tar.xz格式,也可能是.tar.gz,方法类似) 进入下载目录: ```bash cd ~/Downloads ``` 解压安装包(如果是.tar.gz则用`tar -zxvf`,如果是.tar.xz则用`tar -xvf`): ```bash tar -xvf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz -C /usr/local ``` 2. 创建mysql用户和组 ```bash groupadd mysql useradd -r -g mysql -s /bin/false mysql ``` 3. 创建MySQL数据目录并设置权限 ```bash cd /usr/local mv mysql-8.0.21-linux-glibc2.12-x86_64 mysql cd mysql mkdir mysql-files chown mysql:mysql mysql-files chmod 750 mysql-files ``` 4. 初始化MySQL ```bash bin/mysqld --initialize --user=mysql ``` 注意:初始化会生成一个临时密码,需要记录下来(在输出信息中)。 5. 设置SSL(可选,MySQL 8.0默认启用) ```bash bin/mysql_ssl_rsa_setup ``` 6. 启动MySQL ```bash bin/mysqld_safe --user=mysql & ``` 7. 修改root密码 ```bash bin/mysql -u root -p ``` 输入之前记录的临时密码,然后修改密码(在MySQL命令行中): ```sql ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; ``` 8. 设置环境变量(可选): 可以将MySQL的bin目录添加到PATH中,编辑~/.bashrc文件,添加: ```bash export PATH=/usr/local/mysql/bin:$PATH ``` 然后执行: ```bash source ~/.bashrc ``` 9. 设置开机启动(可选) 可以将启动命令添加到/etc/rc.local中。 注意:以上步骤是通用方法,具体细节可能因系统环境和MySQL本而有所不同。特别是初始化步骤,在MySQL 5.7中使用的命令可能是`bin/mysqld --initialize-insecure`(不生成随机密码)或`bin/mysql_install_db`(旧本)。 另外,引用[3]中提到下载的是源码包(如mysql-5.7.16.tar.gz),那是源代码,需要编译安装。而引用[1]中提供的安装包是预编译的二进制包。我们这里介绍的是二进制包的安装方法。 如果下载的是源码包,则需要编译安装,步骤更复杂。但用户要求的是使用.tar.gz安装包安装,通常我们指的是二进制包(虽然源码包也是.tar.gz格式)。因此,我们建议用户下载二进制包(在官网下载时选择Linux - Generic本,通常是.tar.gz.tar.xz格式的二进制包)。 对于源码包安装,步骤包括解压、配置、编译、安装等,这里不展开。 因此,我们强调:从官网下载时,要选择“Linux - Generic”平台,然后下载对应的二进制包(通常是.tar.gz.tar.xz)。 参考引用[3]的下载步骤,在官网选择本时,选择“Linux - Generic”平台,然后下载.tar.gz包(如mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz,注意这个包实际是.tar.xz格式,但安装步骤类似)。 总结:使用二进制包安装较为简单,推荐。 相关问题: 1. 如何验证MySQL安装成功? 2. MySQL安装后如何配置远程访问? 3. 如何卸载使用.tar.gz安装MySQL? </think> ### Linux 系统使用 .tar.gz 安装包安装 MySQL 的方法 以下是详细的二进制安装步骤(以 MySQL 8.0 为例),适用于 Ubuntu/CentOS/Debian 等主流 Linux 发行: #### 一、准备工作 1. **下载安装包** - 官网下载:[MySQL Community Server](https://dev.mysql.com/downloads/mysql/) 选择本 → 平台切换为 **Linux - Generic** → 下载 `mysql-8.0.x-linux-glibc2.12-x86_64.tar.xz` (注:`.tar.xz` 格式需用 `tar -xvf` 解压,若为 `.tar.gz` 则用 `tar -zxvf`) - 网盘备用(引用[1]):[https://pan.quark.cn/s/97fbb8d0fd67](https://pan.quark.cn/s/97fbb8d0fd67) 2. **环境要求** ```bash # 安装依赖库 sudo apt-get install libaio1 libnuma-dev # Ubuntu/Debian sudo yum install libaio numactl # CentOS/RHEL ``` #### 二、安装步骤 1. **解压安装包** ```bash cd ~/Downloads tar -xvf mysql-8.0.x-linux-glibc2.12-x86_64.tar.xz -C /usr/local cd /usr/local sudo ln -s mysql-8.0.x-linux-glibc2.12-x86_64 mysql # 创建软链接 ``` 2. **创建 MySQL 用户和组** ```bash sudo groupadd mysql sudo useradd -r -g mysql -s /bin/false mysql ``` 3. **设置目录权限** ```bash cd mysql sudo mkdir mysql-files sudo chown mysql:mysql mysql-files sudo chmod 750 mysql-files ``` 4. **初始化数据库** ```bash sudo bin/mysqld --initialize --user=mysql # 生成临时密码(记录输出中的密码) sudo bin/mysql_ssl_rsa_setup # 生成SSL证书 ``` 5. **启动 MySQL 服务** ```bash sudo bin/mysqld_safe --user=mysql & # 后台启动 ``` 6. **修改 root 密码** ```bash sudo bin/mysql -u root -p # 输入初始化时的临时密码 ``` ```sql ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPassword'; FLUSH PRIVILEGES; ``` 7. **设置环境变量** ```bash echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bashrc source ~/.bashrc ``` #### 三、配置系统服务(可选) ```bash sudo cp support-files/mysql.server /etc/init.d/mysqld sudo systemctl enable mysqld # 开机自启 sudo systemctl start mysqld # 启动服务 ``` #### 四、验证安装 ```bash mysqladmin -u root -p version # 输入密码后显示本信息 ``` > **注意**: > - MySQL 5.x 需用 `mysql_install_db` 初始化(引用[2]) > - 官网下载需选择 **Source Code** 标签获取 `.tar.gz`(引用[3]) > - 防火墙开放端口:`sudo ufw allow 3306/tcp`
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值