CentOS Linux 5/6安装Mysql数据库服务器

时间:2020-01-09 10:34:10  来源:igfitidea点击:

如何使用命令行选项在CentOS Linux版本5或版本6上安装MySQL数据库服务器?
如何在CentOS Linux上配置MySQL服务器?
您需要在CentOS Linux上安装以下软件包:

  • mysql-server:MySQL服务器和相关文件
  • mysql:MySQL客户端程序和共享库

另外,您可能需要安装以下软件包才能使用各种编程语言访问mysql:

  • php-mysql:使用MySQL数据库的PHP应用程序模块。
  • perl-DBD-MySQL:Perl的MySQL接口。
  • MySQL-python:MySQL的接口。

如何在CentOS上安装MySQL

要安装mysql服务器,请输入以下yum命令:

# yum install mysql-server mysql

输出示例:

Loaded plugins: downloadonly, fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.einstein.yu.edu
* extras: mirrors.loosefoot.com
* updates: yum.singlehop.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.x86_64 0:5.1.71-1.el6 will be installed
---> Package mysql-server.x86_64 0:5.1.71-1.el6 will be installed
--> Processing Dependency: perl-DBI for package: mysql-server-5.1.71-1.el6.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.71-1.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-server-5.1.71-1.el6.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
===================================================================
Package             Arch        Version           Repository
                                                              Size
===================================================================
Installing:
mysql               x86_64      5.1.71-1.el6      base      893 k
mysql-server        x86_64      5.1.71-1.el6      base      8.6 M
Installing for dependencies:
perl-DBD-MySQL      x86_64      4.013-3.el6       base      134 k
perl-DBI            x86_64      1.609-4.el6       base      705 k
 
Transaction Summary
===================================================================
Install       4 Package(s)
 
Total download size: 10 M
Installed size: 29 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): mysql-5.1.71-1.el6.x86_64.rpm        | 893 kB     00:00     
(2/4): mysql-server-5.1.71-1.el6.x86_64.rpm | 8.6 MB     00:01     
(3/4): perl-DBD-MySQL-4.013-3.el6.x86_64.rp | 134 kB     00:00     
(4/4): perl-DBI-1.609-4.el6.x86_64.rpm      | 705 kB     00:00     
------------------------------------------------------------------
Total                              4.2 MB/s |  10 MB     00:02     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : perl-DBI-1.609-4.el6.x86_64                     1/4 
  Installing : perl-DBD-MySQL-4.013-3.el6.x86_64               2/4 
  Installing : mysql-5.1.71-1.el6.x86_64                       3/4 
  Installing : mysql-server-5.1.71-1.el6.x86_64                4/4 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64               1/4 
  Verifying  : mysql-server-5.1.71-1.el6.x86_64                2/4 
  Verifying  : mysql-5.1.71-1.el6.x86_64                       3/4 
  Verifying  : perl-DBI-1.609-4.el6.x86_64                     4/4 
 
Installed:
  mysql.x86_64 0:5.1.71-1.el6  mysql-server.x86_64 0:5.1.71-1.el6 
 
Dependency Installed:
  perl-DBD-MySQL.x86_64 0:4.013-3.el6                              
  perl-DBI.x86_64 0:1.609-4.el6                                    
 
Complete!

导入CentOS Mysql服务器文件和端口

  • MySQL服务器配置文件:/etc/my.cnf
  • MySQL的默认TCP端口:3306(TCP/UDP)
  • Mysql服务器日志文件:/var/log/mysqld.log
  • Mysql数据库存储目录:/var/lib/mysql /
  • 用于启动/停止/重启服务器的Mysql rc脚本:`/etc/init.d/mysqld {start | stop | restart | reload | status | try-restart}

如何在CentOS Linux上配置MySQL

首先,打开mysql服务,键入以下chkconfig命令:

# chkconfig mysqld on

如何在CentOS Linux上启动/停止/重启mysql服务器

使用以下任一命令来启动/停止/重启mysql服务器:

# service mysqld start
# service mysqld restart
# service mysqld stop

或者

# /etc/init.d/mysqld start
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld restart

输出示例:
在centos上启动/停止/重启mysql服务器

设置root用户密码

根用户是mysql admin用户。
默认情况下,没有密码分配给root用户。
使用以下命令为root用户设置新密码:

# mysqladmin -u root password NEWPASSWORD

有关更多信息,请参见如何更改/更新mysqld的root密码。

通过/etc/my.cnf配置文件配置mysqld

编辑/etc/my.cnf,输入:

# vi /etc/my.cnf

示例默认配置文件:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Mysql CentOS Linux防火墙配置

编辑/etc/sysconfig/iptables文件,输入:

# vi /etc/sysconfig/iptables

在CentOS基本服务器上附加以下代码以打开tcp端口3306:

-A INPUT -m state --state NEW,ESTABLISHED -m tcp -p tcp --dport 3306 -j ACCEPT

保存并关闭文件。
要在CentOS/RHEL上重新启动防火墙,请输入:

# service iptables restart

有关更多信息,请参见CentOS/Redhat Iptables防火墙配置教程。

如何查看mysql服务器日志?

要查看服务器错误日志,请使用以下tail命令:

# tail -f /var/log/mysqld.log

输出示例:

131202 11:44:41 [Note] /usr/libexec/mysqld: Shutdown complete

131202 11:44:41 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
131202 11:44:41 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
131202 11:44:41  InnoDB: Initializing buffer pool, size = 8.0M
131202 11:44:41  InnoDB: Completed initialization of buffer pool
131202 11:44:41  InnoDB: Started; log sequence number 0 44233
131202 11:44:41 [Note] Event Scheduler: Loaded 0 events
131202 11:44:41 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.71'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution

如何连接到mysql服务器?

使用mysql命令行,如下所示:

$ mysql -u USER-NAME-HERE -p'PASSWORD-HERE'
$ mysql -u USER-NAME-HERE -p'PASSWORD-HERE' -h 'SERVER-NAME-HOST-NAME-HERE'
$ mysql -u USER-NAME-HERE -p'PASSWORD-HERE' -h 'SERVER-NAME-HOST-NAME-HERE' DB-NAME_HERE
$ 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.1.71 Source distribution
 
Copyright (c) 2000, 2013, 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>