CentOS下MySQL数据库服务器的构建方法

 在提到网站构架的时候,我们经常能够看到“LAMP”的说法,它就是“Linux + Apache + MySQL + PHP”。MySQL+PHP的协同成功案例已经很多了。比如国外的PHPBBVBB,国内的Discuz!论坛等等程序,都是用PHP + MySQL实现的。和那些商务专用数据库比起来,MySQL的功能和规模可能被好多人说的逊色一些,但对于我们个人来说,它的功能已经算是完备。而且,通过Discuz!官方论坛上同时在线人数的数目,也可以证明MySQL的稳定性。  

  这里,介绍一下CentOSMySQL数据库服务器的构建方法。

安装MySQL及相关组件

  首先,安装MySQL

[root@sample ~]# yum -y install mysql-server ←安装MySQL

  然后,安装PHP访问MySQL数据库的工具“php-mysql”。

 [root@sample ~]# yum -y install php-mysql ←安装php-mysql

 

配置MySQL

[root@sample ~]# vi /etc/my.cnf ←编辑MySQL的配置文件

 

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).

old_passwords=1 ←找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8

default-character-set = utf8 ←添加这一行

 

然后在配置文件的文尾填加如下语句:

 

[mysql]

default-character-set = utf8

启动MySQL服务

 

  启动MySQL,并让MySQL在系统重新启动后随系统自动启动。

[root@sample ~]# chkconfig  mysqld on ←设置MySQL服务随系统启动自启动

 

[root@sample ~]# chkconfig --list mysqld ←确认MySQL自启动

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off ←如果2--5on的状态就OK

 

[root@sample ~]# /etc/rc.d/init.d/mysqld start ←启动MySQL服务

 

Initializing MySQL database:         [ OK ]

Starting MySQL:              [ OK ]

 

MySQL初始环境设定

[1]MySQLroot用户设置密码

 

  MySQL在刚刚被安装的时候,它的root用户是没有被设置密码的。首先来设置MySQLroot密码。

 

 

 

[root@sample ~]# mysql -u root ←用root用户登录MySQL服务器

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> select user,host,password from mysql.user; ←查看用户信息

+------+------------------------------+---------------+

| user | host          | password |

+------+------------------------------+---------------+

| root | localhost             | ← root密码为空

| root | sample.centospub.com  |       | ← root密码为空

|   | sample.centospub.com |       |

|   | localhost       |       |

+------+------------------------------+---------------+

4 rows in set (0.00 sec)

 

mysql> set password for root@localhost=password('在这里填入root密码'); ←设置root密码

Query OK, 0 rows affected (0.01 sec)

 

mysql> set password for root@'sample.centospub.com'=password('在这里填入root密码'); ←设置root密码

Query OK, 0 rows affected (0.01 sec)

 

mysql> select user,host,password from mysql.user; ←查看用户信息

+------+--------------------------------+--------------------------+

| user | host          | password    |

+------+--------------------------------+--------------------------+

| root | localhost        | 19b68057189b027f | ← root密码被设置

| root | sample.centospub.com   | 19b68057189b027f | ← root密码被设置

   | sample.centospub.com   |          |

   | localhost        |          |

+------+--------------------------------+--------------------------+

4 rows in set (0.01 sec)

 

mysql> exit ←退出MySQL服务器

Bye

  然后,测试一下root密码有没有生效。

[root@sample ~]# mysql -u root ←通过空密码用root登录

 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ←出现此错误信息说明密码设置成功

 

[root@localhost ~]# mysql -u root -h sample.centospub.com ←通过空密码用root登录

 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ←出现此错误信息说明密码设置成功

 

[root@sample ~]# mysql -u root -p ←通过密码用root登录

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g. ←确认用密码能够成功登录

Your MySQL connection id is 5 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> exit

Bye

 

[root@sample ~]# mysql -u root -h sample.centospub.com -p ←通过密码用root登录

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g. ←确认用密码能够成功登录

Your MySQL connection id is 6 to server version: 4.1.20

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> exit ←退出MySQL服务器

Bye

 

 

[2] 删除匿名用户

 

  在MySQL刚刚被安装后,存在用户名、密码为空的用户。这使得数据库服务器有无需密码被登录的可能性。为消除隐患,将匿名用户删除。

 

 

 

[root@sample ~]# mysql -u root -p ←通过密码用root登录

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> select user,host from mysql.user; ←查看用户信息

+------+----------------------------+

| user | host         |

+------+----------------------------+

|   | localhost      |

 

|   | 127.0.0.1       |

| root | localhost      |

|   | sample.centospub.com |

| root | sample.centospub.com  |

+------+----------------------------+

4 rows in set (0.02 sec)

 

mysql> delete from mysql.user where user=''; ←删除匿名用户

Query OK, 2 rows affected (0.17 sec)

 

mysql> select user,host from mysql.user; ←查看用户信息

+------+----------------------------+

| user | host         |

+------+----------------------------+

| root | localhost      |

| root | sample.centospub.com |

+------+----------------------------+

