CentOS下通過YUM安裝MySQL5.7
1.進入到要存放安裝包的位置
cd /home/lnmp
2.查看系統中是否已安裝 MySQL 服務,以下提供兩種方式:
rpm -qa | grep mysql yum list installed | grep mysql
3.如果已安裝則刪除 MySQL 及其依賴的包:
yum -y remove mysql-libs.x86_64
4.下載 mysql57-community-release-el7-8.noarch.rpm 的 YUM 源:
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
5.安裝 mysql57-community-release-el7-8.noarch.rpm:
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
安裝完後,得到如下兩個包:
mysql-community.repo
mysql-community-source.repo
6.安裝 MySQL,出現提示的話,一路 Y 到底
yum install mysql-server
安裝完畢後,在 /var/log/mysqld.log 文件中會自動生成一個隨機的密碼,我們需要先取得這個隨機密碼,以用於登錄 MySQL 服務端:
grep"password" /var/log/mysqld.log
將會返回如下內容,末尾字符串就是密碼,把它復制下來:
A temporary password is generated for root@localhost: hilX0U!9i3_6
7.登錄到 MySQL 服務端並更新用戶 root 的密碼:
註意:由於 MySQL5.7 采用了密碼強度驗證插件 validate_password,故此我們需要設置一個有一定強度的密碼;
mysql -u root -philX0U!9i3_6
設置用戶 root 可以在任意 IP 下被訪問:
grant all privileges on *.* to root@"%" identified by "新密碼";
設置用戶 root 可以在本地被訪問:
grant all privileges on *.* to root@"localhost" identified by "新密碼";
刷新權限使之生效:
flush privileges;
更新 MySQL 的用戶 root的密碼:
set password = password(‘新密碼‘);
輸入 exit 後用新密碼再次登錄看看吧!
8.MySQL控制命令:啟動、停止、重啟、查看狀態
service mysqld start
service mysqld stop
service mysqld restart
service mysqld status
9.查看 MySQL 的字符集:
show variables like ‘%character%‘;
+-----------------------------------+---------------------------------------+
| Variable_name | Value |
+-----------------------------------+---------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+------------------------------------+---------------------------------------+
8 rows in set (0.01 sec)
10.查看指定的數據庫中指定數據表的字符集,如查看 mysql 數據庫中 servers 表的字符集:
show table status from mysql like ‘%servers%‘;
11.查看指定數據庫中指定表的全部列的字符集,如查看 mysql 數據庫中 servers 表的全部的列的字符集:
show full columns from servers;
12.設置 MySQL 的字符集為 UTF-8:
打開 /etc 目錄下的 my.cnf 文件(此文件是 MySQL 的主配置文件):
cd /etc/my.cnf
在 [mysqld] 前添加如下代碼:
[client]
default-character-set=utf8
在 [mysqld] 後添加如下代碼:
character_set_server=utf8
再次查看字符集:
show variables like ‘%character%‘;
+-----------------------------------+---------------------------------------+
| Variable_name | Value |
+-----------------------------------+---------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+------------------------------------+---------------------------------------+
8 rows in set (0.01 sec)
13. 存放數據庫文件的目錄
cd /var/lib/mysql
14. MySQL 的日誌記錄文件
vim /var/log/ mysqld.log
15. MySQL 采用的 TCP/IP 協議傳輸數據,默認端口號為 3306,我們可以通過如下命令查看:
netstat -anp
16. 忘記密碼時,可用如下方法重置:
service mysqld stop mysqld_safe --user=root --skip-grant-tables --skip-networking & mysql -u root
進入MySQL後
use mysql; update user set password=password("new_password") where user="root"; flush privileges;
本文整理自:
https://www.linuxidc.com/Linux/2016-06/132676.htm
CentOS下通過YUM安裝MySQL5.7