<p>一、文章说明</p><p> 最近接触到mysql数据库,因此闲来研究一下,今天演示一下用源码安装mysql,如果使用rpm包安装的话个人感觉挺简单的。</p><p> 环境说明:</p><p> 操作系统:rhel 5.4 x86</p><p> Mysql版本:mysql-5.5.22</p><p>二、安装cmake(mysql5.5以后是通过cmake来编译的)</p><p> 在mysql5.5之前的话直接编辑就可以,而在5.5以后需要通过cmake来编译,在附录中介绍一下直接编译。</p><p> 下载cmake-2.8.4.tar.gz,本人是在度娘搜的。</p><pre class="brush:bash;toolbar:false">[root@node4~]#tar-zxvfcmake-2.8.4.tar.gz
[root@node4~]#cdcmake-2.8.4
[root@node4cmake-2.8.4]#./configure--prefix=/usr/local/cmake</pre><p>----在这里可以使用# gmake && make install</p><pre class="brush:bash;toolbar:false">[root@node4cmake-2.8.4]#make
[root@node4cmake-2.8.4]#makeinstall</pre><p> 备注:configure需要执行编译到目录。</p><p>三、创建mysql的安装目录及数据库存放目录</p><pre class="brush:bash;toolbar:false">[root@node4~]#mkdir-p/opt/mysql5.5
[root@node4~]#mkdir-p/opt/mysql5.5/data</pre><p>四、创建mysql用户及用户组</p><pre class="brush:bash;toolbar:false">[root@node4~]#groupaddmysql
[root@node4~]#useradd-r-gmysqlmysql</pre><p>五、安装mysql</p><pre class="brush:bash;toolbar:false">
[root@node4~]#tar-zxvfmysql-5.5.22.tar.gz
[root@node4~]#cdmysql-5.5.22</pre><p>----编译出现以下错误,需要ncurses-devel</p><pre class="brush:bash;toolbar:false">[root@node4mysql-5.5.22]#/usr/local/cmake/bin/cmake-DCMAKE_INSTALL_PREFIX=/opt/mysql5.5-DMYSQL_DATADIR=/opt/mysql5.5/data-DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci-DEXTRA_CHARSETS=all-DENABLED_LOCAL_INFILE=1
--MySQL5.5.22
--CouldNOTfindCurses(missing:CURSES_LIBRARYCURSES_INCLUDE_PATH)
CMakeErroratcmake/readline.cmake:83(MESSAGE):
Curseslibrarynotfound.Pleaseinstallappropriatepackage,
removeCMakeCache.txtandreruncmake.OnDebian/Ubuntu,packagenameislibncurses5-dev,onRedhatandderivatesitisncurses-devel.
CallStack(mostrecentcallfirst):
cmake/readline.cmake:127(FIND_CURSES)
cmake/readline.cmake:217(MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:268(MYSQL_CHECK_READLINE)
--Configuringincomplete,errorsoccurred!</pre><p>----查看ncurses是否安装:</p><pre class="brush:bash;toolbar:false">[root@node4mysql-5.5.22]#rpm-qa|grepnecurses
ncurses-5.5-24.20060715
----安装ncurses-devel
[root@node4]#rpm-ivhncurses-devel-5.5-24.20060715.i386.rpm
warning:ncurses-devel-5.5-24.20060715.i386.rpm:HeaderV3DSAsignature:NOKEY,keyID37017186
Preparing...###########################################[100%]
1:ncurses-devel###########################################[100%]
----删除CMakeCache.txt
[root@node4mysql-5.5.22]#find/-nameCMakeCache.txt
/root/mysql-5.5.22/CMakeCache.txt
/root/cmake-2.8.4/CMakeCache.txt
/root/cmake-2.8.4/Tests/ComplexOneConfig/Cache/CMakeCache.txt
/root/cmake-2.8.4/Tests/Complex/Cache/CMakeCache.txt
/root/cmake-2.8.4/Tests/CMakeFiles/CheckFortran/CMakeCache.txt
/root/cmake-2.8.4/Tests/ComplexRelativePaths/Cache/CMakeCache.txt
[root@node4mysql-5.5.22]#rm/root/mysql-5.5.22/CMakeCache.txt
rm:removeregularfile`/root/mysql-5.5.22/CMakeCache.txt'?yes
[root@node4mysql-5.5.22]#rm/root/cmake-2.8.4/CMakeCache.txt
rm:removeregularfile`/root/cmake-2.8.4/CMakeCache.txt'?yes
[root@node4mysql-5.5.22]#rm/root/cmake-2.8.4/Tests/ComplexOneConfig/Cache/CMakeCache.txt
rm:removeregularfile`/root/cmake-2.8.4/Tests/ComplexOneConfig/Cache/CMakeCache.txt'?yes
[root@node4mysql-5.5.22]#rm/root/cmake-2.8.4/Tests/Complex/Cache/CMakeCache.txt
rm:removeregularfile`/root/cmake-2.8.4/Tests/Complex/Cache/CMakeCache.txt'?yes
[root@node4mysql-5.5.22]#rm/root/cmake-2.8.4/Tests/CMakeFiles/CheckFortran/CMakeCache.txt
rm:removeregularfile`/root/cmake-2.8.4/Tests/CMakeFiles/CheckFortran/CMakeCache.txt'?yes
[root@node4mysql-5.5.22]#rm/root/cmake-2.8.4/Tests/ComplexRelativePaths/Cache/CMakeCache.txt
rm:removeregularfile`/root/cmake-2.8.4/Tests/ComplexRelativePaths/Cache/CMakeCache.txt'?yes
----再次进行编译:
[root@node4mysql-5.5.22]#/usr/local/cmake/bin/cmake-DCMAKE_INSTALL_PREFIX=/opt/mysql5.5-DMYSQL_DATADIR=/opt/mysql5.5/data-DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci-DEXTRA_CHARSETS=all-DENABLED_LOCAL_INFILE=1
[root@node4mysql-5.5.22]#make&&makeinstall
备注:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql//安装目录
-DINSTALL_DATADIR=/usr/local/mysql/data//数据库存放目录
-DDEFAULT_CHARSET=utf8 //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci//校验字符
-DEXTRA_CHARSETS=all //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1 //允许从本地导入数据
注意事项:
重新编译时,需要清除旧的对象文件和缓存信息。
#makeclean
#rm-fCMakeCache.txt
#rm-rf/etc/my.cnf</pre><p>六、设置目录权限</p><pre class="brush:bash;toolbar:false">[root@node4~]#cd/opt/mysql5.5/
[root@node4mysql5.5]#chown-Rroot:mysql.//把当前目录中所有文件的所有者设为root,所属组为mysql
[root@node4mysql5.5]#chown-Rmysql:mysqldata/</pre><p>七、设置mysql配置文件</p><pre class="brush:bash;toolbar:false">
[root@node4mysql5.5]#cpsupport-files/my-medium.cnf/etc/my.cnf//将mysql的启动服务添加到系统服务中
[root@node4mysql5.5]#cpsupport-files/my-medium.cnfmy.cnf
[root@node4mysql5.5]#moremy.cnf
----在mysqld下面添加basedir=/opt/mysql5.5datadir=/opt/mysql5.5/data
[mysqld]
port=3306
socket=/tmp/mysql.sock
skip-external-locking
key_buffer_size=16M
max_allowed_packet=1M
table_open_cache=64
sort_buffer_size=512K
net_buffer_length=8K
read_buffer_size=256K
read_rnd_buffer_size=512K
myisam_sort_buffer_size=8M
basedir=/opt/mysql5.5
datadir=/opt/mysql5.5/data</pre><p>八、创建系统数据库的表</p><p>----在这里指定了配置文件和用户 </p><pre class="brush:bash;toolbar:false">[root@node4mysql5.5]#scripts/mysql_install_db--defaults-file=./my.cnf--user=mysql
InstallingMySQLsystemtables...
OK
Fillinghelptables...
OK
Tostartmysqldatboottimeyouhavetocopy
support-files/mysql.servertotherightplaceforyoursystem
PLEASEREMEMBERTOSETAPASSWORDFORTHEMySQLrootUSER!
Todoso,starttheserver,thenissuethefollowingcommands:
/opt/mysql5.5/bin/mysqladmin-urootpassword'new-password'
/opt/mysql5.5/bin/mysqladmin-uroot-hnode4password'new-password'
Alternativelyyoucanrun:
/opt/mysql5.5/bin/mysql_secure_installation
whichwillalsogiveyoutheoptionofremovingthetest
databasesandanonymoususercreatedbydefault.Thisis
stronglyrecommendedforproductionservers.
Seethemanualformoreinstructions.
YoucanstarttheMySQLdaemonwith:
cd/opt/mysql5.5;/opt/mysql5.5/bin/mysqld_safe&
YoucantesttheMySQLdaemonwithmysql-test-run.pl
cd/opt/mysql5.5/mysql-test;perlmysql-test-run.pl
Pleasereportanyproblemswiththe/opt/mysql5.5/scripts/mysqlbugscript!</pre><p>九、设置环境变量</p><pre class="brush:bash;toolbar:false">[root@node4~]#vi/root/.bash_profile
----在PATH=$PATH:$HOME/bin添加参数为:
PATH=$PATH:$HOME/bin:/opt/mysql5.5/bin:/opt/mysql5.5/lib
[root@node4~]#source/root/.bash_profile</pre><p>十、手动启动mysql</p><pre class="brush:bash;toolbar:false">
[root@node4mysql5.5]#./bin/mysqld_safe--user=mysql&//启动MySql但不能停止
[1]27412
[root@node4mysql5.5]#13072808:34:38mysqld_safeLoggingto'/opt/mysql5.5/data/node4.err'.
13072808:34:38mysqld_safeStartingmysqlddaemonwithdatabasesfrom/opt/mysql5.5/data
----这里MYSQL的root用户还没有配置密码,所以空值。需要输入密码时,直接点回车键即可。
[root@node4mysql5.5]#./bin/mysqladmin-uroot-pshutdown
Enterpassword:
13072808:34:46mysqld_safemysqldfrompidfile/opt/mysql5.5/data/node4.pidended
[1]+Done./bin/mysqld_safe--user=mysql</pre><p>十一、另一个简单的启动mysql的方法(mysql已经被添加到系统服务中)</p><pre class="brush:bash;toolbar:false">[root@node4mysql5.5]#servicemysqlstart
[root@node4mysql5.5]#servicemysqlstop
[root@node4mysql5.5]#servicemysqlrestart</pre><p> 如果上述命令出现:mysql未识别的服务</p><pre class="brush:bash;toolbar:false">[root@node4mysql5.5]#servicemysqlstart
mysql.server:unrecognizedservice</pre><p> 则可能mysql还没添加到系统服务中,下面用另一个方法添加:</p><pre class="brush:bash;toolbar:false">[root@node4mysql5.5]#cpsupport-files/mysql.server/etc/init.d/mysql
[root@node4mysql5.5]#servicemysqlstart
StartingMySQL...[OK]
[root@node4mysql5.5]#servicemysqlstop
ShuttingdownMySQL.[OK]</pre><p> 注意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql。然后再用#service mysql start 来启动mysql即可。</p><p>十二、修改Mysql的root用户密码以及打开远程连接</p><pre class="brush:bash;toolbar:false">[root@node4~]#/opt/mysql5.5/bin/mysql-urootmysql
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis1
Serverversion:5.5.22-logSourcedistribution
Copyright(c)2000,2011,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>usemysql;
Databasechanged
mysql>descuser;</pre><p>----为root添加远程连接的能力。</p><pre class="brush:bash;toolbar:false">mysql>grantallprivilegeson*.*toroot@"%"identifiedby"root";
QueryOK,0rowsaffected(0.03sec)
mysql>updateusersetPassword=password('123456')whereUser='root';
QueryOK,5rowsaffected(0.02sec)
Rowsmatched:5Changed:5Warnings:0
mysql>selectHost,User,PasswordfromuserwhereUser='root';
+-----------+------+-------------------------------------------+
|Host|User|Password|
+-----------+------+-------------------------------------------+
|localhost|root|*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
|node4|root|*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
|127.0.0.1|root|*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
|::1|root|*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
|%|root|*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
+-----------+------+-------------------------------------------+
5rowsinset(0.00sec)
mysql>flushprivileges;
QueryOK,0rowsaffected(0.01sec)
mysql>exit;
Bye</pre><pre class="brush:bash;toolbar:false">[root@node4~]#/opt/mysql5.5/bin/mysql-uroot-p
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis3
Serverversion:5.5.22-logSourcedistribution
Copyright(c)2000,2011,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.</pre><pre class="brush:bash;toolbar:false">mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|test|
+--------------------+
4rowsinset(0.10sec)
mysql>selectversion();
+------------+
|version()|
+------------+
|5.5.22-log|
+------------+
1rowinset(0.02sec)</pre><p>附录:</p><pre class="brush:bash;toolbar:false">1.groupaddmysql
2.useradd-gmysqlmysql
3.tarzxvfmysql-VERSION.tar.gz
4.cdmysql-VERSION
5../configure--prefix=/usr/local/mysql
--without-debug
--enable-thread-safe-client
--enable-assembler
--enable-profiling
--with-mysqld-ldflags=-all-static
--with-client-ldflags=-all-static
--with-charset=latin1
--with-extra-charsets=utf8,gbk
--with-mysqld-user=mysql
--without-embedded-server
--with-server-suffix=bbk
--with-plugins=innobase,partition
6.make
7.makeinstall
8.mkdirdata
9.chownmysql:mysql./data/-R
10.cpsupport-files/my-medium.cnf/etc/my.cnf
11.在my.cnfmysqld标签下中添加:
basedir=/opt/mysql5
datadir=/opt/mysql5/data
12../bin/mysql_install_db--default-file=./my.cnf--user=mysql
13.selectversion();</pre>