2 rows in set (0.00 sec)

 

mysql> exit ←退出MySQL服务器

Bye

[3] 删除测试用数据库

 

  在MySQL被安装后,存在名为test的空数据库,将它删除。这里要注意的是,系统默认的还有一个名为mysql的数据库,它用于系统管理,所以请不要删除。

 

 

 

[root@sample ~]# mysql -u root -p ←通过密码用root登录

Enter password: ←在这里输入密码

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> show databases; ←查看系统已存在的数据库

+-------------+

| Database |

+-------------+

| mysql   |

| test   |

+------------+

2 rows in set (0.02 sec)

 

mysql> drop database test; ←删除名为test的空数据库

Query OK, 0 rows affected (0.07 sec)

 

mysql> show databases; ←查看系统已存在的数据库

+-------------+

| Database |

+-------------+

| mysql  | ←确认名为test的数据库被删除,已不存在

+-------------+

1 row in set (0.00 sec)

 

mysql> exit ←退出MySQL服务器

Bye

测试MySQL

 

  下面对MySQL进行测试。包括建立新用户,以及用对关系性数据库进行数据库操作的指令来试着建立数据库及数据表。这里,新建用户以centospub为例。

[root@sample ~]# mysql -u root -p ←通过密码用root登录

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 9 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> grant all privileges on test.* to centospub@localhost identified by '在这里定义密码'; ←建立对test数据库有完全操作权限的名为centospub的用户

Query OK, 0 rows affected (0.03 sec)

 

mysql> select user from mysql.user where user='centospub'; ←确认centospub用户的存在与否

+---------+

| user  |

+---------+

| centospub | ←确认centospub已经被建立

+---------+

1 row in set (0.01 sec)

 

mysql> exit ←退出MySQL服务器

Bye

 

[root@sample ~]# mysql -u centospub -p ←用新建立的centospub用户登录MySQL服务器

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 10 to server version: 4.1.20

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> create database test; ←建立名为test的数据库

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases; ←查看系统已存在的数据库

+-------------+

| Database |

+-------------+

| test    |

+-------------+

1 row in set (0.00 sec)

 

mysql> use test ←连接到数据库

Database changed

 

mysql> create table test(numint, name varchar(50)); ←在数据库中建立表

Query OK, 0 rows affected (0.03 sec)

 

mysql> show tables; ←查看数据库中已存在的表

+-------------------+

| Tables_in_test |

+-------------------+

| test     |

+-------------------+

1 row in set (0.01 sec)

 

mysql> insert into test values(1,'Hello World!'); ←插入一个值到表中

Query OK, 1 row affected (0.02 sec)

 

mysql> select * from test; ←查看数据库中的表的信息

+------+-------------------+

| num | name      |

+------+-------------------+

| 1   | Hello World!  |

+------+-------------------+

1 row in set (0.00 sec)

 

mysql> update test set name='Hello Everyone!'; ←更新表的信息,赋予新的值

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

 

mysql> select * from test; ←查看数据库中的表的信息

+------+----------------------+

| num | name      |

+------+----------------------+

| 1   | Hello Everyone! | ←确认被更新到新的值

+------+----------------------+

1 row in set (0.01 sec)

 

mysql> delete from test where num=1; ←删除表内的值

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from test; ←确认删除结果

Empty set (0.01 sec)

 

mysql> drop table test; ←删除表

Query OK, 0 rows affected (0.01 sec)

 

mysql> show tables; ←查看表信息

Empty set (0.00 sec) ←确认表已被删除

 

mysql> drop database test; ←删除名为test的数据库

Query OK, 0 rows affected (0.01 sec)

 

mysql> show databases; ←查看已存在的数据库

Empty set (0.01 sec) ←确认test数据库已被删除(这里非root用户的关系,看不到名为mysql的数据库)

 

mysql> exit ←退出MySQL服务器

Bye

 

 

  然后,删除测试用过的遗留用户。

[root@sample ~]# mysql -u root -p ←通过密码用root登录

Enter password: ←在这里输入密码

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 12 to server version: 4.1.20

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> revoke all privileges on *.* from centospub@localhost; ←取消centospub用户对数据库的操作权限

Query OK, 0 rows affected (0.00 sec)

 

mysql> delete from mysql.user where user='centospub' and host='localhost'; ←删除centospub用户

Query OK, 1 row affected (0.01 sec)

 

mysql> select user from mysql.user where user='centospub'; ←查找用户centospub,确认已删除与否

Empty set (0.01 sec) ←确认centospub用户已不存在

 

mysql> flush privileges; ←刷新,使以上操作生效

Query OK, 0 rows affected (0.01 sec)

 

mysql> exit

Bye

 

  最后,重新启动一次HTTP服务,让php-mysql反映到HTTP服务中。

[root@sample ~]# /etc/rc.d/init.d/httpd restart ←重新启动HTTP服务

Stopping httpd:             [ OK ]

Starting httpd:             [ OK ] 

 

你可能感兴趣的:(数据库,服务器)