1. 下載mysql軟件包
我們可以先去官網查看我們應該下載哪個版本
網址: https://downloads.mysql.com/archives/community/
然后到鏡像網站去下載,速度會快很多,例如,清華大學鏡像網站,下載5.7版本的軟件包:
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.33-el7-x86_64.tar.gz
2. 解壓
cd /feng tar -zxvf mysql-5.7.33-el7-x86_64.tar.gz # 解壓目錄過長,可以建個軟鏈接,或者改名字 ln -s mysql-5.7.33-el7-x86_64 ./mysql
3. 配置
出於安全考慮,應該避免用root用戶啟動mysql,所以要創建一個mysql的啟動用戶
groupadd dbAdmin ## -r 代表建立一個系統用戶,不會創建家目錄,-g 指定用戶組 ## 建議使用man 命令查看 命令詳情 useradd -r -g dbAdmin dbAdmin
創建mysql 數據目錄,賦予dbAdmin 用戶權限
mkdir -p /feng/data/mysql chown -R dbAdmin:dbAdmin /feng/data/mysql
第二步 解壓的安裝包,也要賦予權限

編寫配置文件: vi /etc/my.cnf
[mysqld] bind-address=0.0.0.0 port=3306 user=dbAdmin basedir=/feng/mysql datadir=/feng/data/mysql socket=/feng/mysql/mysql.sock pid-file=/feng/mysql/mysqld.pid log-error=/var/log/mysqld.log #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true [client] port=3306 socket=/feng/mysql/mysql.sock
創建上述配置中的文件, 授予dbAdmin 權限
touch /feng/mysql/mysql.sock touch /feng/mysql/mysqld.pid touch /var/log/mysqld.log
4. 初始化
cd /feng/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/feng/mysql/ --datadir=/feng/data/mysql --user=dbAdmin --initialize
初始化成功后,會有初始密碼在日志文件里
cat /var/log/mysqld.log |grep "password" 2021-02-21T02:07:12.865619Z 1 [Note] A temporary password is generated for root@localhost: jrwb>b?AX18l
5. 啟動
bin/mysqld_safe --defaults-file=/etc/my.cnf --user=dbAdmin &
6. 客戶端連接
bin/mysql -u root -p
遇到以下報錯及解決方法:

登錄成功后。修改密碼:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpasswd');
7 其他
Failed to restart mysqld.service: Unit mysqld.service not found.
[root@localhost mysql]# service mysqld restart Redirecting to /bin/systemctl restart mysqld.service Failed to restart mysqld.service: Unit mysqld.service not found.
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
使用DataGrip 連接時報錯如下,說明mysql 沒有允許其他客戶端連接,只能localhost連接
[HY000][1130] null, message from server: "Host '192.168.31.62' is not allowed to connect to this MySQL server".

解決如下:
update user set host = '%' where user = 'root';
