申請阿里雲Linux服務器
昨天在阿里雲申請了一個免費試用5天的Linux雲服務器。html
操做系統:Red Hat Enterprise Linux Server 5.4 64位。java
CPU:1核node
內存:512Mmysql
硬盤空間:20Glinux
帶寬:1Mbps。sql
今天在這臺雲服務器上安裝了基本環境(JDK、Tomcat和MySQL)。本文主要在阿里雲Linux雲服務器安裝MySQL全過程,以及對安裝時遇到的問題進行記錄。數據庫
下載MySQL服務器和客戶端
訪問MySQL的官網,進入其下載頁面(http://dev.mysql.com/downloads/mysql/#downloads),選擇MySQL Community Server,在「Select Platform」下拉框中選擇「Oracle & Red Hat Linux 4 & 5」。centos
由於是64位的操做系統,因此選擇下載以下rpm包:緩存
(1)MySQL-server-5.6.10-1.rhel5.x86_64.rpm:MySQL服務器;服務器
(2)MySQL-client-5.6.10-1.rhel5.x86_64.rpm:MySQL客戶端;
(3)MySQL-devel-5.6.10-1.rhel5.x86_64.rpm:Mysql開發依賴包。
將這些rpm包經過FTP上傳到Linux服務器的某個目錄,例如:/root/software。
也可在Linux服務器上經過wget命令取得這些rpm包,Linux命令以下:

wget –c http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-server-5.6.10-1.rhel5.x86_64.rpm/from/http://cdn.mysql.com

wget –c http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-client-5.6.10-1.rhel5.x86_64.rpm/from/http://cdn.mysql.com

wget –c http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-devel-5.6.10-1.rhel5.x86_64.rpm/from/http://cdn.mysql.co
安裝MySQL服務器和客戶端
安裝MySQL服務器
下載完成後使用以下命令開始安裝MySQL服務器:

rpm -ivh MySQL-server-5.6.10-1.rhel5.x86_64.rpm
安裝依賴包libaio
在運行第一條安裝MySQL服務器時會提示依賴包libaio.so.1沒有安裝,錯誤提示以下:

error: Failed dependencies:

libaio.so.1()(64bit) is needed by MySQL-server-5.6.10-1.rhel5.x86_64

libaio.so.1(LIBAIO_0.1)(64bit) is needed by MySQL-server-5.6.10-1.rhel5.x86_64

libaio.so.1(LIBAIO_0.4)(64bit) is needed by MySQL-server-5.6.10-1.rhel5.x86_64
須要使用yum命令快速安裝依賴包libaio,運行以下命令:

yum install libaio
運行該命令時會發如今這臺雲服務器上沒法經過yum快速安裝軟件,搜索了一些資料,原來是阿里雲安裝的是Red Hat5.4的64位系統,可是提供的是CentOS 32位的系統。錯誤信息參考以下:

Setting up Install Process

No package libaio available.

Nothing to do
網上找到的一個解決方案是將yum替換成CentOS的版本,我嘗試了下是可行的。過程以下:
經過rpm命令查看有哪些yum包,而後進行卸載
筆者在雲服務器進行的操做以下:

[root@AY130221101729bc93912 software]# rpm -qa|grep yum

yum-3.2.22-20.el5

yum-metadata-parser-1.1.2-3.el5

[root@AY130221101729bc93912 software]# rpm -e --nodeps yum-3.2.22-20.el5

warning: /etc/yum.conf saved as /etc/yum.conf.rpmsave

[root@AY130221101729bc93912 software]# rpm -e --nodeps yum-metadata-parser-1.1.2-3.el5
經過wget命令在163的鏡像上下載CentOS的yum包
參考命令以下:

wget http://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-3.2.22-40.el5.centos.noarch.rpm

wget http://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-metadata-parser-1.1.2-4.el5.x86_64.rpm

wget http://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm
說明:可能版本會有一些不一樣,請你們在http://mirrors.163.com/centos/5/os/x86_64/CentOS/下查找yum-3xxx開頭、yum-metadata開頭和yum-fastestmirror開頭的rpm包下載。
安裝yum相關的rpm包
使用以下命令安裝在(2)中下載的rpm包:

rpm -ivh yum-*
下載CentOS-Base.repo
找一個CentOS的包資源配置庫,名爲CentOS-Base.repo,放到/etc/yum.repos.d/路徑:

[root@AY130221101729bc93912 software]# cd /etc/yum.repos.d/

[root@AY130221101729bc93912 yum.repos.d]# wget http://www.linuxidc.com/files/2011/05/06/CentOS-Base.repo

--2013-02-22 08:58:27-- http://www.linuxidc.com/files/2011/05/06/CentOS-Base.repo

Resolving www.linuxidc.com

60.191.129.94

Connecting to www.linuxidc.com|60.191.129.94|:80

connected.

HTTP request sent, awaiting response

200 OK

Length: 1426 (1.4K) [application/octet-stream]

Saving to: `CentOS-Base.repo'

100%[==========================================================================================>] 1,426 --.-K/s in 0s

2013-02-22 08:58:28 (124 MB/s) - `CentOS-Base.repo' saved [1426/1426]
生成緩存文件到/var/cache/yum目錄

[root@AY130221101729bc93912 yum.repos.d]# yum makecache

Loaded plugins: fastestmirror

Determining fastest mirrors

addons | 1.9 kB 00:00

addons/filelists_db | 568 B 00:00

addons/other_db

base

…… 3641/3641

base 3641/3641

Metadata Cache Created<p align="justify"></p>
安裝依賴包libaio
第(5)步作完後,此時能夠成功運行yum install libaio命令,參考執行狀況以下:

[root@AY130221101729bc93912 yum.repos.d]# yum install libaio

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

Setting up Install Process

Resolving Dependencies

……

Complete
安裝MySQL Server的rpm包
依賴包安裝完成後,能夠執行rpm -ivh MySQL-server-5.6.10-1.rhel5.x86_64.rpm包安裝MySQL的服務器端。參考執行狀況以下(省略了部份內容):

[root@AY130221101729bc93912 software]# rpm -ivh MySQL-server-5.6.10-1.rhel5.x86_64.rpm

Preparing

########################################### [100%]

1:MySQL-server ########################################### [100%]

2013-02-22 09:03:18 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

…….

A random root password has been set. You will find it in '/root/.mysql_secret'.

2013-02-22 09:03:25 4132 [Note] Binlog end

2013-02-22 09:03:25 4132 [Note] InnoDB: FTS optimize thread exiting.

……
安裝完成後會生成root用戶的隨機密碼,請使用「cat /root/.mysql_secret」或相似命令進行查看。
安裝MySQL客戶端
服務端安裝完成後,使用以下命令安裝MySQL客戶端,命令以下:

rpm -ivh MySQL-client-5.5.28-1.rhel5.i386.rpm
安裝MySQL開發依賴包
接着安裝MySQL-devel-5.5.28-1.rhel5.i386.rpm,命令以下:

rpm -ivh MySQL-devel-5.5.28-1.rhel5.i386.rpm
MySQL的幾個重要目錄
(1)數據庫目錄
/var/lib/mysql/
(2)配置文件
/usr/share/mysql(mysql.server命令及配置文件)
(3)相關命令
/usr/bin(mysqladmin mysqldump等命令)
(4)啓動腳本
/etc/rc.d/init.d/(啓動腳本文件mysql的目錄)
若想查看MySQL安裝到哪一個目錄,可以使用「whereis mysql」命令查看,參考執行結果以下所示:

[root@AY130221101729bc93912 mysql]# whereis mysql

mysql: /usr/bin/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
啓動MySQL
安裝完成後mysql是沒有啓動的,運行mysql命令會提示以下錯誤:

[root@AY130221101729bc93912 software]# mysql

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
可以使用以下命令啓動MySQL:

service mysql start
或者使用以下命令:

/etc/init.d/mysql start
登陸MySQL
使用mysql命令登陸MySQL服務器,運行MySQL的命令時,會提示須要設置root用戶密碼,提示信息以下:

[root@AY130221101729bc93912 mysql]# mysql -u root -p

Enter password:

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

Your MySQL connection id is 5

Server version: 5.6.10

Copyright (c) 2000, 2013, 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> show databases;

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
可以使用SET PASSWORD命令修改root用戶的密碼,參考以下:

mysql> SET PASSWORD = PASSWORD('root123456');

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+
設置開機自啓動
設置開機自啓動的方法有不少,例如使用chkconfig命令,另外也能夠在/etc/rc.local文件中加上以下MySQL的啓動命令,例如:

/etc/init.d/mysql start
設置完成後重啓系統,使用natstat –nat命令可看到MySQL的3306端口:

netstat -nat

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

……
參考文檔
(1)《在Linux下安裝和使用MySQL》:http://www.yesky.com/187/1754687.shtml
(2)《CentOS下以RPM方式安裝MySQL5.5》:http://blog.seweal.com/post/centos-mysql-install-rpm
(3)《Redhat Linux沒法使用yum快速安裝軟件解決方案》:
http://www.2cto.com/os/201112/113105.html
(4)CentOS的一些rpm包的鏡像下載路徑:http://mirrors.163.com/centos/5/os/x86_64/CentOS/
(5)MySQL的Server和Client端的下載路徑:http://dev.mysql.com/downloads/mysql/#downloads