1. 程式人生 > >Linux(centos_7)上安裝MySql5.7

Linux(centos_7)上安裝MySql5.7

使用wget從官網下載tar包

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

資源獲取結束

將MySql安裝至/usr/local/mysql目錄

解壓安裝包

    tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

在這裡插入圖片描述

移動

    mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/

在這裡插入圖片描述
在這裡插入圖片描述

重新命名

 mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

在這裡插入圖片描述

新建data目錄

    mkdir /usr/local/mysql/data

在這裡插入圖片描述

新建mysql使用者、mysql使用者組

mysql使用者組

    groupadd mysql

mysql使用者

     useradd mysql -g mysql
  • 將/usr/local/mysql的所有者及所屬組改為mysql
      chown -R mysql.mysql /usr/local/mysql
  • 配置

    
    /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
    

在這裡插入圖片描述
出現如上圖所示錯誤
則輸入如下命令

    /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

出現如下所示錯誤

    /usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

則執行以下命令:


   yum -y install numactl

在這裡插入圖片描述
得出如下結果後繼續安裝,輸入如下命令

  /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

繼續出現如下所示錯誤

    /usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

則執行以下命令:

     yum install -y libaio

完成後繼續執行

     /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

在這裡插入圖片描述

編輯配置檔案

    vi /etc/my.cnf

修改前

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

修改後

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密碼驗證
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#
# include all files from the config directory
#

開啟服務

將mysql加入服務

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

開機自啟

    chkconfig mysql on

開啟

    service mysql start
  • 設定密碼

登入(由於/etc/my.cnf中設定了取消密碼驗證,所以此處密碼任意)

    /usr/local/mysql/bin/mysql -u root -p

操作mysql資料庫

    >>use mysql;

修改密碼

    >>update user set authentication_string=password('你的密碼') where user='root';
    
    >>flush privileges;
    
    >>exit;

將/etc/my.cnf中的skip-grant-tables刪除

登入再次設定密碼

如果出現操作不了資料庫的狀況,就再次設定skip-grant-tables,再次設定密碼

   /usr/local/mysql/bin/mysql -u root -p

 

    >>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改後的密碼';
    
    >>exit;

允許遠端連線

    /usr/local/mysql/bin/mysql -u root -p

    >>use mysql;
    
    >>update user set host='%' where user = 'root';
    
    >>flush privileges;
    
    >>eixt;

新增快捷方式

    ln -s /usr/local/mysql/bin/mysql /usr/bin

注意:本Linux伺服器是在阿里雲的伺服器,遠端訪問資料庫時,需要在阿里雲上開放埠