centos7安裝mysql5.7解壓版

環境:centos7 64位
本教程安裝MySQL是通過編譯過的二進制文件進行安裝。是不針對特定平臺的通用安裝方法,解壓版本的mysql,使用的二進制文件是後綴爲.tar.gz的壓縮文件
1、下載 http://dev.mysql.com/downloads/mysql/

2、解壓縮到/usr/local/下面,mysql的主目錄命名爲mysql,注意後面創建mysql用戶和組可以不用,即默認mysql屬於root用戶和組

     如果是.tar.gz文件直接解壓  我下載的是.tar 文件 執行

    [root@localhost local]# tar -xvf mysql-5.7.16-linux-glibc2.5-i686.tar

  [root@localhost local]# tar xvf mysql-5.7.16-linux-glibc2.5-i686.tar.gz

解壓後的文件夾重命名

[root@localhost local]# mv mysql-5.7.16-linux-glibc2.5-i686 /mysql

3、在mysql下面創建data數據庫文件目錄

[root@localhost local]# mkdir mysql/data


4、創建mysql的用戶組和用戶,並對mysql目錄設置用戶組和用戶(如果快速用root用戶啓動和使用mysql,可以省略這個步驟)

[root@localhost local]# groupadd mysql

[root@localhost local]# useradd mysql -g mysql

[root@localhost local]# cd mysql

[root@localhost mysql]# pwd

/usr/local/mysql

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

[root@localhost mysql]# chgrp -R mysql .


5、初始化mysql並啓動mysql服務

[root@localhost mysql]# cd /usr/local/mysql/bin

[root@localhost bin]# yum install libaio

已加載插件:fastestmirror, refresh-packagekit, security

設置安裝進程

Loading mirror speeds from cached hostfile

* base: mirrors.opencas.cn

* extras: mirrors.btte.net

* updates: mirrors.btte.net

包 libaio-0.3.107-10.el6.i686 已安裝並且是最新版本

無須任何處理

[root@localhost bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

2016-08-11 12:00:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize

2016-08-11 12:00:31 [WARNING] The bootstrap log isn't empty:

2016-08-11 12:00:31 [WARNING] 2016-01-09T04:00:29.262989Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead

2016-08-11T04:00:29.264643Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)

2016-08-11T04:00:29.264653Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)


[root@localhost bin]# cd /usr/local/mysql/support-files

[root@localhost support-files]# ./mysql.server start

Starting MySQL....                                         [確定]

以上操作流程如下圖

如果啓動報錯:“The server quit without updating PID file ”,是因爲pid文件無法在mysql的data中創建,缺少權限,使用如下命令設置data目錄權限。chmod -R 777 /usr/local/mysql/data

6、登錄mysql,此版本最新版不許空密碼登錄,實際上有個初始化密碼保存在/root/.mysql_secret這個文件裏面,用這個密碼第一次登錄後,再修改密碼。因此先cat查看下初始化密碼(隨機的,每次安裝看到的密碼都不一樣):

[root@localhost support-files]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2016-10-30 21:17:12 
q<.yhitwgg!V
[root@localhost support-files]# cd /usr/local/mysql/bin
[root@localhost bin]# mysql -uroot -p

此處特別說明 如果出現-bash: mysql: command not found 錯誤

出下這種錯誤一般是由於安裝了一鍵安裝腳本後沒有對命令做鏈接,可以用下面的命令找到mysql命令的絕對路徑:

find / -name mysql

假設查找到的路徑爲/usr/local/mysql/bin/mysql,可以使用下面的命令設置一個鏈接:

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

然後就可以使用mysql命令了。

 

 利用初始化密碼q<.yhitwgg!V登錄mysql   操作如下圖:

顯示登錄成功,可以執行mysql命令操作了!

未配置的情況下每次登錄需要進入bin目錄下操作:

 

7、 複製配置文件

[root@localhost mysql] cp support-files/my-medium.cnf /etc/my.cnf

此處特別說明 如果出現 下面這種錯誤

找一下my-default.cnf 文件的位置

[root@localhost ~]# find / -name my-default.cnf 

比如在(/usr/local/mysql/support-files/my-default.cnf)位置

作如下操作:
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf

 


8. 將mysqld服務加入開機自啓動項。

