# 下载

## 下载地址

https://dev.mysql.com/downloads/mysql/

## 下载版本

Linux Generic  linux通用版本

```sh
# 查看glibc版本
[root@hy-node6 ~]# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

# 下载
wget https://dev.mysql.com/get/Downloads/MySQL-8.1/mysql-8.1.0-linux-glibc2.17-x86_64.tar
```

![image-20230921091205221](img\image-20230921091205221.png)

# 安装

## 安装参考地址

https://dev.mysql.com/doc/refman/8.1/en/binary-installation.html

```sh
# 添加用户组mysql  查看用户组  cat /etc/group
groupadd mysql
# 添加用户 hy-mysql并添加到用户组mysql  查看用户 cat /etc/passw
useradd -r -g mysql -s /bin/false hy-mysql
# 解压文件
tar xvf mysql-8.1.0-linux-glibc2.17-x86_64.tar
# 解压
xz -dc mysql-8.1.0-linux-glibc2.17-x86_64.tar.xz | tar x
# 设置环境变量
export PATH=$PATH:/software/mysql/mysql-8.1.0-linux-glibc2.17-x86_64/bin
# 使环境变量生效
source /etc/profile
```

# 设置

## 参考地址

https://dev.mysql.com/doc/refman/8.1/en/postinstallation.html

## 配置文件地址

```sh
# 备份配置文件
cp my.cnf my.cnf.backup
# 默认配置文件，不建议修改建议使用默认
vi /etc/my.cnf
# 修改文件地址
[client]
port=3306
socket=/software/mysql/socket/mysql.sock
[mysqld]
datadir=/software/mysql/datadir
basedir=/software/mysql/basedir
socket=/software/mysql/socket/mysql.sock
```

```
# 设置地址授权,用于配置文件地址路径
[root@hy-node6 mysql-8.1.0-linux-glibc2.17-x86_64]# pwd
/software/mysql
[root@hy-node6 ]# mkdir -p datadir basedir socket/mysql.sock
[root@hy-node6 ]# chown hy-mysql:mysql datadir basedir socket/mysql.sock
[root@hy-node6 ]# chmod 750 datadir basedir socket/mysql.sock

[root@hy-node6 mysql-8.1.0-linux-glibc2.17-x86_64]# bin/mysqld --initialize --user=hy-mysql
2023-09-21T02:22:43.673521Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2023-09-21T02:22:43.675219Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-09-21T02:22:43.675412Z 0 [System] [MY-013169] [Server] /software/mysql/mysql-8.1.0-linux-glibc2.17-x86_64/bin/mysqld (mysqld 8.1.0) initializing of server in progress as process 30526
2023-09-21T02:22:43.695144Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-09-21T02:22:44.540948Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-09-21T02:22:52.149208Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xa=V>dpyK7;y
2023-09-21T02:22:58.749405Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.
```

## 使用systemctl启动

https://dev.mysql.com/doc/mysql-secure-deployment-guide/8.0/en/secure-deployment-post-install.html#secure-deployment-startup-options

```
cd /usr/lib/systemd/system
touch mysqld.service
chmod 644 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=hy-mysql
Group=mysql

# Have mysqld write its state to the systemd notify socket
Type=notify

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/software/mysql/mysql-8.1.0-linux-glibc2.17-x86_64/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS 

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 10000

Restart=on-failure

RestartPreventExitStatus=1

# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1

PrivateTmp=false
```

开始设置

```
systemctl enable mysqld.service
systemctl start mysqld
systemctl status mysqld
```

连接设置mysql

```sql
mysql -u root -p
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
# 设置远程连接
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '123456';
CREATE USER 'root'@'::1' IDENTIFIED BY '123456';
# 设置远程密码
CREATE USER 'root'@'%' IDENTIFIED   WITH mysql_native_password  BY '123456';
# 授予所有权限
GRANT ALL ON *.* TO 'root'@'%';
flush privileges;
```



# 遇到问题

## 一

报错：Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

方式一：

mysql -S /software/mysql/socket/mysql.sock -u root -p

方式二：

/software/mysql/socket/mysql.sock（/etc/my.conf中mysqld配置的socket）

设置软连接不用使用 -S /software/mysql/socket/mysql.sock
ln -s  /software/mysql/socket/mysql.sock /tmp/mysql.sock

方式三：

修改配置文件/etc/my.conf 添加保证client和mysqld中socket相同

```
[client]
port=3306
socket=/software/mysql/socket/mysql.sock
```

# 多实例启动

https://dev.mysql.com/doc/refman/8.1/en/using-systemd.html

# 从机安装

复制安装包

```
scp mysql-8.1.0-linux-glibc2.17-x86_64.tar.xz root@192.168.1.120:/software/mysql

root@localhost: %mfSO2t==tkF
```

