CentOS-7下安裝MySQL5.6.22

來源:互聯網
上載者:User

標籤:

CentOS7下安裝MySQL5.6.22(當前最新版本:2015-01-17)

轉載請註明出處:jiq?欽‘s technical Blog

一  安裝環境

(1)CentOS版本:CentOS-7

查看方法:

[[email protected] 案頭]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)

來源:官網下載

:http://www.centos.org/

(2)MySQL版本:MySQL-5.6.22

來源:官網下載

:https://edelivery.oracle.com/EPD/Search/handle_go

或者http://pan.baidu.com/s/1dDu6n9R

名稱:MySQL Database 5.6.22 RPM for Oracle Linux / RHEL 7 x86 (64bit)

二  安裝方式選擇

在網上搜了一下,Linux下安裝MYSQL有三種方式:

1 通過yum命令線上下載安裝

2 下載離線rpm安裝包安裝

3 下載源碼編譯安裝

方式1不打算用,因為我們大部分專案服務器是不能直接上Internet的,網上關於線上安裝的教程很多,方式3對於只要應用MYSQL的人來說沒必要多此一舉,不過這裡有個比較好的網站可供大家參考:http://www.ecstoredev.com/webserver/linux/centos7-mysql-5-6-10.html,我選擇方式2。

三  安裝步驟

1. 解壓下載的zip包,會發現有以下幾個rpm包:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-embedded-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-test-advanced-5.6.22-1.el7.x86_64.rpm

 

2. 卸載MariaDB

如果直接點擊rpm包安裝會得到錯誤提示。因為CentOS的預設資料庫已經不再是MySQL了,而是MariaDB,為什麼呢?

MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL授權許可。開發這個分支的原因之一是:甲骨文公司收購了MySQL後,有將MySQL閉源的潛在風險,因此社區採用分支的方式來避開這個風險。MariaDB的目的是完全相容MySQL,包括API和命令列,使之能輕鬆成為MySQL的代替品。

查看當前安裝的mariadb包:

[[email protected] 案頭]# rpm -qa | grep mariadb

將它們統統強制性卸載掉:

[[email protected] 案頭]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64

[[email protected] 案頭]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64

[[email protected] 案頭]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64

 

3. 安裝MYSQL

雙擊下面三個包進行自動安裝:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

提示:其實第二個包devel我也不知道是幹什麼的,也不知道是不是必須的(上網搜了一下應該不是必須的),沒有測試是否必須就已經點來裝上了,也不想花時間去測試是否必須了,有測試過的朋友麻煩留言告知。

 

4. 啟動MYSQL

[[email protected] 案頭]#service mysql start

得到錯誤:ERROR!The server quit without updating PID file