*首先需要將scripts/mysql.server服務腳本複製到/etc/init.d/,並重命名爲mysqld。

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

*通過chkconfig命令將mysqld服務加入到自啓動服務項中。

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

*注意服務名稱mysqld就是我們將mysql.server複製到/etc/init.d/時重命名的名稱。

*查看是否添加成功

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

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

9. 重啓系統,mysqld就會自動啓動了。

*檢查是否啓動

[root@localhost mysql]#netstat -anp|grep mysqld

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2365/mysqld 

unix 2 [ ACC ] STREAM LISTENING 14396 2365/mysqld /tmp/mysql.sock

*如果不想重新啓動,那可以直接手動啓動。

[root@localhost mysql]#service mysqld start

Starting MySQL.. SUCCESS!

10 運行客戶端程序mysql,在mysql/bin目錄中,測試能否連接到mysqld。

[root@localhost mysql]#/usr/local/mysql/bin/mysql -uroot -p

password:
Welcome to the MySQLmonitor. Commands end with ; or \g.

Your MySQL connection idis 2

Server version:5.5.29-log MySQL Community Server (GPL)



Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

Oracle is a registeredtrademark of Oracle Corporation and/or its affiliates. Other names may betrademarks of their respective owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql> quit

Bye

*此時會出現mysql>命令提示符,可以輸入sql語句,輸入quit或exit退出。爲了避免每次都輸入mysql的全路徑/usr/local/mysql/bin/mysql,可將其加入環境變量中,在/etc/profile最後加入兩行命令: 

  1. 方法一:  
  2. 在/etc/profile文件中最後一行添加變量export PATH=$PATH:/usr/local/mysql/bin
    【對所有用戶生效(永久的)】  
  3. 用VI在文件/etc/profile文件中增加變量,該變量將會對Linux下所有用戶有效,並且是“永久的”。  
  4. 要讓剛纔的修改馬上生效,需要執行以下代碼  
  5. # source /etc/profile  
  6.   
  7. 方法二:  
  8. 在用戶目錄下的.bash_profile文件中增加變量【對單一用戶生效(永久的)】  
  9. 用VI在用戶目錄下的.bash_profile文件中增加變量,改變量僅會對當前用戶有效,並且是“永久的”。  
  10. 要讓剛纔的修改馬上生效,需要在用戶目錄下執行以下代碼  
  11. # source .bash_profile  
  12.   
  13. 方法三:  
  14. 直接運行export命令定義變量【只對當前shell(BASH)有效(臨時的)】  
  15. 在shell的命令行下直接使用[export變量名=變量值]定義變量,該變量只在當前的shell(BASH)或其子shell(BASH)下是有效的,shell關閉了,變量也就失效了,再打開新shell時就沒有這個變量,需要使用的話還需要重新定義。  
  16. 例如:export PATH=$PATH:/usr/local/mysql/bin

這樣就可以在shell中直接輸入mysql命令來啓動客戶端程序了

[root@localhost mysql]#mysql -uroot -p

 

password:
Welcome to the MySQLmonitor. Commands end with ; or \g.

Your MySQL connection idis 3

Server version:5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql> 



11、改mysql的root密碼,新密碼在此爲'123456'

mysql> set password=password('123456');

Query OK, 0 rows affected, 1 warning (0.00 sec)


12、設定遠程登錄mysql。在Linux下爲了安全,默認是不允許mysql本機以外的機器訪問mysql數據庫服務,因此需要重新授權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> select Host,User from user;

+-----------+-----------+

| Host | User |

+-----------+-----------+

| % | root |

| localhost | mysql.sys |

| localhost | root |

+-----------+-----------+

3 rows in set (0.00 sec)


mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' identified by '000000';

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


授權語句最後的‘000000’是mysql數據庫root用戶的新密碼。


13、非必要的步驟,如果遠程連不上,估計是防火牆的問題,關閉試試:

[root@localhost mysql]# service iptables stop

setenforce 0iptables:將鏈設置爲政策 ACCEPT:filter [確定]

iptables:清除防火牆規則: [確定]

iptables:正在卸載模塊: [確定]

[root@localhost mysql]# setenforce 0

setenforce: SELinux is disable

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