在CentOS上MySQL数据库服务器配置方法

很文博客hinwi.com 数据库评论382字数 12648阅读模式
广告也精彩

最近工作中经常需要使用到MySQL,有时候在WINXP,有时候在Linux中,而这次,需要在CentOS中配置一下,还需要用到phpmyadmin, 在网上搜了不少的资料。
无意中还找到了CentOS的专业论坛
于是,开始的学习配置
本次环境:
CentOS系统,
mysql-server(版本忘了,因为是使用yum安装的)
过程如下(转载,根据本人安装详细要求,略加修改) :
在提到网站构架的时候,我们经常能够看到“LAMP”的说法,它就是“Linux + Apache + MySQL + PHP”。 MySQL+PHP的协同成功案例已经很多了。比如国外的PHPBB,VBB,国内的Discuz!论坛等等程序,都是用PHP + MySQL实现的。和那些商务专用数据库比起来,MySQL的功能和规模可能被好多人说的逊色一些,但对于我们个人来说,它的功能已经算是完备。而且,通 过Discuz!官方论坛上同时在线人数的数目,也可以证明MySQL的稳定性。

这里,介绍一下CentOS下MySQL数据库服务器的构建方法。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

安装MySQL及相关组件文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

首先,安装MySQL。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

  1. [root@sample ~]# yum -y install mysql-server  ← 安装MySQL
  2. Setting up Install Process
  3. Setting up repositories
  4. dag 100% |=========================| 1.1 kB 00:00
  5. update 100% |=========================| 951 B 00:00
  6. base 100% |=========================| 1.1 kB 00:00
  7. addons 100% |=========================| 951 B 00:00
  8. extras 100% |=========================| 1.1 kB 00:00
  9. Reading repository metadata in from local files
  10. primary.xml.gz 100% |=========================| 106 kB 00:00
  11. update : ################################################## 261/261
  12. Added 5 new packages, deleted 0 old in 2.02 seconds
  13. Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only
  14. Finished
  15. Parsing package install arguments
  16. Resolving Dependencies
  17. --> Populating transaction set with selected packages. Please wait.
  18. ---> Downloading header for mysql-server to pack into transaction set.
  19. mysql-server-4.1.20-1.RHE 100% |=========================| 29 kB 00:00
  20. ---> Package mysql-server.i386 0:4.1.20-1.RHEL4.1 set to be updated
  21. --> Running transaction check
  22. --> Processing Dependency: libmysqlclient.so.14(libmysqlclient_14) for package: mysql-server
  23. --> Processing Dependency: perl-DBD-MySQL for package: mysql-server
  24. --> Processing Dependency: perl-DBI for package: mysql-server
  25. --> Processing Dependency: perl(DBI) for package: mysql-server
  26. --> Processing Dependency: mysql = 4.1.20-1.RHEL4.1 for package: mysql-server
  27. --> Processing Dependency: libmysqlclient.so.14 for package: mysql-server
  28. --> Processing Dependency: libmysqlclient_r.so.14(libmysqlclient_14) for package: mysql-server
  29. --> Processing Dependency: libmysqlclient_r.so.14 for package: mysql-server
  30. --> Restarting Dependency Resolution with new changes.
  31. --> Populating transaction set with selected packages. Please wait.
  32. ---> Downloading header for perl-DBD-MySQL to pack into transaction set.
  33. perl-DBD-MySQL-2.9004-3.1 100% |=========================| 5.4 kB 00:00
  34. ---> Package perl-DBD-MySQL.i386 0:2.9004-3.1 set to be updated
  35. ---> Downloading header for mysql to pack into transaction set.
  36. mysql-4.1.20-1.RHEL4.1.i3 100% |=========================| 35 kB 00:00
  37. ---> Package mysql.i386 0:4.1.20-1.RHEL4.1 set to be updated
  38. ---> Downloading header for perl-DBI to pack into transaction set.
  39. perl-DBI-1.40-8.i386.rpm 100% |=========================| 11 kB 00:00
  40. ---> Package perl-DBI.i386 0:1.40-8 set to be updated
  41. --> Running transaction check
  42. Dependencies Resolved
  43. =============================================================================
  44. Package Arch Version Repository Size
  45. =============================================================================
  46. Installing:
  47. mysql-server i386 4.1.20-1.RHEL4.1 update 9.8 M
  48. Installing for dependencies:
  49. mysql i386 4.1.20-1.RHEL4.1 update 2.9 M
  50. perl-DBD-MySQL i386 2.9004-3.1 base 111 k
  51. perl-DBI i386 1.40-8 base 466 k
  52. Transaction Summary
  53. =============================================================================
  54. Install 4 Package(s)
  55. Update 0 Package(s)
  56. Remove 0 Package(s)
  57. Total download size: 13 M
  58. Downloading Packages:
  59. (1/4): perl-DBD-MySQL-2.9 100% |=========================| 111 kB 00:00
  60. (2/4): mysql-server-4.1.2 100% |=========================| 9.8 MB 00:11
  61. (3/4): mysql-4.1.20-1.RHE 100% |=========================| 2.9 MB 00:03
  62. (4/4): perl-DBI-1.40-8.i3 100% |=========================| 466 kB 00:01
  63. Running Transaction Test
  64. Finished Transaction Test
  65. Transaction Test Succeeded
  66. Running Transaction
  67. Installing: perl-DBI ######################### [1/4]
  68. Installing: mysql ######################### [2/4]
  69. Installing: perl-DBD-MySQL ######################### [3/4]
  70. Installing: mysql-server ######################### [4/4]
  71. Installed: mysql-server.i386 0:4.1.20-1.RHEL4.1
  72. Dependency Installed: mysql.i386 0:4.1.20-1.RHEL4.1 perl-DBD-MySQL.i386 0:2.9004-3.1 perl-DBI.i386 0:1.40-8
  73. Complete!

