1. 程式人生 > 其它 >Linux CentOS 7.8安裝MySQL5.7

Linux CentOS 7.8安裝MySQL5.7

CentOS和MySQL

  • CentOS Linux release 7.8.2003 (Core)
  • MySQL 5.7

1.安裝MySQL源

# 下載
bash > wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安裝 MySQL 源
bash > yum localinstall mysql57-community-release-el7-11.noarch.rpm

 

2.檢查源是否安裝成功

bash > yum repolist enabled | grep "mysql.*-community.*"

頁面輸出資訊表示源安裝成功

mysql-connectors-community/x86_64 MySQL Connectors Community                 165
mysql-tools-community/x86_64      MySQL Tools Community                      115
mysql57-community/x86_64          MySQL 5.7 Community Server                 444

  

3.使用yum install 命令安裝MySQL

bash > yum install -y mysql-community-server

4.啟動MySQL服務

CentOs7 下 啟動/關閉/重啟服務的命令是 systemctl start|stop|restart

bash > systemctl start mysqld

systemctl status 命令檢視服務執行狀態

bash > systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2020-07-28 21:09:44 CST; 57s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 25677 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 25628 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 25681 (mysqld)
CGroup: /system.slice/mysqld.service
└─25681 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

7月 28 21:09:38 iZm5ebq5g3dxxmygidjc5cZ systemd[1]: Starting MySQL Server...
7月 28 21:09:44 iZm5ebq5g3dxxmygidjc5cZ systemd[1]: Started MySQL Server.

 這裡可看到MySQL執行的狀態active (running) 

5.設定開機啟動MySQL 服務

bash > systemctl enable mysqld
# 過載所有修改過的配置檔案
bash > systemctl daemon-reload

重啟ContOs 伺服器測試.(沒有問題)

6.檢視MySQL預設密碼

MySQL 安裝完成之後,生成的預設密碼在 /var/log/mysqld.log 檔案中。使用 grep 命令找到日誌中的密碼。

bash > grep 'temporary password' /var/log/mysqld.log
檢視初始密碼

7.首次通過初始密碼登入後,使用以下命令修改密碼

bash > mysql -u root -p
#輸入密碼 
#執行修改密碼語句.
bash > ALTER USER 'root'@'localhost' IDENTIFIED BY 'Vine@yyy1';

8.新增一個用於遠端連線的賬號,如Navicat

bash > GRANT ALL PRIVILEGES ON *.* TO 'xianglin'@'%' IDENTIFIED BY 'Xiang@lin!1996' WITH GRANT OPTION;
#Query OK, 0 rows affected, 1 warning (0.00 sec) 

9.設定預設編碼為 utf8

MySQL 安裝後預設不支援中文,需要修改編碼。

現在檢視編碼:

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.00 sec)

修改 /etc/my.cnf 配置檔案,在相關節點(沒有則自行新增)下新增編碼配置,如下:

[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

  

儲存之後使用systemctl restart mysqld 檢視編碼。

mysql> 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.00 sec)

預設配置檔案路徑:

配置檔案:/etc/my.cnf
日誌檔案:/var/log/mysqld.log
服務啟動指令碼:/usr/lib/systemd/system/mysqld.service
socket檔案:/var/run/mysqld/mysqld.pid


轉自:https://www.jianshu.com/p/19291fb17b99
來源:簡書