一、安裝 MySQL 的 yum 源
https://dev.mysql.com/downloads/repo/yum/
# 安裝源,會在 /etc/yum.repos.d/ 下生成 mysql-community.repo 文件 rpm -ivh https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm # 查看 yum 源中是否有 mysql 安裝包 yum repolist all | grep mysql
mysql-community.repo

# Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-preview] name=MySQL Tools Preview baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.5-community] name=MySQL Cluster 7.5 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.6-community] name=MySQL Cluster 7.6 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-8.0-community] name=MySQL Cluster 8.0 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
mysql-community-source.repo

[mysql55-community-source] name=MySQL 5.5 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql56-community-source] name=MySQL 5.6 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community-source] name=MySQL 5.7 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community-source] name=MySQL 8.0 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-connectors-community-source] name=MySQL Connectors Community - Source baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community-source] name=MySQL Tools Community - Source baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-preview-source] name=MySQL Tools Preview - Source baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.5-community-source] name=MySQL Cluster 7.5 Community - Source baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.6-community-source] name=MySQL Cluster 7.6 Community - Source baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-8.0-community-source] name=MySQL Cluster 8.0 Community - Source baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
二、選擇 MySQL 的版本並安裝
# 安裝管理工具 yum install -y yum-utils # 禁用 mysql 8.0 yum-config-manager --disable mysql80-community # 啟用 mysql 5.7 yum-config-manager --enable mysql57-community # 安裝 yum install -y mysql-community-server
三、相關設置與啟動
啟動服務
# 開啟服務 systemctl start mysqld.service # 開機自啟動 systemctl enable mysqld.service # 檢查MySQL服務器的狀態 systemctl status mysqld.service # 查看端口 netstat -ln | grep 3306 # 從日志文件中獲取 root 賬戶初始密碼 grep "password" /var/log/mysqld.log
初始化服務
# 直接用該密碼登陸會要求改密碼,最好先進行向導操作 # 安全向導 mysql_secure_installation # 輸入 root 初始密碼,初始密碼中有特殊字符時需轉義 Enter password for user root: # 設置新密碼,密碼太簡單會提示錯誤:Your password does not satisfy the current policy requirements New password # 是否確認修改root密碼 Change the password for root # 是否刪除匿名用戶 Remove anonymous users # 是否禁止root遠程登錄 Disallow root login remotely # 是否刪除test數據庫 Remove test database and access to it # 是否現在刷新權限 Reload privilege tables now
設置簡單密碼
官方 在新版本中對密碼設置要求比較嚴格
-- 查看密碼策略 SHOW VARIABLES LIKE 'validate_password%'; -- 修改密碼策略,修改密碼檢查強度和密碼長度就可以 -- 8.0 之前 set global validate_password_policy=0; set global validate_password_length=4; -- 8.0 之后 set global validate_password.policy=0; set global validate_password.length=4; -- 修改當前用戶密碼 ALTER USER USER() IDENTIFIED BY '123456'; -- 修改指定用戶密碼 ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
忘記 root 密碼情況下設置密碼
# 編輯配置文件 vim /etc/my.cnf # 文件最后添加免密登陸 skip-grant-tables # 重啟 mysql 服務 systemctl restart mysqld.service # 登陸 mysql,不用密碼 mysql -uroot
清空密碼
-- 清空指定賬戶密碼 use mysql; update user set authentication_string='' where user='root'; -- 退出 mysql,刪除免密登陸配置,重啟 mysql 服務,用空密碼登陸,然后進行修改密碼操作
添加遠程訪問權限
首先開放本機 3306 端口或關閉防火牆
# iptables # 開放端口 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT # 保存配置 service iptables save # firewalld # 開放端口 firewall-cmd --zone=public --add-port=3306/tcp --permanent # 重啟 firewall firewall-cmd --reload # 查看已開放端口 firewall-cmd --list-ports # 關閉防火牆 systemctl stop firewalld.service # 關閉開機自啟 systemctl disable firewalld.service
為已有用戶添加遠程訪問權限
-- 選擇mysql數據庫 use mysql; -- 修改指定用戶登錄位置,%為任意地址,可配置指定IP update user set host='%' where user='root'; -- 修改登陸密碼和加密規則,8 版本默認的認證插件為Caching_sha2_password,原來是mysql_native_password,不修改 Navicat 等工具連接會認證失敗 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; -- 刷新權限 flush privileges;
創建新用戶並添加遠程訪問權限
-- 創建用戶(user1為用戶名,%為登陸地址任意ip,也可指定,123456為登錄密碼) CREATE USER 'user1'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; -- 默認創建的用戶是無權限,只能登錄而已,(all:所有權限,有select,update等等權限,后面的*.*:指定數據庫.指定表,這里指所有,to后面是用戶) grant all on *.* to 'user1'@'%'; -- 刷新權限 flush privileges;
查看登陸權限
use mysql; select host,user from user;
四、配置(/etc/my.cnf)
修改 datadir 路徑到其它磁盤后可能會出現服務啟動失敗的情況,這時關閉 selinux 即可。
[mysqld] # 關閉密碼驗證,可設置簡單密碼 validate_password=off # 存儲引擎 default-storage-engine=INNODB # 編碼 character-set-server=utf8 # 排序規則 collation-server=utf8_general_ci # 免密登陸 skip-grant-tables
五、常用命令
-- 查看版本 select version(); -- 設置編碼 SET NAMES utf8; -- 查看編碼 show variables like '%character%'; -- 查看排序規則 show variables like 'collation%'; -- 查看儲存引擎,Support列,YES表示支持,DEFAULT表示默認,NO表示不支持 show engines; show variables like '%storage_engine%'; -- 設置儲存引擎 SET default_storage_engine=InnoDB; -- 查看日志文件位置 select @@log_error; -- 查看數據文件位置 select @@datadir;