從這個網站(地址http://www.jb51.net/article/33810.htm)發現引起這個錯誤的可能性有很多,比如設定檔有小問題也會引起這個錯誤,我們這裡主要是因為:selinux惹的禍,如果是centos系統,預設會開啟selinux。解決方案是關閉它,開啟/etc/selinux/config,把SELINUX=enforcing改為SELINUX=disabled後存檔退出重啟機器。

然後再啟動mysql就沒問題了:

[[email protected] 案頭]#service mysql start

查看MySQL運行狀態:

[[email protected] 案頭]# service mysql status

 SUCCESS! MySQL running (2377)

 

5. 預設root使用者登入MYSQL

[[email protected] 案頭]# mysql -u root -p

Enter password:

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

發現有有錯誤,然後在網上查了一下說使用下面命令修改root初始化密碼:

[[email protected] 案頭]# /usr/bin/mysqladmin -u root password ‘passok‘

/usr/bin/mysqladmin: connect to server at‘localhost‘ failed

error: ‘Accessdenied for user ‘root‘@‘localhost‘ (using password: NO)‘

發現MYSQL資料庫預設的root使用者還是沒辦法設定密碼進行登入,需要做一下操作:

 

重設MySQL中root使用者密碼及驗證

還是不行,然後在網上又找到一個重設MySQL中root使用者密碼及驗證的方法:

(1)     停止MySQL服務

[[email protected] 案頭]# service mysql stop

Shutting down MySQL.. SUCCESS!

(2)     輸入繞過密碼認證命令

[[email protected] 案頭]# mysqld_safe --user=mysql --skip-grant-tables--skip-networking &

[1] 5807

150117 22:23:31 mysqld_safe Logging to ‘/var/lib/mysql/bogon.err‘.

150117 22:23:31 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql

(3)     輸入登入使用者命令

[[email protected] 案頭]# mysql -u root mysql

Reading table information for completion oftable and column names

You can turn off this feature to get aquicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version:5.6.22-enterprise-commercial-advanced MySQL Enterprise Server - AdvancedEdition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.

(4)     輸入修改root密碼SQL語句

mysql> UPDATEuser SET Password=PASSWORD(‘passok‘) where USER=‘root‘;

Query OK, 4 rows affected (0.04 sec)

Rows matched: 4  Changed: 4 Warnings: 0

(5)     輸入資料重新整理命令

mysql> FLUSHPRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

(6)     退出

mysql> quit

Bye

(7)     啟動MYSQL

[[email protected] 案頭]# service mysql start

Starting MySQL SUCCESS!

 

登入mysql,查看所有資料庫:

[[email protected] 案頭]# mysql -u root -p

mysql> show  databases;

ERROR 1820 (HY000):You must SET PASSWORD before executing this statement

 

還是有錯誤啊,提示要再設定一下密碼:

mysql> SETPASSWORD = PASSWORD(‘passok‘);

Query OK, 0 rows affected (0.00 sec)

顯示資料庫:

mysql> show  databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

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

4 rows in set (0.00 sec)

進入資料庫建立表、顯示表:

mysql> use  test;

Database changed

mysql> show  tables;

Empty set (0.02 sec)

mysql>create table testTable(name char(15) not null,passwd char(15) not null);

Query OK, 0 rows affected (0.87 sec)

mysql> show  tables;

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

| Tables_in_test |

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

| testTable      |

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

1 row in set (0.00 sec)

 

備忘:我的設定的密碼是“passok”。大家在執行showdatabases收到錯誤ERROR 1820 (HY000): You must SET PASSWORD before executing this statement後也可以試試以下面方式登入mysql來執行showdatabases是否就不會有這個錯誤,我沒有條件測試了:

登入MySQL介面:mysql –uroot -p(修改的新密碼)

例如:mysql -u root -p123456

 

mysql安裝後三個主要的目錄及其功能:

/var/lib/mysql 資料庫檔案

/usr/share/mysql 命令及設定檔

/usr/bin mysqladmin、mysqldump等命令

 

6. windows7上使用workbench串連到遠端MySQL Server

(1)      下載安裝workbench(MySQL Workbench 6.2.4 MSI for Windows x86(64bit))

(2)      Windows下安裝mysql workbench需要具備以下環境:

Microsoft .NET Framework 4 Client Profile

Microsoft Visual C++ 2013 RedistributablePackage (x64)

第一個就不用說了,第二個在:

http://www.microsoft.com/zh-CN/download/details.aspx?id=40784

(3)安裝完成之後開啟,進行串連配置:

發現連不上。

開啟cmd命令列輸入telnet 192.168.1.108 3306也提示串連不上(BTY:windows7預設沒有安裝telnet,需要通過控制台中的開啟或者關閉windows功能來開啟telnet用戶端功能)。

 

如果要想遠端訪問MYSQL資料庫,還需要:

(1)      給指定使用者賦予遠端訪問mysql資料庫的許可權;

(2)      配置防火牆放開對3306連接埠的限制;

 

1給指定使用者賦予遠端訪問mysql資料庫的許可權

授權命令是:

grant 許可權1,許可權2,…許可權n on 資料庫名.表名 to使用者名稱@使用者地址 identified by‘口令’

[[email protected] 案頭]# mysql -uroot-ppassok -e "GRANT ALL PRIVILEGES ON *.* TO‘root‘@‘%‘ IDENTIFIED BY ‘passok‘ WITH GRANT OPTION;"

Warning: Using a password on the commandline interface can be insecure.

用root使用者的身份執行grant命令(-e參數表示執行一段sql命令),含義是:把在所有資料庫的所有表的所有許可權賦值給位於所有IP地址的root使用者。如果你只想讓位於192.168.1.108機器上面的root使用者訪問,命令如下:

mysql -uroot -ppassok -e "GRANT ALL PRIVILEGES ON *.* TO‘root‘@‘192.168.1.108‘ IDENTIFIED BY ‘passok‘ WITH GRANT OPTION;"

 

2 配置防火牆放開3306連接埠的限制

CentOS 7.0版本的防火牆,預設使用的是firewall,與之前的版本使用iptables是不一樣,經過我的測試,只要firewall處於開啟狀態,就不可能遠端訪問MYSQL資料庫。

首先將firewall關閉:

[[email protected] 案頭]# systemctl stop firewalld.service #停止firewall

[[email protected] 案頭]# systemctl disable firewalld.service #禁止firewall開機啟動

CentOS雖然預設的不是iptables,但是也是已經安裝好的,然後我按照網上的方法,為3306連接埠配置開放規則:

[[email protected] 案頭]# vi /etc/sysconfig/iptables

# sample configuration for iptables service

# you can edit this manually or usesystem-config-firewall

# please do not ask us to add additionalports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --stateRELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp--dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-withicmp-host-prohibited

-A FORWARD -j REJECT --reject-withicmp-host-prohibited

COMMIT

 

然後重啟防火牆:

[[email protected] 案頭]# service iptables restart

然後立刻用mysql workbench測試,發現還是一樣連不上。

 

然後執行下面命令永久關閉iptables:

[[email protected] bin]# chkconfig  iptables off

注意:正在將請求轉寄到“systemctl disable iptables.service”。

然後立刻用mysql workbench測試,發現還是一樣連不上。

 

重啟CentOS系統,串連成功

看來要重啟才會生效。

 

進一步測試:

查看firewall狀態是關閉的:

[[email protected] bin]# service  firewall status

Redirecting to /bin/systemctl status  firewall.service

firewall.service

  Loaded: not-found (Reason: No such file or directory)

   Active: inactive (dead)

 

查看iptables狀態也是關閉的:

[[email protected] bin]# service  iptables status

Redirecting to /bin/systemctl status  iptables.service

iptables.service - IPv4 firewall withiptables

  Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

   Active: inactive (dead)

 

然後我又啟動iptables防火牆,還是能夠訪問,然後把/etc/sysconfig/iptables中的規則全部注釋掉之後,還是能夠訪問,重啟還是可以,不知道為什麼,查看iptables的狀態也是處於啟用狀態的:

 [[email protected]]# service iptables status

Redirecting to /bin/systemctl status  iptables.service

iptables.service - IPv4 firewall withiptables

  Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

   Active: active (exited) since 日 2015-01-1818:17:07 CST; 20s ago

 Process: 14440 ExecStop=/usr/libexec/iptables/iptables.init stop(code=exited, status=0/SUCCESS)

 Process: 14648 ExecStart=/usr/libexec/iptables/iptables.init start(code=exited, status=0/SUCCESS)

 MainPID: 14648 (code=exited, status=0/SUCCESS)

 

1月 18 18:17:07 bogoniptables.init[14648]: iptables: Applying firewall rules: [  確定  ]

1月 18 18:17:07 bogonsystemd[1]: Started IPv4 firewall with iptables.

反正永久關閉iptables防火牆是可以的,但是記得要重啟。

 

7. 設定mysql開機自啟動

設定開機啟動服務選擇使用chkconfig命令,可以看到我們永久性關閉iptables就用的這個命令,命令的格式如下:

chkconfig 功能說明:檢查,設定系統的各種服務。

文法:chkconfig [--add][--del][--list][系統服務]或 chkconfig [--level <等級代號>][系統服務][on/off/reset]

--add 添加服務

--del 刪除服務

--list 查看各服務啟動狀態

 

我這裡安裝好了mysql之後預設就是開機自啟動的:

[[email protected] 案頭]# chkconfig --list mysql

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置資料可能被原生 systemd 配置覆蓋。

     如果您想列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。

     欲查看對特定 target 啟用的服務請執行

     ‘systemctl list-dependencies [target]‘。

mysql             0:關 1:關 2:開 3:開 4:開 5:開 6:關

如果不是開機自啟動,使用開啟MySQL服務自動開啟命令:

chkconfig mysqld on

chkconfig mysql on

 

CentOS-7下安裝MySQL5.6.22

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.