http://jingyan.baidu.com/article/ad310e80cf9ce81849f49ea2.html
有些情况,为节省机器资源,多个人共用一台Linux物理机器,但是数据库不许共用,这样就需要各自安装MySQL数据库软件了,但是RPM版需要root权限,那么可以选择免安装的MySQL;Windows也有免安装版的MySQL,不在此介绍。下面进行Linux环境免安装MySQL配置步骤介绍,本文以CentOS Linux为例,所安装MySQL版本为5.1.72,高手请飘过。
方法/步骤
-
下载MySQL软件包到指定目录
/home/codebrother/mysql/mysql-5.1.72-linux-x86_64-glibc23.tar
-
创建MySQL安装主目录
我的机器以mysql_db命名为mysql的主目录,创建mysql_db目录,并进入mysql_db目录,如图一
mkdir mysql_db
cd mysql_db
-
解压缩MySQL软件包,如图二
tar -xvf /home/codebrother/mysql/mysql-5.1.72-linux-x86_64-glibc23.tar
-
修改解压后的目录名称,如图三
执行命令格式:mv 源目录名称 新目录名称
mv mysql-5.1.72-linux-x86_64-glibc23 mysql-5.1.72
-
复制my-medium.cnf文件到指定目录,并命名为my.cnf,如图四、图五
进入mysql-5.1.72目录,执行命令:
cd mysql-5.1.72/support-files/
cp my-medium.cnf ../my.cnf
再返回mysql_db目录,看到my.cnf文件已经在此目录下
-
配置my.cnf文件
编辑my.cnf文件,默认内容如图六
vim my.cnf
-
检查MySQL默认端口3306是否被占用,如图七
执行命令:netstat -apn | grep 3306
-
3306已经被占用,那么换一个端口,建议端口号在10000以上,比如改成13306,如图八
在[client]里修改port端口号
port=13306
在[client]里修改sock文件位置:
socket = /home/codebrother/mysql_db/mysql-5.1.72/mysql.sock
-
在[mysqld]里修改port=13306
port = 13306
在[mysqld]里修改sock文件位置:
socket = /home/codebrother/mysql_db/mysql-5.1.72/mysql.sock
在[mysqld]里添加MySQL基础目录:
basedir = /home/codebrother/mysql_db/mysql-5.1.72
在[mysqld]里添加MySQL数据存储目录:
datadir = /home/codebrother/mysql_db/mysql-5.1.72/data
在[mysqld]里添加MySQL日志文件:
log-error = /home/codebrother/mysql_db/mysql-5.1.72/logs/mysql_error.log
在[mysqld]里添加MySQL进程文件:
pid-file = /home/codebrother/mysql_db/mysql-5.1.72/mysql.pid
在[mysqld]里添加指定MySQL数据库所属用户:
user = codebrother (这里填写你所用的linux用户)
-
按ESC键,输入:wq 即保存退出。
-
创建logs目录
在mysql_db目录下创建logs目录,如图十
执行命令:mkdir logs
-
安装MySQL
执行安装命令:
./script/mysql_install_db --defaults-file=my.cnf
-
如果执行成功,会提示如下内容:
[codebrother@codebrother mysql-5.1.72]$ ./scripts/mysql_install_db --defaults-file=my.cnf
WARNING: The host 'codebrother' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
150904 17:27:00 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK
Filling help tables...
150904 17:27:01 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/home/codebrother/mysql_db/mysql-5.1.72/bin/mysqladmin -u root password 'new-password'
/home/codebrother/mysql_db/mysql-5.1.72/bin/mysqladmin -u root -h codebrother password 'new-password'
Alternatively you can run:
/home/codebrother/mysql_db/mysql-5.1.72/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /home/codebrother/mysql_db/mysql-5.1.72 ; /home/codebrother/mysql_db/mysql-5.1.72/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /home/codebrother/mysql_db/mysql-5.1.72/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /home/codebrother/mysql_db/mysql-5.1.72/scripts/mysqlbug script!
-
启动MySQL,如图十一
执行命令:
bin/mysqld_safe --defaults-file=my.cnf &
-
检查MySQL端口、sock文件、log日志文件
检查端口,执行命令:
netstat -an | apn 13306
如图,代表mysql已经使用13306端口,正常运行,如图十二
-
查看sock文件是否存在,如图十三,即为正常状态
-
查看logs日志文件,内容如下,如果有错误,会提示Error,请仔细检查一下。
[codebrother@codebrother mysql-5.1.72]$ cat logs/mysql_error.log
150904 17:46:35 mysqld_safe Starting mysqld daemon with databases from /home/codebrother/mysql_db/mysql-5.1.72/data
150904 17:46:35 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
150904 17:46:35 [Note] Plugin 'FEDERATED' is disabled.
150904 17:46:35 InnoDB: Initializing buffer pool, size = 8.0M
150904 17:46:35 InnoDB: Completed initialization of buffer pool
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
150904 17:46:35 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
150904 17:46:35 InnoDB: Started; log sequence number 0 44233
150904 17:46:35 [Note] Recovering after a crash using mysql-bin
150904 17:46:35 [Note] Starting crash recovery...
150904 17:46:35 [Note] Crash recovery finished.
150904 17:46:35 [Note] Event Scheduler: Loaded 0 events
150904 17:46:35 [Note] /home/codebrother/mysql_db/mysql-5.1.72/bin/mysqld: ready for connections.
Version: '5.1.72-log' socket: '/home/codebrother/mysql_db/mysql-5.1.72/mysql.sock' port: 13306 MySQL Community Server (GPL)
-
访问MySQL数据库,如图十四
执行命令格式:mysql -u用户名 -h127.0.0.1 -P端口号
mysql -uroot -h127.0.0.1 -P13306
-
查看已有数据库,删除内置的test数据库,如图十五
执行命令:
showdatabases;
drop database test;
-
查看内置MySQL用户,删除匿名用户,如图十六
切换到mysql数据库,执行命令:
use mysql;
select host, user, password from user;
-
执行删除命令,并确认已经删除匿名用户,如图十七
delete from user where user = '';
-
修改用户密码,如图十八
use mysql;
update user set password = password('root');
-
至此免安装MySQL配置已经完毕!
END
注意事项
-
免安装版MySQL不会自启动,包括service mysql start|stop|restart 是不识别的,需要另配置。
-
Linux配置自启动操作会在后续文章中编写
-
如有不足之处,欢迎大家随时批评指正,共同学习,共同进步!