安裝環(huán)境:CentOS7 64位 MINI版丑蛤,安裝MySQL5.7
1拷橘、配置YUM源
在MySQL官網(wǎng)中下載YUM源rpm安裝包:http://dev.mysql.com/downloads/repo/yum/
# 下載mysql源安裝包shell> wgethttp://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安裝mysql源shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm
檢查mysql源是否安裝成功
shell> yum repolist enabled| grep "mysql.*-community.*"
看到上圖所示表示安裝成功膜眠。
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改變默認(rèn)安裝的mysql版本账蓉。比如要安裝5.6版本肮塞,將5.7源的enabled=1改成enabled=0。然后再將5.6源的enabled=0改成enabled=1即可梯澜。改完之后的效果如下所示:
2寞冯、安裝MySQL
shell>yum install mysql-community-server
3、啟動(dòng)MySQL服務(wù)
shell> systemctl start mysqld
查看MySQL的啟動(dòng)狀態(tài)
shell> systemctl status mysqld● mysqld.service- MySQL Server? Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)Active: active (running) since 五2016-06-2404:37:37CST; 35min agoMain PID:2888(mysqld)? CGroup: /system.slice/mysqld.service└─2888/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid6月2404:37:36localhost.localdomainsystemd[1]: Starting MySQL Server...6月2404:37:37localhost.localdomainsystemd[1]: Started MySQL Server.
4腊徙、開機(jī)啟動(dòng)
shell> systemctl enable mysqldshell> systemctl daemon-reload
5简十、修改root本地登錄密碼
mysql安裝完成之后,在/var/log/mysqld.log文件中給root生成了一個(gè)默認(rèn)密碼撬腾。通過下面的方式找到root默認(rèn)密碼螟蝙,然后登錄mysql進(jìn)行修改:
shell> grep'temporary password'/var/log/mysqld.log
shell>mysql-uroot-pmysql>ALTER USER'root'@'localhost'IDENTIFIEDBY'MyNewPass4!';
或者
mysql>setpasswordfor'root'@'localhost'=password('MyNewPass4!');
注意:mysql5.7默認(rèn)安裝了密碼安全檢查插件(validate_password),默認(rèn)密碼檢查策略要求密碼必須包含:大小寫字母民傻、數(shù)字和特殊符號(hào)胰默,并且長(zhǎng)度不能少于8位。否則會(huì)提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements錯(cuò)誤漓踢,如下圖所示:
通過msyql環(huán)境變量可以查看密碼策略的相關(guān)信息:
mysql> show variableslike'%password%';
validate_password_policy:密碼策略牵署,默認(rèn)為MEDIUM策略
validate_password_dictionary_file:密碼策略文件,策略為STRONG才需要
validate_password_length:密碼最少長(zhǎng)度
validate_password_mixed_case_count:大小寫字符長(zhǎng)度喧半,至少1個(gè)
validate_password_number_count :數(shù)字至少1個(gè)
validate_password_special_char_count:特殊字符至少1個(gè)
上述參數(shù)是默認(rèn)策略MEDIUM的密碼檢查規(guī)則奴迅。
共有以下幾種密碼策略:
策略檢查規(guī)則
0 or LOWLength
1 or MEDIUMLength; numeric, lowercase/uppercase, and special characters
2 or STRONGLength; numeric, lowercase/uppercase, and special characters; dictionary file
MySQL官網(wǎng)密碼策略詳細(xì)說明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
修改密碼策略
在/etc/my.cnf文件添加validate_password_policy配置,指定密碼策略
# 選擇0(LOW)挺据,1(MEDIUM)取具,2(STRONG)其中一種脖隶,選擇2需要提供密碼字典文件validate_password_policy=0
如果不需要密碼策略,添加my.cnf文件中添加如下配置禁用即可:
validate_password =off
重新啟動(dòng)mysql服務(wù)使配置生效:
systemctl restart mysqld
6暇检、添加遠(yuǎn)程登錄用戶
默認(rèn)只允許root帳戶在本地登錄产阱,如果要在其它機(jī)器上連接mysql,必須修改root允許遠(yuǎn)程連接块仆,或者添加一個(gè)允許遠(yuǎn)程連接的帳戶构蹬,為了安全起見,我添加一個(gè)新的帳戶:
mysql>GRANTALLPRIVILEGESON*.*TO'yangxin'@'%'IDENTIFIEDBY'Yangxin0917!'WITHGRANT OPTION;
7悔据、配置默認(rèn)編碼為utf8
修改/etc/my.cnf配置文件庄敛,在[mysqld]下添加編碼配置,如下所示:
[mysqld]character_set_server=utf8init_connect='SET NAMES utf8'
重新啟動(dòng)mysql服務(wù)蜜暑,查看數(shù)據(jù)庫默認(rèn)編碼如下所示:
默認(rèn)配置文件路徑:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服務(wù)啟動(dòng)腳本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid