我linux是redhat enterparise 6.0 ,32位的.
linux安装mysql有两种方式,一种是tar包的形式,这种方式是通过源码自行变异安装,适合高级用户定制MySQL的特性。是通过编译过的二进制文件进行安装。一种是rpm包的形式,安装进程会自动完成系统的相关配置。tar包的形式就有点复杂,比rpm的复杂。个人建议是用rpm包进行安装,方便。
下载安装包:
1.官网地址:http://www.mysql.com/downloads/
将显示:
进行选择linux的相应的版本下载即可。
a.如果你下的是tar包的话,按如下方式安装(个人建议用rpm的方式):
下载tar包,检查你的linux上是否安装了mysql相关的lib,如有将其卸载。
#rpm -qa|grep -i mysql
卸载:
#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps,
之后是否卸载成功,输入rpm -qa|grep -i mysql 进行相应的查看。
b.添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组。
#groupadd mysql
#useradd -r -g mysql mysql
*useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
c. 将二进制文件解压到指定的安装目录,我们这里指定为/usr/local
# cd/usr/local/
#tar -zxvf mysql-********.tar.gz,解压你的mysql包。
d.进入mysql文件夹,也就是mysql所在的目录,并更改所属的组和用户。
#cd mysql
#chown -R mysql .
#chgrp -R mysql .
e. 执行mysql_install_db脚本,对mysql中的 data目录进行初始化并创建一些系统表格。注意mysql服务进程mysqld运行时会访问data目录,所以必须由启动mysqld进程的用户(就是 我们之前设置的mysql用户)执行这个脚本,或者用root执行,但是加上参数--user=mysql。
[root@localhost mysql]scripts/mysql_install_db --user=mysql
*如果mysql的安装目录(解压目录)不是/usr/local/mysql,那么还必须指定目录参数,如
[root@localhost mysql]scripts/mysql_install_db --user=mysql \
--basedir=/opt/mysql/mysql \
--datadir=/opt/mysql/mysql/data
*将mysql/目录下除了data/目录的所有文件,改回root用户所有,mysql用户只需作为mysql/data/目录下所有文件的所有者。
[root@localhost mysql]chown -R root .
[root@localhost mysql]chown -R mysql data
f. 复制配置文件
[root@localhost mysql] cp support-files/my-medium.cnf /etc/my.cnf
g. 将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
h. 重启系统,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!
i. 运行客户端程序mysql,在mysql/bin目录中,测试能否连接到mysqld。
[root@localhost mysql]#/usr/local/mysql/bin/mysql
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最后加入两行命令:
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
这样就可以在shell中直接输入mysql命令来启动客户端程序了
[root@localhost mysql]#mysql
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>
2.使用rpm安装
下载相应的rpm包,如下所示:
当然,这里还是要检查你的系统中是否有mysql的相应的lib包。有的话将其卸载掉。
#rpm -qa|grep -i mysql
卸载:
#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps,
之后是否卸载成功,输入rpm -qa|grep -i mysql 进行相应的查看。
将你的rpm包放在/usr/local/下面。进入/usr/local/,执行
#rpm -ivh mysql-server.*******.rpm,进行安装。
安装成功之后,需要链接的话要装mysql-client,和上面的步骤是一样的。
#rpm -ivh mysql-client.*******rpm,进行安装。
之后启动sql的服务:
#service mysql start.
Starting MySQL.. SUCCESS!
可通过检查端口是否开启来查看MySQL是否正常启动:
#netstat -anp|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 34693/mysqld
如果安装成功应该可以运行mysql命令,注意必须是mysqld服务以及开启:
#mysql
Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 1
Server version: 5.5.29MySQL Community Server (GPL)
Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademarkof Oracle Corporation and/or its affiliates. Other names may be trademarks oftheir respective owners.
Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.
mysql>