centos系統安裝MySQL8


使用yum倉庫安裝MySQL8

1.查看centos系統版本

# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

2.下載並安裝repo倉庫文件

下載地址:https://dev.mysql.com/downloads/repo/yum/


# wget https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm

# yum localinstall -y mysql80-community-release-el7-5.noarch.rpm 
已加載插件:fastestmirror
正在檢查 mysql80-community-release-el7-5.noarch.rpm: mysql80-community-release-el7-5.noarch
mysql80-community-release-el7-5.noarch.rpm 將被安裝
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 mysql80-community-release.noarch.0.el7-5 將被 安裝
--> 解決依賴關系完成

依賴關系解決

================================================================================================
 Package                     架構     版本      源                                         大小
================================================================================================
正在安裝:
 mysql80-community-release   noarch   el7-5     /mysql80-community-release-el7-5.noarch   9.1 k

事務概要
================================================================================================
安裝  1 軟件包

總計:9.1 k
安裝大小:9.1 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : mysql80-community-release-el7-5.noarch                                      1/1 
  驗證中      : mysql80-community-release-el7-5.noarch                                      1/1 

已安裝:
  mysql80-community-release.noarch 0:el7-5                                                      

完畢!

3.驗證倉庫軟件


# yum makecache

# yum repolist all|grep mysql8
mysql80-community/x86_64            MySQL 8.0 Community Server      啟用:    321
mysql80-community-source            MySQL 8.0 Community Server - So 禁用

# yum repolist enabled | grep "mysql*"            
mysql-connectors-community/x86_64       MySQL Connectors Community           230
mysql-tools-community/x86_64            MySQL Tools Community                138
mysql80-community/x86_64                MySQL 8.0 Community Server           321

4.安裝MySQL8

說明:此時倉庫中只有一個MySQL版本,因此可以直接安裝

# yum install -y mysql-community-server

# 使用以下方式檢查安裝包
# rpm -qa|grep -i "mysql.*8.*"
mysql80-community-release-el7-5.noarch
mysql-community-client-plugins-8.0.28-1.el7.x86_64
mysql-community-client-8.0.28-1.el7.x86_64
mysql-community-libs-compat-8.0.28-1.el7.x86_64
mysql-community-common-8.0.28-1.el7.x86_64
mysql-community-libs-8.0.28-1.el7.x86_64
mysql-community-icu-data-files-8.0.28-1.el7.x86_64
mysql-community-server-8.0.28-1.el7.x86_64

默認配置文件內容

# cat /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

查看默認密碼

yum方式安裝的MySQL8卸載

使用rpm文件安裝MySQL8

說明:使用rpm包的形式安裝MySQL8需要下載多個rpm文件,但是官方已經提供好一個壓縮包里,里面是所有的rpm文件,只需要下載這個壓縮包就行了


tar xfc mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
rpm -i mysql-community-{server-8,client,common,libs}*

下載rpm安裝軟件

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

使用通用二進制文件安裝MySQL8

下載地址:https://dev.mysql.com/downloads/mysql/

1.安裝依賴
mysql依賴libaio,如果未在本地安裝,則數據目錄初始化和后續的服務啟動將失敗

# 默認是已安裝的

[root@localhost ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64

# yum -y install libaio # 若不存在則安裝

2.下載二進制安裝文件

# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

3.安裝

# 添加mysql組和mysql用戶,所有的文件和目錄應該在mysql用戶下
# groupadd mysql
# useradd -r -g mysql -s /bin/false mysql

# 設置安裝位置為/usr/local,可以修改為其他位置
# tar -xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

# 添加軟鏈接,以后升級版本,只需要解壓到相同的位置,更改符號鏈接就行了
# cd /usr/local/
# ll
總用量 0
drwxr-xr-x. 2 root root   6 4月  11 2018 bin
drwxr-xr-x. 2 root root   6 4月  11 2018 etc
drwxr-xr-x. 2 root root   6 4月  11 2018 games
drwxr-xr-x. 2 root root   6 4月  11 2018 include
drwxr-xr-x. 2 root root   6 4月  11 2018 lib
drwxr-xr-x. 2 root root   6 4月  11 2018 lib64
drwxr-xr-x. 2 root root   6 4月  11 2018 libexec
drwxr-xr-x  9 root root 129 3月  30 13:52 mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 4月  11 2018 sbin
drwxr-xr-x. 5 root root  49 4月   9 2021 share
drwxr-xr-x. 2 root root  56 3月  30 13:47 src

# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql

# ll
總用量 0
drwxr-xr-x. 2 root root   6 4月  11 2018 bin
drwxr-xr-x. 2 root root   6 4月  11 2018 etc
drwxr-xr-x. 2 root root   6 4月  11 2018 games
drwxr-xr-x. 2 root root   6 4月  11 2018 include
drwxr-xr-x. 2 root root   6 4月  11 2018 lib
drwxr-xr-x. 2 root root   6 4月  11 2018 lib64
drwxr-xr-x. 2 root root   6 4月  11 2018 libexec
lrwxrwxrwx  1 root root  35 3月  30 13:53 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x  9 root root 129 3月  30 13:52 mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 4月  11 2018 sbin
drwxr-xr-x. 5 root root  49 4月   9 2021 share
drwxr-xr-x. 2 root root  56 3月  30 13:47 src

# 創建必要的目錄並將所有權更改為mysql

# cd mysql
# ll
總用量 284
drwxr-xr-x  2 7161 31415   4096 12月 18 02:28 bin
drwxr-xr-x  2 7161 31415     55 12月 18 02:28 docs
drwxr-xr-x  3 7161 31415    282 12月 18 02:28 include
drwxr-xr-x  6 7161 31415    201 12月 18 02:28 lib
-rw-r--r--  1 7161 31415 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 7161 31415     30 12月 18 02:28 man
-rw-r--r--  1 7161 31415    666 12月 18 00:07 README
drwxr-xr-x 28 7161 31415   4096 12月 18 02:28 share
drwxr-xr-x  2 7161 31415     77 12月 18 02:28 support-files

[root@localhost mysql]# mkdir data
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]# chmod 750 data
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .
[root@localhost mysql]# ll
總用量 284
drwxr-xr-x  2 mysql mysql   4096 12月 18 02:28 bin
drwxr-x---  2 mysql mysql      6 3月  30 02:28 data
drwxr-xr-x  2 mysql mysql     55 12月 18 02:28 docs
drwxr-xr-x  3 mysql mysql    282 12月 18 02:28 include
drwxr-xr-x  6 mysql mysql    201 12月 18 02:28 lib
-rw-r--r--  1 mysql mysql 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 mysql mysql     30 12月 18 02:28 man
drwxr-x---  2 mysql mysql      6 3月  30 13:57 mysql-files
-rw-r--r--  1 mysql mysql    666 12月 18 00:07 README
drwxr-xr-x 28 mysql mysql   4096 12月 18 02:28 share
drwxr-xr-x  2 mysql mysql     77 12月 18 02:28 support-files

