Centos8 安裝 MySQL8.0.26


下載

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

  • 選擇 Red Hat Enterprise Linux / Oracle Linux
  • 選擇 Red Hat Enterprise Linux 8 / Oracle Linux (x86, 64-bit)
  • 下載 RPM Bundle (mysql-8.0.26-1.el8.x86_64.rpm-bundle.tar) 這是多個rpm打包的文件

也可以直接在服務器上用wget

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.26-1.el8.x86_64.rpm-bundle.tar

安裝

安裝手冊 https://dev.mysql.com/doc/refman/8.0/en/linux-installation-rpm.html

yum update
tar xvf mysql-8.0.26-1.el8.x86_64.rpm-bundle.tar 
# 按順序執行
rpm -ivh mysql-community-common-8.0.26-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.26-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.26-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.26-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.26-1.el8.x86_64.rpm

安裝server時會要求perl和libaio, 如果沒有安裝, 需要先用yum install perl libaio安裝一下.
上面的安裝結束后會提示

[root@db backup]# rpm -ivh mysql-community-server-8.0.26-1.el8.x86_64.rpm 
warning: mysql-community-server-8.0.26-1.el8.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-server-8.0.26-1.e################################# [100%]
[/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.

參考

  • https://access.redhat.com/solutions/4154291, Due to the systemd change, any package using /var/run will need to be updated to attempt to remove the logs., 根據Systemd的修改, 任何使用 /var/run 這個目錄的軟件都需要更新路徑來避免出現這個提示.
  • https://bugs.gentoo.org/768051 A workaround of this case is to edit /usr/lib/tmpfiles.d/mysql.conf, Do not edit /usr/lib/tmpfiles.d/mysql.conf. Copy that file to /etc/tmpfiles.d and edit it there if you need to.

需要復制 /usr/lib/tmpfiles.d/mysql.conf 到 /etc/tmpfiles.d/,

cp /usr/lib/tmpfiles.d/mysql.conf /etc/tmpfiles.d/
vi /etc/tmpfiles.d/mysql.conf

將最后一行

d /var/run/mysqld 0755 mysql mysql  -

改為

d /run/mysqld 0755 mysql mysql  -

安裝的過程中除了安裝MySQL自身, 會對系統做以下修改

  • 添加group:mysql, user:mysql
    可以通過more /etc/passwd, more /etc/group查看
  • 添加服務mysqld, 並被設為enabled
    可以通過more /usr/lib/systemd/system/mysqld.service查看
  • 安裝過程中如果產生錯誤, 會被記錄到 /var/log/mysqld.log

文件打開數量限制

如果使用Centos8官方的CloudImage鏡像, 默認的打開文件數為1024, 可以修改 /etc/security/limits.conf , 增加兩行

*                soft    nofile          10240
*                hard    nofile          10240

然后重啟

運行

啟動服務前, 修改數據目錄

編輯 /etc/my.cnf , 修改

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

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

修改為

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

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

注意: 如果修改了socket=/var/lib/mysql/mysql.sock, 那么需要增加client配置, 否則本地執行mysql -uroot -p會提示無法連接socket

SELinux錯誤

如果出現這樣的錯誤

SELinux is preventing /usr/sbin/mysqld from write access on the directory mysql

檢查對比一下原目錄和新目錄的selinux權限

ls -Zl /var/lib/mysql
...
-rw-r-----. 1 mysql mysql system_u:object_r:mysqld_db_t:s0 16777216 Oct 18 16:09  undo_002
ls -Zl /data/mysql/
...
-rw-r-----. 1 mysql mysql system_u:object_r:default_t:s0 16777216 Oct 18 16:14  undo_002

可以看到差一個mysqld_db_t, 用下面的命令將原目錄的selinux權限賦給新的目錄

semanage fcontext -a -e /var/lib/mysql /data/mysql

chcon -R -t mysqld_db_t /data/mysql/命令不行, 啟動還會報錯mysql Activating service name='org.fedoraproject.Setroubleshootd'這樣的錯誤, 參考這篇關於selinux和mysql的詳細解釋
之后重啟就沒問題了

初次啟動時, 會產生的修改

第一次啟動MySQL服務時, 如果發現data目錄是空的, 會進行如下操作

  • 初始化服務器
  • 在data目錄下創建 SSL certificate 和 key
  • 安裝並啟用 validate_password
  • 創建用戶 'root'@'localhost', 隨機口令寫入到 error log 文件, 通過以下命令查看
# RHEL, Oracle Linux, CentOS, and Fedora systems:
sudo grep 'temporary password' /var/log/mysqld.log
# SLES systems:
sudo grep 'temporary password' /var/log/mysql/mysqld.log

注意
如果你初始化完改完密碼, 又換了個新的數據目錄, 那么mysqld會又進行一次初始化工作, 之前設置的密碼是無效的, 要重新去日志里查一下.

初次登錄

用安裝程序產生的隨機口令登錄后, 創建新口令

$ mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

注意 因為默認安裝並啟用了validate_password, 所以口令必須滿足以下全部條件: 至少一個大寫字母+至少一個小寫字母+至少一個數字+至少一個符號+總長度不低於8.

開啟遠程訪問

如果要從遠程訪問, 或者用mysql-shell管理, 需要開放用戶的遠程訪問

方法一
創建另一個 root 用戶 (因為存在的是 'root@localhost')

CREATE USER 'root'@'%' IDENTIFIED BY '123';

Give the privileges:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

從 MySQL 8 開始, 不能在創建用戶時帶 GRANT 參數, 要分開執行:

mysql> CREATE USER 'admin'@'192.168.0.0/255.255.0.0' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'192.168.0.0/255.255.0.0' WITH GRANT OPTION;
-- or
CREATE USER 'admin'@'192.168.12.0/24' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'192.168.12.0/24' WITH GRANT OPTION;
-- or
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

最后

mysql> FLUSH PRIVILEGES;

方法二
修改原 root 用戶

UPDATE mysql.user SET host='%' WHERE user='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

配置優化和調整

sql_mode

MySQL8默認的服務器模式 The default SQL mode in MySQL 8.0 includes these modes:

ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION

NO_AUTO_CREATE_USER在MySQL8中已經被移除.

模式的說明

  • ONLY_FULL_GROUP_BY
    沒有在Group By中的字段, 將被禁止出現在select中, 對於有些項目, 這個選項需要關閉. Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns. A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. The HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.
  • STRICT_TRANS_TABLES
    對事務型存儲引擎, 啟用嚴格SQL模式, 對非事務型則能啟用就啟用, 這個選項是一定要開的. Enable strict SQL mode for transactional storage engines, and when possible for nontransactional storage engines. For details, see Strict SQL Mode.
  • NO_ZERO_IN_DATE,
    這個選項是要開的, 這個選項已經是deprecated了, 在將來的版本這個選項會被合並到嚴格SQL模式中默認開啟.
    NO_ZERO_IN_DATE: 是否允許時間數據出現年份非0但是月份或天為0的記錄. 像'2010-00-01'和'2010-01-00'是不允許的, 但是'0000-00-00'不受這個選項影響. NO_ZERO_IN_DATE也受是否啟用嚴格SQL模式影響:
    • 如果未設置 NO_ZERO_IN_DATE, 那么為0的字段都是可以寫入的也不會報warning
    • 如果設置了 NO_ZERO_IN_DATE, 那么帶0的字段的日期, 會被記為'0000-00-00'同時報一個warning.
    • 如果設置了 NO_ZERO_IN_DATE, 同時還開啟了嚴格SQL模式, 帶0字段的日期就不能寫入了, 會報錯(除非帶IGNORE). 對於 INSERT IGNORE 和 UPDATE IGNORE, 帶0字段的日期會被記為'0000-00-00'並產生一個warning.
  • NO_ZERO_DATE
    這個選項也要開, NO_ZERO_DATE 這個選項是用來禁止'0000-00-00'的, 這個選項也deprecated了, 在將來的版本這個選項會被合並到嚴格SQL模式中默認開啟.
  • ERROR_FOR_DIVISION_BY_ZERO
    就是字面的意思, 是否允許0除, 也要開. 這個選項也deprecated了, 在將來的版本這個選項會被合並到嚴格SQL模式中默認開啟.
  • NO_ENGINE_SUBSTITUTION
    是否自動用默認的存儲引起代替, 如果CREATE TABLE 或 ALTER TABLE 指定的存儲引擎不存在(未編譯)或被禁用了. 默認啟用.

在項目中可以關閉的只有ONLY_FULL_GROUP_BY, 用於兼容一些GROUP BY語句.

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_
ZERO,NO_ENGINE_SUBSTITUTION

其它變量

根據服務器內存大小自行調整

join_buffer_size = 128M
sort_buffer_size = 8M
read_rnd_buffer_size = 8M
key_buffer_size=32M
max_allowed_packet=16M
read_buffer_size = 4M
tmp_table_size = 128M
max_connections = 256
max_heap_table_size = 256M

對應Java項目的修改

依賴修改為

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    <version>8.0.13</version>
</dependency>

spring boot連接參數變為

spring:
  ...
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver

參考


免責聲明!

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



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