Linux(Centos 7) 安裝 Mysql 5.7 步驟 配置過程

一、檢查是否已經安裝了mysql

 [root@localhost /]# rpm -qa | grep -i mysql

如果有,則使用刪除語句刪除

 [root@localhost /]# yum -y remove mariadb-libs-5.5.56-2.el7.x86_64

再使用查詢命令,查找mysql相關的文件

 [root@localhost /]# find / -name mysql

然後把存在的文件都刪除

 [root@localhost /]# rm –rf {目錄名}

二、下載mysql安裝包

進入包下載所放位置下載

 [root@localhost /]# cd /usr/local/src
 [root@localhost src]# wget  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

進行解壓

     [root@localhost src]# tar zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz 
     [root@localhost src]# ls
mysql-5.7.25-linux-glibc2.12-x86_64  mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz   

移位置並改名

     [root@localhost src]# mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql

      [root@localhost src]# cd /usr/local/mysql
  [root@localhost mysql]# ls
   bin  COPYING    docs  include  lib  man  README  share  support-files

三、配置初始化mysql

添加用戶組和新用戶名

      [root@localhost mysql]# groupadd mysql
      [root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql   

創建data目錄

     [root@localhost mysql]# mkdir data

修改mysql目錄用戶爲剛剛新建的mysql組中的mysql用戶

       [root@localhost mysql]# chown -R mysql:mysql ./  

安裝必要文件包

        [root@localhost mysql]# yum install -y libaio

初始化安裝mysql數據庫

  [root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql          -- datadir=/usr/local/mysql/data --initialize
 2019-07-07T07:51:36.066665Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-07T07:51:37.197916Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-07-07T07:51:37.299392Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-07-07T07:51:37.823229Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0c916440-a08c-11e9-87be-000c2954a0d9.
2019-07-07T07:51:37.846198Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-07-07T07:51:37.847191Z 1 [Note] A temporary password is generated for root@localhost: yrntyj)qo6Nd

修改my.conf配置文件,從版本5.7.18開始,mysql免安裝二進制包中就不包含該文件了,即不需要my.conf文件也可以正常運行;my.conf文件中配置的選項會在命令行啓動mysql的時候作爲參數進行啓動,爲了後面搭建mysql主從環境方便,下面可以添加了一個簡單的my.conf文件作爲實例(如果只是單純的搭建一個mysql實例,可以直接忽略此步驟),使用vim /etc/my.conf命令,如果在該目錄上不存在則會新建

       [mysqld]
    character_set_server=utf8
   init_connect='SET NAMES utf8'
   basedir=/usr/local/mysql
   datadir=/usr/local/software/data
   socket=/usr/local/software/mysql.sock
    #設置忽略大小寫(簡單來說就是sql語句是否嚴格),默認庫名錶名保存爲小寫, 不區分大小寫
    lower_case_table_names = 1
    # 開啓ip綁定
   bind-address = 0.0.0.0
  [mysqld_safe]
  log-error=/var/log/mysqld.log
  pid-file=/usr/local/mysql/data/mysqld.pid
   #指定客戶端連接mysql時的socket通信文件路徑
  [client]
  socket=/usr/local/mysql/mysql.sock
  default-character-set=utf8

四、其他配置

添加開機啓動

         [root@localhost mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld

修改mysqld,使用vim /etc/init.d/mysqld 命令 修改以下代碼部分

             basedir=/usr/local/mysql
       datadir=/usr/local/mysql/data

設置開機啓動

      [root@localhost mysql]# chkconfig --add mysqld

爲了可以在任意目錄上都可以使用mysql命令登錄mysql,將mysql安裝目錄配置到環境變量中,在/etc/profile文件的末尾添加以下代碼

        export PATH=$PATH:/usr/local/mysql/bin    

使配置文件的配置立即生效

        [root@localhost mysql]# source /etc/profile    

重啓mysql服務,並且使用mysql的root用戶登錄mysql

           [root@localhost mysql]# service mysqld restart
    Shutting down MySQL.... SUCCESS! 
    Starting MySQL.. SUCCESS! 
      [root@localhost mysql]# mysql -uroot -p
     Enter password: 
     Welcome to the MySQL monitor.  Commands end with ; or \g.
     Your MySQL connection id is 2
   Server version: 5.7.25 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> 

修改root用戶的密碼爲root,並且刷新

    mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

  mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)

此時mysql數據庫只能在本機上使用mysql命令進行登錄,還無法使用navicat等數據庫可視化工具進行遠程登錄,下面設置允許root用戶遠程連接數據庫

      mysql> use mysql;
    Reading table information for completion of table and column names
 You can turn off this feature to get a quicker startup with -A

     Database changed
  mysql> update user set user.Host='%' where user.User='root';
 Query OK, 1 row affected (0.01 sec)
 Rows matched: 1  Changed: 1  Warnings: 0

    mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)

    mysql> exit
Bye

查看防火牆

        [root@localhost mysql]# firewall-cmd --state
  running     

打開3306端口

        [root@localhost mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent

success

防火牆重啓

         [root@localhost mysql]# firewall-cmd --reload

success

至此,可以直接使用navicat等連接數據庫

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