CentOS7.6 MySQL8環境搭建 配置遠端登入 字符集UTF8 簡單密碼

陳楊發表於2019-01-04

一、環境準備

1、清理環境中系統自帶的MySQL

(1)刪除系統自帶的MySQL或Mariadb

yum remove mysql-libs

(2)查詢系統中是否還有殘餘的依賴包

rpm -qa | grep mariadb 

(3)刪除rpm依賴包

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

2依賴包下載

注:請按照自己的實際需求進行包管理

(1)安裝所有的開發工具包

yum groupinstall -y "Development tools" 

(2)安裝其它的必需包

yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel 

二、MySQL8安裝

1、在官網上下載rpm包管理檔案

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

2、本地安裝mysql

yum localinstall mysql80-community-release-el7-1.noarch.rpm 

3、查詢是否能連線上MySQL倉庫

yum repolist enabled | grep "mysql.*-community.*"

4、安裝MySQL服務

yum install mysql-community-server

三、MySQL配置

1、開啟相容MySQL8之前版本

vim /etc/my.cnf  
default-authentication-plugin=mysql_native_password

2、修改MySQL預設初始密碼

(1)查詢MySQL預設初始密碼

grep `temporary password` /var/log/mysqld.log

(2)登入MySQL

mysql -u root -p 

(3)輸入查詢顯示的密碼

(4)修改密碼

ALTER USER `root`@`localhost` IDENTIFIED BY `Welcome_1`;

(5)重新整理系統許可權列表

flush privileges;

(6)重新登入

3、遠端登入授權

use mysql;
select user,host from user; 
update user set host = `%` where user = `root`; 

4、防火牆開啟3306與22埠

3306 MySQL預設埠
22 SSH訪問埠

5、密碼策略

(1)檢視MySQL密碼策略

show variables like `%validate%`;

(2)設定簡單密碼

注:global配置全域性 相當於更改配置檔案my.cnf

set global validate_password.policy=0;
set global validate_password.mixed_case_count=0;
set global validate_password.number_count=0; 
set global validate_password.special_char_count=0; 
flush privileges; 

(3)修改密碼

mysql> alter user `root`@`%` IDENTIFIED BY `密碼`;

6、預設字符集修改

(1)檢視MySQL密碼策略

show variables like `%character%`;

(2)my.cnf配置檔案修改

[mysqld]
init_connect=`set collation_connection = utf8_general_ci`
init_connect=`set collation_database=utf8_general_ci` 
init_connect=`set names utf8`
init_connect=`set character_set_connection=utf8`
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake

四、MySQL啟動

1、啟動MySQL服務

systemctl start mysqld

2、檢視MySQL服務狀態

systemctl status mysqld

3、開機自啟

systemctl enable mysqld.service

4、重啟MySQL服務

systemctl restart mysqld;

五、注意事項

1、以上操作均為root使用者
2、ERROR 1819 (HY000)

mysql5.7預設安裝了密碼安全檢查外掛(validate_password)
預設密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,並且長度不能少於8位。

3、新設定使用者或更改密碼後需用flush privileges重新整理MySQL的系統許可權相關表,否則會出現拒絕訪問
flush privileges; 
4、如果是阿里雲等雲伺服器 需要在安全設定訪問管理中 開啟3306埠訪問
5、如果使用Xshell連線訪問22號埠 使用Xftp請使用SFTP協議訪問22號埠

6、配置字符集編碼時請使用“utf8_general_ci”而非“utf8_unicode_ci”

相關文章