然后,安装PHP访问MySQL数据库的工具“php-mysql”。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

  1. [root@sample ~]# yum -y install php-mysql  ← 安装php-mysql
  2. Setting up Install Process
  3. Setting up repositories
  4. Reading repository metadata in from local files
  5. Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only
  6. Finished
  7. Parsing package install arguments
  8. Resolving Dependencies
  9. --> Populating transaction set with selected packages. Please wait.
  10. ---> Downloading header for php-mysql to pack into transaction set.
  11. php-mysql-4.3.9-3.15.i386 100% |=========================| 18 kB 00:00
  12. ---> Package php-mysql.i386 0:4.3.9-3.15 set to be updated
  13. --> Running transaction check
  14. Dependencies Resolved
  15. =============================================================================
  16. Package Arch Version Repository Size
  17. =============================================================================
  18. Installing:
  19. php-mysql i386 4.3.9-3.15 update 35 k
  20. Transaction Summary
  21. =============================================================================
  22. Install 1 Package(s)
  23. Update 0 Package(s)
  24. Remove 0 Package(s)
  25. Total download size: 35 k
  26. Downloading Packages:
  27. (1/1): php-mysql-4.3.9-3. 100% |=========================| 35 kB 00:00
  28. Running Transaction Test
  29. Finished Transaction Test
  30. Transaction Test Succeeded
  31. Running Transaction
  32. Installing: php-mysql ######################### [1/1]
  33. Installed: php-mysql.i386 0:4.3.9-3.15
  34. Complete!

配置MySQL
然后,配置MySQL。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

  1. [root@sample ~]# vi /etc/my.cnf  ← 编辑MySQL的配置文件
  2. [mysqld]
  3. datadir=/var/lib/mysql
  4. socket=/var/lib/mysql/mysql.sock
  5. # Default to using old password format for compatibility with mysql 3.x
  6. # clients (those using the mysqlclient10 compatibility package).
  7. old_passwords=1  ← 找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8
  8. default-character-set = utf8  ← 添加这一行
  9. 然后在配置文件的文尾填加如下语句:
  10. [mysql]
  11. default-character-set = utf8

启动MySQL服务文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

启动MySQL,并让MySQL在系统重新启动后随系统自动启动。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/46655.html

  1. [root@sample ~]# chkconfig mysqld on  ← 设置MySQL服务随系统启动自启动
  2. [root@sample ~]# chkconfig --list mysqld  ← 确认MySQL自启动
  3. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 如果2--5为on的状态就OK
  4. [root@sample ~]# /etc/rc.d/init.d/mysqld start  ← 启动MySQL服务
  5. Initializing MySQL database:         [ OK ]
  6. Starting MySQL:              [ OK ]

MySQL初始环境设定