# 清除mariadb有關文件
# rpm -qa | grep "mariadb"
mariadb-libs-5.5.68-1.el7.x86_64

# rm -rf /etc/my.cnf
# rm -rf /etc/my.cnf.d/

# 初始化MySQL,會生成一個臨時密碼
# 此時data目錄下會有初始化后的數據
[root@localhost mysql]# bin/mysqld --initialize --user mysql
2022-03-30T06:28:50.697879Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 2346
2022-03-30T06:28:50.704665Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-03-30T06:28:51.016806Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-03-30T06:28:51.942504Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,ai:FtMdM8tX

# 設置SSL的RSA,此時會在data存儲目錄下生成有關文件
[root@localhost mysql]# bin/mysql_ssl_rsa_setup 

[root@localhost data]# pwd
/usr/local/mysql/data
[root@localhost data]# ll

-rw------- 1 mysql mysql     1680 3月  30 14:53 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 ca.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 client-cert.pem
-rw------- 1 mysql mysql     1680 3月  30 14:53 client-key.pem
-rw------- 1 mysql mysql     1680 3月  30 14:53 private_key.pem
-rw-r--r-- 1 mysql mysql      452 3月  30 14:53 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 server-cert.pem
-rw------- 1 mysql mysql     1676 3月  30 14:53 server-key.pem

# 將二進制文件的所有權更改為root,數據文件的所有權更改為mysql
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
[root@localhost mysql]# chown -R mysql mysql-files
[root@localhost mysql]# ll
總用量 284
總用量 288
drwxr-xr-x  2 root  mysql   4096 12月 18 02:28 bin
drwxr-x---  6 mysql mysql   4096 3月  30 14:53 data
drwxr-xr-x  2 root  mysql     55 12月 18 02:28 docs
drwxr-xr-x  3 root  mysql    282 12月 18 02:28 include
drwxr-xr-x  6 root  mysql    201 12月 18 02:28 lib
-rw-r--r--  1 root  mysql 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 root  mysql     30 12月 18 02:28 man
drwxr-x---  2 mysql mysql      6 3月  30 14:52 mysql-files
-rw-r--r--  1 root  mysql    666 12月 18 00:07 README
drwxr-xr-x 28 root  mysql   4096 12月 18 02:28 share
drwxr-xr-x  2 root  mysql     77 12月 18 02:28 support-files

# 將啟動腳本復制到init.d中
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

# 將mysql的二進制文件到處到PATH環境變量中
# 臨時辦法,重啟就不能用了
[root@localhost mysql]# export PATH=$PATH:/usr/local/mysql/bin

# 永久辦法
[root@localhost mysql]# cat >> /etc/profile.d/mysql.sh << "EOF"
#!/bin/bash

export PATH=$PATH:/usr/local/mysql/bin
EOF

source /etc/profile.d/mysql.sh

安裝完畢后將在/usr/local/mysql中獲得下圖中列出的目錄

啟動MySQL8

默認沒有/etc/my.cnf配置文件

[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS! 
# 使用生成的臨時密碼登錄,並為root用戶設置自定義密碼
# mysql -hlocalhost -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28

Copyright (c) 2000, 2022, 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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass5!';
Query OK, 0 rows affected (0.01 sec)

mysql> exit;
Bye

# 退出MySQL,並重啟MySQL就能使用新密碼登錄了
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

設置開機啟動

chkconfig --add mysqld
chkconfig mysqld on
chkconfig mysql --list

日常運行用service或者/etc/init.d/mysqld都可以

[root@localhost mysql]# service mysqld status
 ERROR! MySQL is not running
[root@localhost mysql]# service mysqld start
Starting MySQL. SUCCESS! 
[root@localhost mysql]# service mysqld status
 SUCCESS! MySQL running (1908)
[root@localhost mysql]# service mysqld stop
Shutting down MySQL. SUCCESS! 
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@localhost mysql]# service mysqld status
 SUCCESS! MySQL running (2083)


二進制安裝方式卸載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM