操作系統(tǒng)環(huán)境:centos linux release 7.7.1908 (Core)

基礎(chǔ)環(huán)境配置
關(guān)閉SeLinux及防火墻
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0
?
systemctl stop firewalld //停止系統(tǒng)默認(rèn)的防火墻
systemctl mask firewalld //屏蔽服務(wù)(讓它不能啟動(dòng))
yum remove -y firewalld //卸載系統(tǒng)自帶的防火墻
安裝運(yùn)維基礎(chǔ)工具
yum install -y chkconfig net-tools ntsysv mlocate lrzsz wget lsof setuptool
安裝yum源
yum install -y epel-release
wget https://dev.MySQL.com/get/mysql80-community-release-el7-2.noarch.rpm
yum localinstall -y mysql80-community-release-el7-2.noarch.rpm
MySQL8.0安裝
yum install -y bison-devel libaio-devel perl-Data-Dumper
yum install -y mysql-server
systemctl start mysqld //啟動(dòng)MySQL
systemctl enable mysqld.service //開(kāi)機(jī)自啟動(dòng)
由于mysql的yum源是在國(guó)外,所以,在國(guó)內(nèi)安裝很慢,因此,森哥的做法,是直接下載rpm包回來(lái)手工安裝 。如下操作:
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
yum localinstall -y mysql-community-* //使用localinstall會(huì)自己補(bǔ)足未安裝的所需要組件
systemctl start mysqld //啟動(dòng)MySQL
systemctl enable mysqld.service //開(kāi)機(jī)自啟動(dòng)


修改初始密碼
grep 'temporary password' /var/log/mysqld.log //mysql5.7版本后,初始密碼不再為空,默認(rèn)隨機(jī)生成,可通過(guò)該命令查詢
mysql -u root -p //進(jìn)入mysql,輸入剛剛查到的密碼
alter user 'root'@'localhost' identified with mysql_native_password by '三種或以上的八位字符'; //修改密碼,并將密碼插件更改為mysql_native_password
初始my.cnf配置的內(nèi)容為:
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
原則上,上面的這個(gè)配置,就可以正常使用了。但有時(shí)候?yàn)榱艘恍I(yè)務(wù)場(chǎng)合的應(yīng)用,就需要考慮對(duì)其進(jìn)行修改,以達(dá)到符合實(shí)現(xiàn)業(yè)務(wù)的需求。
mkdir /var/log/mysqld
touch /var/log/mysqld/error.log
chown mysql.mysql /var/log/mysqld -R
# 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
# server-id = 1 //單MySQL服務(wù)下,可以省略不用
datadir=/var/lib/mysql //數(shù)據(jù)文件所在位置
socket=/var/lib/mysql/mysql.sock //sock文件所在位置
log_error = /var/log/mysqld/error.log //數(shù)據(jù)庫(kù)錯(cuò)誤日志文件
# skip_name_resolve = 1 //設(shè)置這個(gè),說(shuō)明只能通過(guò)IP連接,不用主機(jī)名,一般不建議使用
character-set-server=utf8mb4 //數(shù)據(jù)庫(kù)默認(rèn)字符集,可以支持表情符號(hào)等,utf8不支持表情符號(hào)。另外utf8mb4是mysql8.0起支持的。
collation-server = utf8mb4_general_ci //數(shù)據(jù)庫(kù)字符集對(duì)應(yīng)一些排序等規(guī)則,必須和character-set-server對(duì)應(yīng)
transaction_isolation = READ-COMMITTED //事務(wù)隔離級(jí)別,默認(rèn)為可重復(fù)讀
init_connect='SET NAMES utf8mb4' //設(shè)置client連接mysql時(shí)的字符集,防止亂碼
max_connections=500 //最大連接數(shù)
max_connect_errors = 1000 //最大錯(cuò)誤連接數(shù)
explicit_defaults_for_timestamp = true //TIMESTAMP如果沒(méi)有顯示聲明NOT NULL,允許NULL值
max_allowed_packet = 1024M //SQL數(shù)據(jù)包發(fā)送的大小,如果有BLOB對(duì)象建議修改成1G
# interactive_timeout = 1800 //MySQL默認(rèn)的wait_timeout 值為8個(gè)小時(shí), interactive_timeout參數(shù)需要同時(shí)配置才能生效
# wait_timeout = 1800
tmp_table_size = 134217728 //內(nèi)部?jī)?nèi)存臨時(shí)表的最大值,超過(guò)了這個(gè)值將寫(xiě)入磁盤(pán),系統(tǒng)IO壓力增大
max_heap_table_size = 134217728
# query_cache_size = 0 //禁用mysql的緩存查詢結(jié)果集功能,可根據(jù)業(yè)務(wù)需求是否開(kāi)啟,通常為關(guān)閉
# query_cache_type = 0
# slow_query_log = 1 //慢查詢sql日志設(shè)置
# slow_query_log_file = /var/log/mysqld/slow.log
# long_query_time = 8 //慢查詢執(zhí)行的秒數(shù),必須達(dá)到此值可被記錄
# log_queries_not_using_indexes = 1 //檢查未使用到索引的sql
# log_throttle_queries_not_using_indexes = 5 //開(kāi)啟后,記錄慢sql的頻次、每分鐘記錄的條數(shù)
# min_examined_row_limit = 100 // 檢索的行數(shù)必須達(dá)到此值才可被記為慢查詢
# skip-log-bin //關(guān)閉binlog功能
binlog_expire_logs_seconds=604800 //binlog日志文件保存的過(guò)期時(shí)間,過(guò)期后自動(dòng)刪除
innodb_log_file_size=60M
innodb_buffer_pool_size=128M
[client]
default-character-set=utf8mb8
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
open-files-limit = 8192
log-error=/var/log/mysqld.log
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
密碼恢復(fù)
有時(shí)候,會(huì)將root密碼給忘記了,就需要對(duì)MySQL進(jìn)行密碼恢復(fù)。
在/etc/my.cnf中,的[mysqld]內(nèi),加入如下內(nèi)容:
skip-grant-table //改為安全模式,無(wú)法密碼登陸
重啟mysqld
systemctl restart mysqld
進(jìn)入重置密碼
mysql -u root -p //空密碼,直接回車
use mysql;
update user set authentication_string='' where user='root'; //先清空密碼
注釋掉my.cnf中的skip-grant-table,再重啟mysqld。然后就可以免密碼進(jìn)入,再修改密碼。
mysql -u root -p //不用輸入密碼,直接進(jìn)入
alter user 'root'@'localhost' identified with mysql_native_password by '三種或以上的八位字符'; //修改密碼,并將密碼插件更改為mysql_native_password
現(xiàn)在就恢復(fù)正常使用了。