[1]为MySQL的root用户设置密码

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

  1. [root@sample ~]# mysql -u root  ← 用root用户登录MySQL服务器
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 2 to server version: 4.1.20
  4. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  5. mysql> select user,host,password from mysql.user;  ← 查看用户信息
  6. +------+------------------------------+---------------+
  7. | user | host          | password |
  8. +------+------------------------------+---------------+
  9. | root | localhost        |       |  ← root密码为空
  10. | root | sample.centospub.com  |       |  ← root密码为空
  11. |   | sample.centospub.com |       |
  12. |   | localhost       |       |
  13. +------+------------------------------+---------------+
  14. 4 rows in set (0.00 sec)
  15. mysql> set password for root@localhost=password('在这里填入root密码');  ← 设置root密码
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> set password for root@'sample.centospub.com'=password('在这里填入root密码');  ← 设置root密码
  18. Query OK, 0 rows affected (0.01 sec)
  19. mysql> select user,host,password from mysql.user;  ← 查看用户信息
  20. +------+--------------------------------+--------------------------+
  21. | user | host          | password     |
  22. +------+--------------------------------+--------------------------+
  23. | root | localhost        | 19b68057189b027f |  ← root密码被设置
  24. | root | sample.centospub.com   | 19b68057189b027f |  ← root密码被设置
  25. |    | sample.centospub.com   |          |
  26. |    | localhost        |          |
  27. +------+--------------------------------+--------------------------+
  28. 4 rows in set (0.01 sec)
  29. mysql> exit  ← 退出MySQL服务器
  30. Bye

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

  1. [root@sample ~]# mysql -u root  ← 通过空密码用root登录
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)  ← 出现此错误信息说明密码设置成功
  3. [root@localhost ~]# mysql -u root -h sample.centospub.com  ← 通过空密码用root登录
  4. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)  ← 出现此错误信息说明密码设置成功
  5. [root@sample ~]# mysql -u root -p  ← 通过密码用root登录
  6. Enter password:  ← 在这里输入密码
  7. Welcome to the MySQL monitor. Commands end with ; or \g.  ← 确认用密码能够成功登录
  8. Your MySQL connection id is 5 to server version: 4.1.20
  9. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  10. mysql> exit
  11. Bye
  12. [root@sample ~]# mysql -u root -h sample.centospub.com -p  ← 通过密码用root登录
  13. Enter password:  ← 在这里输入密码
  14. Welcome to the MySQL monitor. Commands end with ; or \g.  ← 确认用密码能够成功登录
  15. Your MySQL connection id is 6 to server version: 4.1.20
  16. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  17. mysql> exit  ← 退出MySQL服务器
  18. Bye

[2] 删除匿名用户

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

  1. [root@sample ~]# mysql -u root -p  ← 通过密码用root登录
  2. Enter password:  ← 在这里输入密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 7 to server version: 4.1.20
  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  6. mysql> select user,host from mysql.user;  ← 查看用户信息
  7. +------+----------------------------+
  8. | user | host         |
  9. +------+----------------------------+
  10. |   | localhost       |
  11. |   | 127.0.0.1       |
  12. | root | localhost       |
  13. |   | sample.centospub.com |
  14. | root | sample.centospub.com  |
  15. +------+----------------------------+
  16. 4 rows in set (0.02 sec)
  17. mysql> delete from mysql.user where user='';  ← 删除匿名用户
  18. Query OK, 2 rows affected (0.17 sec)
  19. mysql> select user,host from mysql.user;  ← 查看用户信息
  20. +------+----------------------------+
  21. | user | host         |
  22. +------+----------------------------+
  23. | root | localhost      |
  24. | root | sample.centospub.com |
  25. +------+----------------------------+
  26. 2 rows in set (0.00 sec)
  27. mysql> exit  ← 退出MySQL服务器
  28. Bye

[3] 删除测试用数据库

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

  1. [root@sample ~]# mysql -u root -p  ← 通过密码用root登录
  2. Enter password:  ← 在这里输入密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8 to server version: 4.1.20
  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  6. mysql> show databases;  ← 查看系统已存在的数据库
  7. +-------------+
  8. | Database |
  9. +-------------+
  10. | mysql   |
  11. | test   |
  12. +------------+
  13. 2 rows in set (0.02 sec)
  14. mysql> drop database test;  ← 删除名为test的空数据库
  15. Query OK, 0 rows affected (0.07 sec)
  16. mysql> show databases;  ← 查看系统已存在的数据库
  17. +-------------+
  18. | Database |
  19. +-------------+
  20. | mysql   |  ← 确认名为test的数据库被删除,已不存在
  21. +-------------+
  22. 1 row in set (0.00 sec)
  23. mysql> exit  ← 退出MySQL服务器
  24. Bye

