当前的数据库种类比较多,有MS-SQL,Oracle,MYSQL,POSTGRESQL等,而MYSQL在LAMP架构中的份量是举足轻重的,下面就来介绍下Centos5下MySQL的安装和基本配置。
使用yum安装MySQL(在LAMP架构中,开始安装mysql之前应该先安装apache和php)
[root@centos5 /]# yum -y install mysql-server
Loading "fastestmirror" plugin
Loading mirror speeds from cached hostfile
* base: centos.candishosting.com.cn
* updates: mirror.khlug.org
* addons: centos.candishosting.com.cn
* extras: centos.candishosting.com.cn
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package mysql-server.i386 0:5.0.45-7.el5 set to be updated
--> Processing Dependency: libmysqlclient.so.15(libmysqlclient_15) for package: mysql-server
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server
--> Processing Dependency: libmysqlclient_r.so.15(libmysqlclient_15) for package: mysql-server
--> Processing Dependency: libmysqlclient_r.so.15 for package: mysql-server
--> Processing Dependency: mysql = 5.0.45-7.el5 for package: mysql-server
--> Processing Dependency: libmysqlclient.so.15 for package: mysql-server
--> Running transaction check
---> Package perl-DBD-MySQL.i386 0:3.0007-1.fc6 set to be updated
---> Package mysql.i386 0:5.0.45-7.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
mysql-server i386 5.0.45-7.el5 base 9.7 M
Installing for dependencies:
mysql i386 5.0.45-7.el5 base 4.1 M
perl-DBD-MySQL i386 3.0007-1.fc6 base 147 k
Transaction Summary
=============================================================================
Install 3 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 14 M
Downloading Packages:
(1/3): mysql-5.0.45-7.el5 100% |=========================| 4.1 MB 02:57
(2/3): mysql-server-5.0.4 100% |=========================| 9.7 MB 07:19
(3/3): perl-DBD-MySQL-3.0 100% |=========================| 147 kB 00:07
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: mysql ######################### [1/3]
Installing: perl-DBD-MySQL ######################### [2/3]
Installing: mysql-server ######################### [3/3]
Installed: mysql-server.i386 0:5.0.45-7.el5
Dependency Installed: mysql.i386 0:5.0.45-7.el5 perl-DBD-MySQL.i386 0:3.0007-1.fc6
Complete!
安装PHP访问MySQL数据库的工具“php-mysql”
[root@centos5 ~]# yum -y install php-mysql
Loading "fastestmirror" plugin
Loading mirror speeds from cached hostfile
* base: centos.candishosting.com.cn
* updates: mirror.khlug.org
* addons: centos.candishosting.com.cn
* extras: centos.candishosting.com.cn
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package php-mysql.i386 0:5.1.6-20.el5_2.1 set to be updated
--> Processing Dependency: php-pdo for package: php-mysql
--> Running transaction check
---> Package php-pdo.i386 0:5.1.6-20.el5_2.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
php-mysql i386 5.1.6-20.el5_2.1 updates 84 k
Installing for dependencies:
php-pdo i386 5.1.6-20.el5_2.1 updates 62 k
Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 146 k
Downloading Packages:
(1/2): php-pdo-5.1.6-20.e 100% |=========================| 62 kB 00:20
(2/2): php-mysql-5.1.6-20 100% |=========================| 84 kB 00:11
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: php-pdo ######################### [1/2]
Installing: php-mysql ######################### [2/2]
Installed: php-mysql.i386 0:5.1.6-20.el5_2.1
Dependency Installed: php-pdo.i386 0:5.1.6-20.el5_2.1
Complete!
[root@centos5 ~]# vi /etc/my.cnf (编辑mysql配置文件)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set = utf8 (添加这一行,让MySQL的默认编码为UTF-8)
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
default-character-set = utf8 (添加这一行,设置mysql在安全模式启动后的默认编码为UTF-8)
[root@centos5 ~]# chkconfig mysqld on (设置mysql在开机后自动启动)
[root@centos5 ~]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos5 ~]# /etc/rc.d/init.d/mysqld start (启动mysql服务)
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
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:
/usr/bin/mysqladmin -u root password 'new-password'
(在这里系统提示可以使用mysqladmin为root,localhost和root,centos5设置密码,centos5是我的主机名,可使用这个命令的时候系统却报错了!)
/usr/bin/mysqladmin -u root -h centos5 password 'new-password'
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[ OK ]
Starting MySQL: [ OK ]
[root@centos5 /]# netstat -ntpl |grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LIST EN 8168/mysqld
[root@centos5 /]# mysql -u root -p (正如前面提到的报错,在这里是因为root这个mysql用户已经有非空的密码存在了。)
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
(在这里我在Internet上搜索到了一个解决的办法)
[root@centos5 /]# /etc/rc.d/init.d/mysqld stop (先停止mysql服务)
Stopping MySQL: [ OK ]
[root@centos5 /]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking&(重新启动mysql服务的时候加上这些参数,这些参数具体代表了什么,我也不明白,大概是设置启动服务的时候跳过验证密码的表和网络吧!)
[1] 8850
Starting mysqld daemon with databases from /var/lib/mysql
[root@centos5 /]# /etc/rc.d/init.d/mysqld status
mysqld (pid 8902) is running...
[root@centos5 /]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> (果然这次不用输入密码就可以进来了,也没有什么报错信息)
mysql> select user,host,password from mysql.user; (查看系用户表,果然localhost,root真的有密码,其他的两个都没有,如果用另外两个root用户登陆应该就不会出现之前的问题了)
+------+-----------+------------------+
| user | host | password |
+------+-----------+------------------+
| root | localhost | 7a3310dc39c1d3df |
| root | centos5 | |
| root | 127.0.0.1 | |
+------+-----------+------------------+
3 rows in set (0.01 sec)
mysql> UPDATE user SET Password=PASSWORD('123') where USER='root'; (接下来当然要修改下密码啦,这里就简单的设置成123)
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> show databases; (查看下当前的数据库)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
[root@centos5 /]# /etc/rc.d/init.d/mysqld stop (停止mysql服务)
STOPPING server from pid file /var/run/mysqld/mysqld.pid
090201 09:23:00 mysqld ended
Stopping MySQL: [ OK ]
[1]+ Done mysqld_safe --user=mysql --skip-grant-tables --skip-networking
[root@centos5 /]# /etc/rc.d/init.d/mysqld start (以正常的方式启动服务)
Starting MySQL: [ OK ]
[root@centos5 /]# mysql -u root -p (输入密码123登陆)
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select user,host from mysql.user; (查看用户,并删除另外的两个root用户)
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | centos5 |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)
mysql> delete from mysql.user where user='root' and host='127.0.0.1';
Query OK, 0 rows affected (0.01 sec)
mysql> delete from mysql.user where user='root' and host='centos5';
Query OK, 1 row affected (0.01 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)
mysql> exit
Bye
重新启动一次HTTP服务,让php-mysql反映到HTTP服务中
[root@centos5 /]# /etc/rc.d/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
本实验以此网页http://www.centospub.com/make/mysql.html为指导!mysql其他的基本操作可参考这个网页
5月2日,出现的错误及解决办法!
今天拿台新的VMware,使用yum安装完了mysql服务,启动的时候却出现了如下错误
[root@centos5 ~]# service mysqld start
Timeout error occurred trying to start MySQL Daemon.
Starting MySQL: [FAILED]
于是查看了下日志信息,似乎是因为mysql.host文件不存在引起的
[root@centos5 ~]# tail -f /var/log/mysqld.log
090502 20:05:26 mysqld started
090502 20:05:26 InnoDB: Started; log sequence number 0 43655
090502 20:05:26 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
090502 20:05:26 mysqld ended
在网上找到了解决的办法,具体如下:
[root@centos5 ~]# /usr/bin/mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
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:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h centos5 password 'new-password'
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
启动下mysql服务,这次可以正常启动了!
[root@centos5 ~]# service mysqld start
Starting MySQL: [ OK ]
再次查看下日志
[root@centos5 /]# tail -f /var/log/mysqld.log
090502 20:05:26 mysqld started
090502 20:05:26 InnoDB: Started; log sequence number 0 43655
090502 20:05:26 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
090502 20:05:26 mysqld ended
090502 20:10:16 mysqld started
090502 20:10:16 InnoDB: Started; log sequence number 0 43655
090502 20:10:17 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.0.45' socket: '/var/lib/mysql/mysql.sock' port: 3306 Source distribution
A mysqld process already exists at Sat May 2 20:10:39 CST 2009
查看3306端口是否开启
[root@centos5 /]# netstat -ntpl |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3473/mysqld
重启下mysql服务,看是否能正常运行
[root@centos5 /]# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL:
[ OK ]
最后,登陆试下!
[root@centos5 /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>