---
title: "linux + mysql5"
author: "ericFang"
date: "2008-10-04T23:57:22Z"
tags: []
slug: "linux-mysql5"
draft: false
lastmod: "2008-10-04T23:57:22Z"
id: 248979
url: "https://ericfang.iteye.com/blog/248979"
---

mysql5.x下载地址: http://dev.mysql.com/downloads/mysql/5.0.html
以上是mysql的官方地址，上面的mysql版本太多，而且速度很慢，建议大家从中文网站http://download.mysql.cn/src/上下载，速度可以而求版本的兼容性很强，不用为每个特定的Linux寻找版本mysql工具下载: http://dev.mysql.com/downloads/gui-tools/5.0.html

Linux安装下mysql5.x
在中文网站上可以下到两种Linux版本：后缀名闻.gz的和.rpm的，前者是mysql的压缩文件，有的是源文件（需要编译），有的是可执行文件（binary）；后者是RedHat Linux制作的安装包，安装比较方便。下面分别介绍两种安装方式，第二种方式比较简单，建议使用。
一.编译安装
这里用的mysql-5.0.22.tar.gz源文件压缩包，文件存放在/home/software/目录下，进入shell，安装步骤如下：
#cd /home/software               //进入安装文件目录
#tar zxvf mysql-5.0.22.tar.gz    //解压文件到本目录下
# cd mysql-5.0.22                  //进入源文件目录
# ./configure --prefix=/usr/local/mysql #--with-charset=GBK           //设定 configure 安装选项，选择安装目录 （prefix）以
                                                                                             及支持中文GBK 码（with-charset=GBK）
# make                                //开始编译
# make install                        //执行安装
# scripts/mysql_install_db        //产生 MySQL grant tables（会建立一个 mysql 资料库和一些 tables，用来管理使用 MySQL 的授
                                           权资讯，也就是使用者有哪些使用资料库的权限）
如果执行上面命令后出现一大堆编译信息，并且最后没有报错，那么恭喜你，安装已经完成了，不过不要得以太早，能不能启动还要看你的人品嘞。
mysql有好几种启动方式，有的可能启动不了，但多试几种，有时就能正常启动了。下面是几种启动命令（/usr/local/mysql/是安装目录）：
# chmod 744 mysql.server                                        //把mysql.server设成可执行得
# /usr/local/mysql/share/mysql/mysql.server start         //有的可能是mysql/share/mysql.server 目录，不同版本不同
                                                                           另外可将这行指令加在 /etc/rc.d/rc.local 档中，让 MySQL 在开机时自动启动。
# /usr/local/mysql/bin/mysqld_safe --user=root&
# /usr/local/mysql/bin/mysqld_safe --user=mysql&
# /usr/local/mysql/bin/safe_mysqld--uer=root&
用命令：
#netstat -nat
查看端口，如果出现一行长的像：
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN   
那么恭喜你，启动成功了。
要停止 MySQL 的方法：　　
# /usr/local/mysql/bin/mysqladmin shutdown
如果你为 MySQL Administrator root 帐号（非作业系统的 root）设了密码，要停止 MySQL 则必须像下列这样做，MySQL 会询问你 root 的密码後才会执行 shutdown 的工作：　　
# /usr/local/mysql/bin/mysqladmin -u root -p shutdown
或者干脆点，直接找到mysql的运行进程，kill了它^_^.
二.下载rpm安装包，体验快捷安装 
下载合适的rpm包到待安装临时目录，比如/home/software/,本文用的是MySQL-server-5.0.22-0.i386.rpm安装包，
安装过程如下：
# cd /home/software
# rpm -ivh MySQL-server-5.0.22-0.i386.rpm
如果提示如下错误:
warning: mysql-3.23.54a-11.i386.rpm: V3 DSA signature: NOKEY, key ID db42a60e
error: Failed dependencies:
perl(CGI) is needed by mysql-3.23.54a-11
perl-DBD-MySQL is needed by mysql-3.23.54a-11
则需要加参数,进行强制安装:
# rpm -ivh MySQL-server-5.0.22-0.i386.rpm --force --nodeps
最后如果出现如下提示：
Preparing...             ########################################### 
1:MySQL-server           ########################################### 
071016  9:27:51  Asked for 196608 thread stack, but got 126976
071016  9:27:51  Asked for 196608 thread stack, but got 126976
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
See the manual for more instructions. 
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
Starting MySQL
那么恭喜你，安装成功，而且mysql会自动启动。
你可以利用上面的几种方法启动和停止mysql，也可以利用
#service mysql start
#service mysql stop
来启动和停止服务

Linux下的mysql卸载
编译安装的Mysql直接删除安装目录就可以了，例如
# cd /usr/local
# rm -rf mysql
rpm安装的mysql也要用rpm卸载，步骤如下：
#rpm -qa |grep -i mysql    //找到系统中的Mysql版本，如果是系统自带的Mysql则是rpm -qa |grep mysql   
MySQL-server-5.0.22-0
如果系统有mysql则会显示上面的内容（Mysql版本），接下来就可以卸载了：
#rpm -ev MySQL-server-5.0.22-0
跟Windows系统差不多，卸载后会留下一些文件，如果不用，可以强制删除：
# rm -f /etc/my.cnf
# rm -rf /var/lib/mysql 

进入Mysql
Linux下的mysql命令和Windows下一样，进入时的命令：
# /usr/local/mysql/bin/mysql -u root -p    // /usr/local/mysql/是其安装目录

修改root密码
修改密码命令：mysqladmin -u用户名 -p旧密码  password 新密码
比如原root密码为空，新密码为"ab12"
# bin/mysqladmin -uroot -password ab12
再次改密码为"cde345"
# # bin/mysqladmin -uroot -pab12 password cde345
添加远程访问权限
添加远程权限：grant all privileges on *.* to username@'%' identified by 'passwd' with grant option;
# bin/mysql -uroot -p  //根据系统提示输入正确password
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 135 to server version: 5.0.22-standard-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
mysql> grant all privileges on *.* to root@'%' identified by 'cde345' with grant option;
mysql> flush privileges