日期:2014-05-16  浏览次数:20710 次

在Linux上部署Java开发环境笔记(五)Linux中安装MySQL

一、下载MySQL
下载文件为“mysql-5.0.22.tar.gz”
?
二、解压缩
将下载的文件放在/opt目录下,执行shell解压缩
cd /opt
tar -zvxf mysql-5.0.22.tar.gz
解压完毕后,会生成/opt/mysql-5.0.22目录
?
三、安装
此安装文件需用源码安装的方式进行
?
1.创建用户和用户组
groupadd mysql
useradd -g mysql mysql
在/opt目录下,新建mysql/data目录
mkdir -p /opt/mysql/data
?
2.配置configure
?
cd /opt/mysql-5.0.22
./configure --prefix=/opt/mysql --with-charset=gb2312 --with-raid --localstatedir=/opt/mysql/data
?
执行后,出现下面信息:
MySQL has a Web site at http://www.mysql.com/ which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.
Remember to check the platform specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.
Thank you for choosing MySQL!
?
3.生成安装文件并安装
然后按照下面步骤输入shell命令:
make???????????????? -- 生成安装文件 (此过程有点耗时,耐心等待!)
make install????? -- 安装
?
cp support-files/my-medium.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld??????????? -- copy启动的mysqld文件
chmod 700 /etc/init.d/mysqld
./scripts/mysql_install_db --user=mysql????????????????????????? -- 生成mysql用户数据库和表文件
./scripts/mysqld_safe --user=mysql &????????????????????????????? -- (用mysql用户启动很重要。)
?

cd /opt/mysql
chown -R root .
chown -R root data
chgrp -R mysql .
./bin/mysqladmin -u root password '123456'????????????????? -- (123456为管理员root用户密码,默认为空)?
?
4.查看3306端口是否打开:
netstat -na|grep 3306
?
5.使用mysql
./bin/mysql -uroot -p123456??????????????????????????????????????????? -- (输入密码,默认密码为空,直接回车即可)
?
Welcome to the MySQL monitor.? Commands end with ; or /g.
Your MySQL connection id is 4 to server version: 5.0.22-log
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql>
?
?
看到“mysql>”出现后,就可以使用mysql了
mysql> use mysql
Database changed
mysql> select * from user
...? (省略n行数据表查询结果)
mysql> quit
Bye
?
?
?
四、配置mysql服务
授权mysql远程连接:
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
此命令可以使远程的电脑访问mysql数据库,%也可以换成某个具体的IP
?
关闭mysql数据库服务:
cd /opt/mysql/
./bin/mysqladmin -u root shutdown
?
重新启动mysql:
/etc/init.d/mysqld restart
把mysql加入到系统服务中(就不用像上面那样启动mysql服务了,开机自动启动服务):
cp? /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld

把mysql加入到环境变量里面:
cp /opt/mysql/bin/mysql?? /usr/bin/mysql??? -- 这样就可以直接使用mysql命令了
?
?
若创建数据库和数据表时,显示“./mysql/不能创建”之类的信息,请将mysql整个目录的访问权限全部打开即可:
chmod -R 777 /opt/mysql???????????????????????????????? --??????? -R表示递归之下的所有目录

至此,MySQL已经安装完毕