本文首发于 烂泥行天下 。 本次实验的mysql、OS相关信息如下: Mysql:5.0.96 OS:centos 32 bit 最近公司要上新的业务,指明数据库使用的是mysql5.0版本。如果是使用rpm包安装的话,那就很简单了。直接使用yum安装即可,命令如下: yum –y install mysql m
本文首发于烂泥行天下。
本次实验的mysql、OS相关信息如下:
Mysql:5.0.96 OS:centos 32 bit
最近公司要上新的业务,指明数据库使用的是mysql5.0版本。如果是使用rpm包安装的话,那就很简单了。直接使用yum安装即可,命令如下:
yum –y install mysql mysql-server
650) this.width=650;” title=”clip_image001″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image001″ src=”https://img.gaodaima.com/d/file/2021/11/10/94e9aad0bcffee8797ab448dfe11cf14.png” height=”95″ />
650) this.width=650;” title=”clip_image002″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image002″ src=”https://img.gaodaima.com/d/file/2021/11/10/695acbe072963de1f2fd2124ac5a9819.png” height=”274″ />
Yum方式安装完毕后,直接启动mysql数据库服务即可。如下图:
650) this.width=650;” title=”clip_image003″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image003″ src=”https://img.gaodaima.com/d/file/2021/11/10/3b057c7f6e3942fa81a9636c8764ffcc.png” height=”203″ />
这样基本上就可以了。
但是这样安装mysql数据库,没有进行定制。比如mysql数据库的数据文件存储位置。rpm形式安装的数据文件默认位置为/var/lib/mysql。这个我们可以通过mysql的配置文件查看所得,如下图:
650) this.width=650;” title=”clip_image004″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image004″ src=”https://img.gaodaima.com/d/file/2021/11/10/1ce1348687bbb942dc429def0e906e58.png” height=”230″ />
实际的情况是,我们新的业务要求mysql数据库的数据文件需要单独进行存放,这个使用rpm形式就不太容易达到要求。所以打算使用源码进行编译安装,而且自己也没有对mysql数据库进行源码安装过。刚好趁这次机会学习如何对mysql数据库进行源码安装。
首先,在相应的网站下载mysql的安装包。为什么说是相应的网站,而不是mysql的官网。因为现在在官网无法下载到5.0版本的,只能下载到5.5以上的版本。我现在是在这个网站下载的:http://download.mysql.cn/src/
下载完毕上传到服务器上,具体如何上传你可以使用winscp、FTP。或者也可以使用lrzsz这个软件。
我现在使用的就是lrzsz这个软件,如果你的服务器没有安装的话,可以通过:
yum –y install lrzsz
进行安装,如下图:
650) this.width=650;” title=”clip_image005″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image005″ src=”https://img.gaodaima.com/d/file/2021/11/10/94e38be75dbe1016627558708ff2cc63.png” height=”157″ />
rz 是上传的命令,sz是下载的命令。
我们现在使用日志命令把mysql的安装包,上传到服务器上,如下图:
650) this.width=650;” title=”clip_image006″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image006″ src=”https://img.gaodaima.com/d/file/2021/11/10/355145294caffa0857d20a6f654e7467.png” height=”513″ />
650) this.width=650;” title=”clip_image007″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image007″ src=”https://img.gaodaima.com/d/file/2021/11/10/3f441f66463e109e6f99a842edbb7d20.png” height=”295″ />
650) this.width=650;” title=”clip_image008″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image008″ src=”https://img.gaodaima.com/d/file/2021/11/10/6547b0424d0512ff71297885f7741dee.png” height=”170″ />
Mysql安装包已经上传完毕,那么我们现在进行安装mysql之前的工作。
安装mysql编译所需要的工具包,如下图:
650) this.width=650;” title=”clip_image009″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image009″ src=”https://img.gaodaima.com/d/file/2021/11/10/e39a47d333b44f661cb19f232772dd96.png” height=”149″ /> 650) this.width=650;” title=”clip_image010″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image010″ src=”https://img.gaodaima.com/d/file/2021/11/10/f8d1017fdcf592f8f740aff760258a72.png” height=”302″ />
650) this.width=650;” title=”clip_image011″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image011″ src=”https://img.gaodaima.com/d/file/2021/11/10/84f2294b70c7b2ceaa5037ea0396ecf2.png” height=”85″ />
yum -y install gcc gcc-c++ ncurses-devel
工具包安装完毕后,我们来新建相关的用户。第一个用户ilanni,用来安装mysql使用。第二个用户是mysql,用来运行mysql的。如下图:
650) this.width=650;” title=”clip_image012″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image012″ src=”https://img.gaodaima.com/d/file/2021/11/10/86a702f60102e21c37a5a553fa2544a3.png” height=”173″ />
把mysql安装包移动到ilanni用户的家目录下,并解压,如下图:
650) this.width=650;” title=”clip_image013″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image013″ src=”https://img.gaodaima.com/d/file/2021/11/10/4bf29b67c62206aaf86ef6c61203548d.png” height=”27″ />
650) this.width=650;” title=”clip_image014″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image014″ src=”https://img.gaodaima.com/d/file/2021/11/10/d5dbbf57377a67f159312dae02a264fb.png” height=”164″ />
以上工作是前提工作,做完后。我们现在进入mysql-5.0.96目录开始安装mysql。
首先编译mysql数据库,如下图:
650) this.width=650;” title=”clip_image015″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image015″ src=”https://img.gaodaima.com/d/file/2021/11/10/15f94673f45b0cf0b6878c49da1a7521.png” height=”100″ />
./configure –prefix=/usr/local/mysql 其中/usr/local/mysql为mysql数据库的安装位置。
650) this.width=650;” title=”clip_image016″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image016″ src=”https://img.gaodaima.com/d/file/2021/11/10/f9d30b1d54b41ee2fd968158aaf670df.png” height=”395″ />
650) this.width=650;” title=”clip_image017″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image017″ src=”https://img.gaodaima.com/d/file/2021/11/10/5585d6d627c2486654de5021586c71a7.png” height=”275″ />
可以看到没有报错,那我们开始make,如下图:
650) this.width=650;” title=”clip_image018″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image018″ src=”https://img.gaodaima.com/d/file/2021/11/10/0f7edd4c45c66fbf8fb35aa6821f1d31.png” height=”505″ />
Make过程比较慢,我这边大约10分钟左右。
650) this.width=650;” title=”clip_image019″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image019″ src=”https://img.gaodaima.com/d/file/2021/11/10/036371dea02d1552e0bf04770bf34e22.png” height=”167″ />
Make终于完毕了,通过上图可以没有报错。
下面开始make install,如下图:
650) this.width=650;” title=”clip_image020″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image020″ src=”https://img.gaodaima.com/d/file/2021/11/10/435ba4dab556a3975dead1e2a12e9564.png” height=”499″ />
650) this.width=650;” title=”clip_image021″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image021″ src=”https://img.gaodaima.com/d/file/2021/11/10/53b9ff6f91eeb304ce239d92e22e5247.png” height=”253″ />
Make install还是比较快的,可以看到没有报错。
Mysql已经安装完毕,下面开始进行相关的配置。
还是在该安装目录下,如下图:
650) this.width=650;” title=”clip_image022″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image022″ src=”https://img.gaodaima.com/d/file/2021/11/10/6294d232f7bfea752834183410e8dcea.png” height=”70″ />
复制support-files目录下的my-medium.cnf到/etc/下作为mysql的配置文件:
cp support-files/my-medium.cnf /etc/my.cnf
650) this.width=650;” title=”clip_image023″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image023″ src=”https://img.gaodaima.com/d/file/2021/11/10/73b1ac3e5b16716c939affdde7edae76.png” height=”470″ />
复制support-files目录下的mysql.server到/etc/init.d/目录下作为mysql的启动文件
650) this.width=650;” title=”clip_image024″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image024″ src=”https://img.gaodaima.com/d/file/2021/11/10/991bb9d8936e1dee6539a3e9d2ce38bc.png” height=”27″ />
现在我们来启动下使用/etc/init.d/mysqld启动mysql 试下,如下图:
650) this.width=650;” title=”clip_image025″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image025″ src=”https://img.gaodaima.com/d/file/2021/11/10/13438e654337c56a46853a7e6c195eb2.png” height=”89″ />
可以看到系统提示我们,没有执行该脚本的权限。而mysqld脚本的权限,看下图:
650) this.width=650;” title=”clip_image026″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image026″ src=”https://img.gaodaima.com/d/file/2021/11/10/4c575298d95b5f76c89cede45a41d567.png” height=”54″ />
可以很明显的看到目前该脚本的权限为744,根本没有执行权限。
现在更改该脚本的权限,如下图:
650) this.width=650;” title=”clip_image027″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image027″ src=”https://img.gaodaima.com/d/file/2021/11/10/c5a62bb6b6b82383ad11655469e570b3.png” height=”110″ />
Ok,该文件的权限修改完毕后,我们来初始化mysql数据库。在文章的开头我就说了,我现在要把mysql的数据文件存放在/data目录下。
首先要创建/data目录,并修改该目录的所属用户及用户组。如下图:
650) this.width=650;” title=”clip_image028″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image028″ src=”https://img.gaodaima.com/d/file/2021/11/10/cb9966cc74656d345e46f866e115be07.png” height=”243″ />
修改完毕后,我们还需要修改/usr/local/mysql的所属用户及用户组,如下图:
650) this.width=650;” title=”clip_image029″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image029″ src=”https://img.gaodaima.com/d/file/2021/11/10/673ab452d413b9edc0ceadb991ab3b73.png” height=”369″ />
以上修改完毕后,我们还需要修改mysql的配置文件。主要是要在配置文件中加入目前自己定义数据文件位置,如下图:
650) this.width=650;” title=”clip_image030″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image030″ src=”https://img.gaodaima.com/d/file/2021/11/10/3f99359b3f59a1e9ef1c25ee74e9dcfe.png” height=”266″ />
以上所有的修改进行完毕后,我们在执行mysql安装目录scripts下mysql_install_db文件,如下图:
650) this.width=650;” title=”clip_image031″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image031″ src=”https://img.来1源gaodai#ma#com搞*代#码1网gaodaima.com/d/file/2021/11/10/8bf0fa12173b0ac4355734882827d8a6.png” height=”277″ />
./scripts/mysql_install_db –user=mysql 中的–user=mysql一定要有。
我们来查看mysql初始化的结果,查看/data目录,如下图:
650) this.width=650;” title=”clip_image032″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image032″ src=”https://img.gaodaima.com/d/file/2021/11/10/de91df54fde25c2efd85fa250039c677.png” height=”165″ />
可以看到mysql的默认数据mysql及test都在里面。
现在我们来启动mysql看看实际的情况,如下图:
650) this.width=650;” title=”clip_image033″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image033″ src=”https://img.gaodaima.com/d/file/2021/11/10/18d83b292262b8f50f6472d80cb9fa3b.png” height=”161″ />
/etc/init.d/mysqld start
如果要把mysql开机启动,可以使用chkconfig命令。如下图:
650) this.width=650;” title=”clip_image034″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image034″ src=”https://img.gaodaima.com/d/file/2021/11/10/cdd3465560db7cce65cbab77809aba5a.png” height=”137″ />
如果你想在如何目录下直接mysql命令,需要把/usr/local/mysql/bin路径加入到全局设置/etc/profile文件中,如下图:
650) this.width=650;” title=”clip_image035″ style=”border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;” border=”0″ alt=”clip_image035″ src=”https://img.gaodaima.com/d/file/2021/11/10/ee43d783f7f422c32d38f4313692abb6.png” height=”282″ />
Ok,到此mysql数据库就安装完毕。
如果mysql客户端连接,mysql服务比较慢的话。我们需要在my.cnf文件中加入skip-name-resolve,如下:
650) this.width=650;” src=”https://img.gaodaima.com/d/file/2021/11/10/243d13819384cbf50ce7d296f6e07a3f.jpg” width=”430″ height=”84″ alt=”wKiom1RXLsnhbFsiAACS1IQBpCE094.jpg” />
PS:有关本次实验使用的命令如下,在执行过程中建议尽量按照命令的顺序进行操作:
yum -y install gcc gcc-c++ ncurses-devel
tar xf mysql-5.0.22.tar.gz
useradd ilanni
useradd -s /sbin/nologin mysql
cd /home/ilanni/mysql-5.0.22
./configure –prefix=/usr/local/mysql
make
make install
cp /home/ilanni/mysql-5.0.96/support-files/my-medium.cnf /etc/my.cnf
cp /home/ilanni/mysql-5.0.96/support-files/mysql.server /etc/init.d/mysqld
chmod -R 755 /etc/init.d/mysqld
chown -R mysql:mysql /usr/local/mysql
mkdir /data
chown -R mysql:mysql /data
/usr/local/mysql/bin/mysql_install_db –basedir=/usr/local/mysql
/etc/init.d/mysqld start
echo PATH=$PATH:/usr/local/mysql/bin/ >>/etc/profile
tail -10 /etc/profile
source /etc/profile