Linux下MySQL的安裝和配置

CentOS 7.x及以上mysql安裝和配置

在安裝MySQL之前需要先將服務器的3306端口打開

  1. 下載MySQL源安裝包: wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

  2. 安裝MySQL源: yum localinstall mysql57-community-release-el7-8.noarch.rpm

  3. 安裝MySQL: yum install mysql-community-server

  4. 設置開機啓動MySQL服務:systemctl enable mysqld

    # CentOS7.x以上的版本,程序控制指令
    systemctl enable 程序名     -  開機自啓動程序/服務
    systemctl start  程序名     -  啓動程序/服務
    systemctl stop   程序名     -  關閉程序/服務
    systemctl restart  程序名   -  重啓程序/服務
    systemctl disable  程序名   -  禁用程序/服務
    注意: 這兒的程序名一般需要在你安裝的程序的原名的基礎上加d
    
    # CentOS7.x一下的版本,不能用systemctl來控制,需要直接使用mysql去調用相關指令(因爲一般不會向低版本發展,所以可以不用管,如果需要可以查資料,網上很多)
    
  5. 啓動MySQL服務:systemctl start mysqld

  6. 設置MySQL登錄密碼

    這個過程比較麻煩,請嚴格按照以下步驟完成!

    第一步: 執行 systemctl stop mysqld 關閉mysql服務

    第二步: 使用vim打開mysql配置文件 \etc\my.cnf,並且在末尾添加兩行代碼然後保存退出

    validate_password=off  
    skip-grant-tables
    

    在這裏插入圖片描述

    第三步:執行 systemctl start mysqld開啓mysql服務

    第四步:在shell中執行 mysql -u root -p 設置密碼(注意密碼設置好以後要記住),並且按以下提示輸入內容

    [root ~]# mysql -u root -p   (需要輸入的指令)
    Enter password:   (這個地方自己設置密碼,輸入的時候不會有顯示提示)
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 10
    Server version: 5.7.28 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> update mysql.user set authentication_string=password("yuting123456") where user="root";    (設置連接密碼,將yuting123456設置爲自己的密碼就行,最好和上面的密碼保持一致,否則容易混亂)
    Query OK, 1 row affected, 1 warning (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 1
    
    mysql> flush privileges;     (刷新權限)
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit;   (退出mysql指令模式)
    Bye
    

    第五步:執行systemctl stop mysqld 關閉mysql服務,然後重新打開mysql配置文件,註釋或者刪除之前添加的 skip-grant-tables ,保存退出後再執行 systemctl start mysqld 開啓mysql服務
    在這裏插入圖片描述

    # 這個過程比較麻煩,請嚴格按照以下步驟完成!
    第一步: 執行 'systemctl stop mysqld' 關閉mysql服務
    第二步: 使用vim打開mysql配置文件 '\etc\my.cnf',並且在末尾添加兩行代碼(注意:只需要複製引號中的內容)然	   後保存退出:
    		'validate_password=off   #關閉mysql密碼強度驗證'
    		'skip-grant-tables    #跳過密碼驗證'
    		
    第三步:執行 'systemctl start mysqld' 開啓mysql服務
    第四步:在shell中執行以'mysql -u root -p'設置密碼(注意:密碼設置好以後要記住)
    
  7. 設置mysql能夠遠程訪問
    執行mysql -u root -p 登錄mysql添加遠程連接權限。登錄的密碼就是之前設置的密碼

       [root ~]# mysql -u root -p   (需要輸入的指令)
       Enter password:     (輸入密碼)
       Welcome to the MySQL monitor.  Commands end with ; or \g.
       Your MySQL connection id is 6
       Server version: 5.7.28 MySQL Community Server (GPL)
       
       Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
       
       Oracle is a registered trademark of Oracle Corporation and/or its
       affiliates. Other names may be trademarks of their respective
       owners.
       
       Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
       
       mysql> grant all privileges on *.* to 'root'@'%' identified by 'yuting123456' with grant option;     (讓所有的ip地址的設備都可以通過root賬號並且密碼是yuting123456進行連接;這個地方可以將%換成指定的某一個ip地址,%表示所有的ip; 如果出錯執行: ALTER USER USER() IDENTIFIED BY '自己的密碼';)
       Query OK, 0 rows affected, 1 warning (0.00 sec)
       
       mysql> flush privileges;  (刷新權限)
       Query OK, 0 rows affected (0.00 sec)
    
  8. 終於結束啦!!!

如果設置密碼的時候失敗,可以嘗試通過執行 SET PASSWORD = PASSWORD(‘寫你的新密碼‘);來進行設置。

原創聲明,如需轉載請標明出處~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章