前言
这篇博文是以前写的,有些地方不是很专业,大家凑合看吧,当然按照步骤成功安装MYSQL是不成问题的。
安装环境说明:
系统:ubuntu 11.10
MySql:mysql 5.5.17
1,在Root下尝试
主要由于我在普通用户下的权限问题搞的有点头大,所以切了过来。
1.1 MYSQL安装
在这里先列一下安装说明里面的解答:
To install and use a MySQL binary distribution, the basic command
sequence looks like this:
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/msql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
在普通用户下尝试多次后,还是遇到数据库无法启动的问题,所以,我切到root用户下安装,
提示:ubuntu默认root不设密码,所以你无法登录,在当前用户下,终端输入:sudo passwd root 输入当前用户登录密码,再设置root用户密码就可以了
附命令记录:
cd /etc
rm my.cnf
rm -rf mysql/
cd /
find -name "mysql" -print
cd /etc/apparmor.d/abstractions/
rm -rf mysql
以上命令是因为已经安装过mysql,会遗留一些垃圾文件,搜寻一下,全部删除。
groupadd mysql
useradd -r -g mysql mysql
cd /usr/local
cd src
ln -s /usr/local/src/mysql-5.5.17-linux2.6-i686/ mysql
cd mysql
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chgrp -R mysql .
chown -R root .
chown -R mysql data
ls -l
bin/mysqld_safe --user=mysql &
前面可能因为没有chown -R mysql data产生了一系列问题,不知道是不是根本原因所在,先记录一下。执行到这地方,命令行会返回一个进程号,终端提示mysql进程启动,一定没有报错才行。这证明mysql启动了。
1.2 测试服务
新开一个终端,进行如下操作:
/usr/local/mysql/bin/mysqladmin version
正常是返回如下:
root@air:~# /usr/local/mysql/bin/mysqladmin version
/usr/local/mysql/bin/mysqladmin Ver 8.42 Distrib 5.5.17, for linux2.6 on i686
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.5.17
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 1 min 5 sec
Threads: 1 Questions: 1 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.015
1.3配置参数
接下来,我们把默认的配置文件复制到/etc文件夹下,命名为my.cnf文件,命令如下:
cd /usr/local/mysql
cp support-files/my-medium.cnf /etc/my.cnf
现在我们做好了启动服务的准备工作,可以启动了:
bin/mysql -u root -p
如果正常,返回如下:
root@air:/usr/local/mysql# bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# 输\s看下状态,\q退出。
1.4安全配置
现在我们服务起来了,但是密码等都没有设置,接下来我们可以运行如下命令:
root@air:/usr/local/mysql# bin/mysql_secure_installation
返回如下:我列举出来,其实可以按着INSTALL-BINARY说明文件中的两条语句手工设置密码,删除TEST数据库,和匿名用户等操作,(从安全性讲,一定要做如上动作)但是推荐还是用上面的语句一并设置。
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone ca
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
ERROR 1010 (HY000) at line 1: Error dropping database (can't rmdir './test/', errno: 17)
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
2.环境变量以及自启动
2.1 配置环境变量
但是我们发现每次运行命令基本上都要输入绝对路径,或者在安装目录下运行bin/###.##等,如果用apt-get安装的话是不会遇到这个问题的,主要是安装程序已经帮你做了。这里面我们设置环境变量来解决这个问题,类似Windows下配置JDK环境,需要设置JAVA命令PATH一样,在Ubuntu下,环境变量可以设置成用户级或者系统级,区别是对单个用户还是对全局用户都生效,是对BASH生效还是其他等等,这个我也刚接触,列举下面的操作流程和方法,我采用系统级变量:
系统环境变量一般保存在下面的文件中:
/etc/environment
/etc/profile
/etc/bash.bashrc
/etc/profile和 /etc/bash.bashrc在Ubuntu 10.0版本中不推荐使用。
如想将一个路径加入到$PATH中,可以像下面这样做(修改/etc/profile):
$ sudo nano /etc/profile
添加如下语句:
export PATH="$PATH:/my_new_path"
你可以自己加上指定的多个路径,中间用冒号隔开。环境变量更改后,在用户下次登陆时生效,如果想立刻生效,则可执行下面的语句:
$source /etc/profile
需要注意的是,最好不要把当前路径”./”放到PATH里,这样可能会受到意想不到的攻击。
其他文件的修改方式与此类似,需要注意的是/etc/environment不需要使用export设置环境变量,其他profile文件需要。
这样所有用户下的终端都认识/usr/local/mysql/bin下的命令了。
2.2 配置自启动
可以如下启动:
/usr/local/mysql/bin/mysqld //启动Mysql服务
/usr/local/mysql/bin/mysql -u root //访问Mysql服务器
/usr/local/mysql/bin/mysqladmin -u root -p shutdown //关闭mysql服务器
最好应该使其运行在后台:
/usr/local/mysql/bin/mysqld &
让服务开机自动运行
cp /usr/local/mysql/support-files/my.server /etc/init.d/mysqld
sudo update-rc.d mysqld default
把mysqld加入到服务中,重新启动机器即可
当然以后启动可以直接输入
/etc/init.d/mysqld restart|start
到此,MYSQL的安装工作基本已经做完了。
几个常见错误:
常见错误1:第一次在普通用户权限下安装,可能因为权限的问题,并丢失了一个操作步骤,所以导致安装成功,但是每次启动总报错:
air@air:/usr/local/mysql/bin$ sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 6378
air@air:/usr/local/mysql/bin$ 111116 11:58:56 mysqld_safe Logging to '/var/log/mysql/error.log'.
touch: 无法创建"/var/log/mysql/error.log": 没有那个文件或目录
chown: 无法访问"/var/log/mysql/error.log": 没有那个文件或目录
111116 11:58:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
/usr/local/mysql/bin/mysqld_safe: 107: /usr/local/mysql/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Directory nonexistent
/usr/local/mysql/bin/mysqld_safe: 1: eval: cannot create /var/log/mysql/error.log: Directory nonexistent
111116 11:58:57 mysqld_safe mysqld from pid file /var/lib/mysql/air.pid ended
从上面看,应该是设置 MYSQL安装文件夹下data用户以及用户组的问题,因为这个是我唯一没有做的操作,可能导致权限一系列问题。
至于所谓.pid文件,并不需要创建,这个和.sock文件是动态自动创建的。怀疑是由于用了mysql用户,但是属性变成了当前用户air,导致这个问题产生。
希望有大牛能够指点一下,不是很明白。
常见错误2:
air@air:/usr/local/mysql$ scripts/mysql_install_db --user=mysql
FATAL ERROR: Could not find mysqld
The following directories were searched:
/usr/libexec
/usr/sbin
/usr/bin
If you compiled from source, you need to run 'make install' to copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top level of the extracted archive, or pass the --basedir option pointing to that locatio
解决方案:这个问题说的比较明显,对于二进制安装来说,一定要在/usr/local文件夹下安装,解压的时候可以在别的文件夹,(最好在/usr/local/src下,便于管理),一定要在/usr/local下建立链接指向你的源二进制包
命令:ln -s /usr/local/src/mysql-VERSION mysql
解压:sudo tar zxvf /path/to/your/source.tar.gz
常见问题3:
air@air:/usr/local/mysql$ sudo scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql
Installing MySQL system tables...
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
Installation of system tables failed! Examine the logs in
/var/lib/mysql for more information.
You can try to start the mysqld daemon with:
shell> /usr/local/mysql/bin/mysqld --skip-grant & and use the command line tool /usr/local/mysql/bin/mysql to connect to the mysql database and look at the grant tables:
shell> /usr/local/mysql/bin/mysql -u root mysql
mysql> show tables
Try 'mysqld --help' if you have problems with paths. Using --log gives you a log in /var/lib/mysql that may be helpful.
Please consult the MySQL manual section 'Problems running mysql_install_db', and the manual section that describes problems on your OS. Another information source are the MySQL email archives available at http://lists.mysql.com/.
Please check all of the above before mailing us! And remember, if you do mail us, you MUST use the /usr/local/mysql/scripts/mysqlbug script!
一般同学解决上面的安装文件夹后,往往还会遇到如上问题,如果你安装完ubuntu后,没有更新库或者安装运行支持库的话。
主要是少了libaio.so.1这个文件,网上搜一下,或者新立得或者软件中心搜一下,
知道要安装如下包:
libaio1 0.3.109-2ubuntu1,
安装以下不再报错。
任何问题,欢迎斧正