新增 MySQL Yum Repository
MySQL 官方 Yum Repository
sudo yum localinstall https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
檢視 MySQL 儲存庫是否新增成功
yum repolist enabled | grep "mysql.*-community.*"
選擇 MySQL 的版本
yum repolist all | grep mysql # 檢視所有版本
sudo yum-config-manager --disable mysql57-community # 禁用5.7的版本
sudo yum-config-manager --enable mysql80-community # 啟用8.0的版本
# 還可以通過手動編輯/etc/yum.repos.d/mysql-community.repo 檔案來選擇
# 檢視啟用的版本
yum repolist enabled | grep mysql
安裝 MySQL
sudo yum install -y mysql-community-server
啟動 MySQL 伺服器
sudo systemctl enable mysqld # 開啟自啟
sudo systemctl disable mysqld # 關閉自啟
sudo systemctl start mysqld # 啟動服務
sudo systemctl status mysqld # 檢視狀態
sudo service mysqld start # 啟動服務
sudo service mysqld status # 檢視狀態
MySQL 安全設定 (推薦第2種方式)
# 安裝完成,MySQL會預設建立 root 賬戶,密碼儲存在日誌中,通過以下命令檢視賬戶與密碼
$ sudo grep 'temporary password' /var/log/mysqld.log
- 登入並修改預設密碼
# 通過臨時密碼登入並修改root密碼
$ mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'w+daDp=ti40<1';
- 通過 mysql_secure_installation 命令重新設定密碼
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: # 輸入臨時密碼
The existing password for the user account root has expired. Please set a new password.
New password: # 輸入新密碼
Re-enter new password: # 再次輸入
Estimated strength of the password: 100 # 密碼強度評級
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 是否使用輸入的密碼?
By default, a MySQL installation has an anonymous user, # 預設情況下,MySQL有一個匿名使用者
allowing anyone to log into MySQL without having to have # 這個匿名使用者允許任何人登入到MySQL
a user account created for them. This is intended only for # 這只是為了方便測試使用
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production # 在正式環境使用的時候,建議你移除它
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 提示移除匿名使用者
Success.
Normally, root should only be allowed to connect from # 一般情況下,root使用者只允許使用"localhost"方式登入,
'localhost'. This ensures that someone cannot guess at
the root password from the network. # 以此確保,不能被某些人通過網路的方式訪問
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : No # 不允許root遠端登陸?
... skipping.
By default, MySQL comes with a database named 'test' that # 預設情況下,MySQL資料庫中有一個 test database
anyone can access. This is also intended only for testing, # 這也僅僅是為了測試
and should be removed before moving into a production # 在正式環境下,應該移除掉
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y # 確認刪除test資料庫?
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately. # 重新整理許可權表,以確保所有的修改可以立刻生效
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y # 是否重新整理?
Success.
All done!
工具無法連線 mysql 解決辦法
# 檢視所有使用者許可權
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
# 新增root使用者在所有ip可以登入
- 5.*版本
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
- 8.*版本
CREATE USER 'root1'@'%' IDENTIFIED BY 'w+daDp=ti40<1';
GRANT ALL PRIVILEGES ON *.* TO 'root1'@'%' WITH GRANT OPTION;
# 重新整理許可權,使修改生效
flush privileges;
# 工具需要先ssh登入,再登入MySQL
MySQL 官方安裝介紹:
MySQL 安裝
本作品採用《CC 協議》,轉載必須註明作者和本文連結