Centos7安裝mysql5.7.27 史上最全最簡單的教程

有欲無求發表於2020-10-13

1、檢視Linux中是否安裝了Mysql或者Maridb(Centos7系統會預設安裝Maridb)

rpm -ivh |grep mysql

rmp -ivh |grep mari*

2、若存在會列印mysql或者maridb資訊,若不存在則什麼都不會顯示。解除安裝mysql或者maridb

      1)解除安裝mysql及maridb的元件

yum -y remove mysql*
yum -y remove mari*

      2)刪除mysql或maridb的資料

rm -rf /var/lib/mysql/*

3、下載mysql的repo源


   [root@localhost ~]# cd /usr/local/src/  //注:該路徑自己選擇
   [root@localhost src]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm 
 
   [root@localhost src]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm 
 
   [root@localhost src]#  yum -y install mysql-server

注:若無wget命令則先安裝wget命令

yum -y install wget

4、配置my.cnf檔案

輸入vim /etc/my.cnf進入配置檔案,按ins啟動編輯,寫入下面程式碼,然後按esc輸入:wq!強制儲存並退出

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id=1
expire_logs_days=3

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5、啟動mysql服務,重置密碼

一、啟動mysql

      1)啟動mysql服務

systemctl start mysqld.service

      2)檢視mysql服務是否正常啟動

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

cdh01 systemd[1]: Starting MySQL Server...
cdh01 systemd[1]: Started MySQL Server.

二、登入mysql,重製密碼

      1)登入mysql

mysql -u root -p

      注:會出現如下情況,為密碼輸入錯誤,拒絕訪問的錯誤資訊

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)  

      2)安裝好mysql之後系統會生成一個隨機密碼,這個密碼既複雜又不方便,我們先不講怎麼找到隨機密碼,如何用這個隨機密碼登入,我們採取的策略是直接修改成自定義密碼,使用自定義密碼登入mysql

            1、停止mysql服務

systemctl stop mysqld.service

            2、設定mysql無密碼登入

      編輯/etc/my.cnf檔案,新增如下程式碼

skip-grant-tables

            3、重啟mysql服務

systemctl start mysqld.server

      3)重新登入mysql

此時輸入登入命令,會提示輸入密碼直接回車即可

mysql -u root -p

      4)修改root使用者的密碼

            1、首先切換到mysql庫

use mysql;

            2、我們輸入命令修改密碼,下面程式碼中password( )括號中裡面的內容為自定義的新密碼

輸入命令這裡列出3個,不同的版本修改密碼語句不同。我使用的是第三條命令,然後密碼才修改成功。大家可以用這三條語句試一試,這3條語句應該可以解決大多數的修改密碼無效問題。

UPDATE user SET password=PASSWORD(‘root’)WHERE user=’root’;
update user set password=password("root") where user="root";
update mysql.user set authentication_string=password('root') where user='root' ;

            3、修改後重新整理一下mysql許可權相關的表

flush privileges;

            4、退出mysql客戶端

exit;

      5)刪除之前新增的到/etc/my.cnf中的程式碼

skip-grant-tables

      6)重啟mysql服務

systemctl restart mysqld.service

      7)再次登入,使用修改後的密碼顯示登入成功

6、啟動3306埠,授權遠端可訪問mysql資料庫

使用遠端客戶端(navicat等)連線mysql需要確保兩點

    a、3306埠有沒有被防火牆禁用

    b、mysql遠端訪問許可權是否已開啟

      一、首先開啟3306埠,因為這個埠預設會被防火牆禁掉,否則授權也沒有用

firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload

        二、開啟mysql遠端訪問許可權

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

若密碼簡單會報如下錯誤:

ERROR 1819 (HY000): Your password does not satisfy ...

此時需要調整:

       1)、mysql密碼強度限制:

set global validate_password_policy=0;

      2)、mysql密碼長度限制(密碼長度設定為4,根據實際情況自己調整長度)

set global validate_password_length=4;

      3)、更新授權表,使以上調整生效

flush privileges;

 

相關文章