测试MySQL

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

  1. [root@sample ~]# mysql -u root -p  ← 通过密码用root登录
  2. Enter password:  ← 在这里输入密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 9 to server version: 4.1.20
  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  6. mysql> grant all privileges on test.* to centospub@localhost identified by '在这里定义密码';  ← 建立对test数据库有完全操作权限的名为centospub的用户
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> select user from mysql.user where user='centospub';  ← 确认centospub用户的存在与否
  9. +---------+
  10. | user  |
  11. +---------+
  12. | centospub |  ← 确认centospub已经被建立
  13. +---------+
  14. 1 row in set (0.01 sec)
  15. mysql> exit  ← 退出MySQL服务器
  16. Bye
  17. [root@sample ~]# mysql -u centospub -p  ← 用新建立的centospub用户登录MySQL服务器
  18. Enter password:  ← 在这里输入密码
  19. Welcome to the MySQL monitor. Commands end with ; or \g.
  20. Your MySQL connection id is 10 to server version: 4.1.20
  21. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  22. mysql> create database test;  ← 建立名为test的数据库
  23. Query OK, 1 row affected (0.00 sec)
  24. mysql> show databases;  ← 查看系统已存在的数据库
  25. +-------------+
  26. | Database |
  27. +-------------+
  28. | test    |
  29. +-------------+
  30. 1 row in set (0.00 sec)
  31. mysql> use test  ← 连接到数据库
  32. Database changed
  33. mysql> create table test(num int, name varchar(50));  ← 在数据库中建立表
  34. Query OK, 0 rows affected (0.03 sec)
  35. mysql> show tables;  ← 查看数据库中已存在的表
  36. +-------------------+
  37. | Tables_in_test |
  38. +-------------------+
  39. | test     |
  40. +-------------------+
  41. 1 row in set (0.01 sec)
  42. mysql> insert into test values(1,'Hello World!');  ← 插入一个值到表中
  43. Query OK, 1 row affected (0.02 sec)
  44. mysql> select * from test;  ← 查看数据库中的表的信息
  45. +------+-------------------+
  46. | num | name      |
  47. +------+-------------------+
  48. | 1   | Hello World!  |
  49. +------+-------------------+
  50. 1 row in set (0.00 sec)
  51. mysql> update test set name='Hello Everyone!';  ← 更新表的信息,赋予新的值
  52. Query OK, 1 row affected (0.00 sec)
  53. Rows matched: 1 Changed: 1 Warnings: 0
  54. mysql> select * from test;  ← 查看数据库中的表的信息
  55. +------+----------------------+
  56. | num | name      |
  57. +------+----------------------+
  58. | 1   | Hello Everyone! |  ← 确认被更新到新的值
  59. +------+----------------------+
  60. 1 row in set (0.01 sec)
  61. mysql> delete from test where num=1;  ← 删除表内的值
  62. Query OK, 1 row affected (0.00 sec)
  63. mysql> select * from test;  ← 确认删除结果
  64. Empty set (0.01 sec)
  65. mysql> drop table test;  ← 删除表
  66. Query OK, 0 rows affected (0.01 sec)
  67. mysql> show tables;  ← 查看表信息
  68. Empty set (0.00 sec)  ← 确认表已被删除
  69. mysql> drop database test;  ← 删除名为test的数据库
  70. Query OK, 0 rows affected (0.01 sec)
  71. mysql> show databases;  ← 查看已存在的数据库
  72. Empty set (0.01 sec)  ← 确认test数据库已被删除(这里非root用户的关系,看不到名为mysql的数据库)
  73. mysql> exit  ← 退出MySQL服务器
  74. Bye

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

  1. [root@sample ~]# mysql -u root -p  ← 通过密码用root登录
  2. Enter password:  ← 在这里输入密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12 to server version: 4.1.20
  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  6. mysql> revoke all privileges on *.* from centospub@localhost;  ← 取消centospub用户对数据库的操作权限
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> delete from mysql.user where user='centospub' and host='localhost';  ← 删除centospub用户
  9. Query OK, 1 row affected (0.01 sec)
  10. mysql> select user from mysql.user where user='centospub';  ← 查找用户centospub,确认已删除与否
  11. Empty set (0.01 sec)  ← 确认centospub用户已不存在
  12. mysql> flush privileges;  ← 刷新,使以上操作生效
  13. Query OK, 0 rows affected (0.01 sec)
  14. mysql> exit
  15. Bye

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

  1. [root@sample ~]# /etc/rc.d/init.d/httpd restart  ← 重新启动HTTP服务
  2. Stopping httpd:             [ OK ]
  3. Starting httpd:             [ OK ]

终于搞掂!
(*^__^*) 嘻嘻……,接下来配置phpmyadmin....

工具:作品在线观看

女优:最新作品观看

中文:国语在线观看

weinxin
我的微信
扫一扫更精彩
大家的支持是我更新的动力!!!
 
广告也精彩
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证