Linux学习之CentOS(一)--CentOS6.6下Mysql数据库的安装与配置

时间:2021-01-19 11:29:01

在这里我是通过yum来进行mysql数据库的安装的,通过这种方式进行安装,可以将跟mysql相关的一些服务、jar包都给我们安装好,所以省去了很多不必要的麻烦!!!

[root@larry ~]# cat /etc/redhat-release  //查看操作系统版本
CentOS release 6.6 (Final)

1.卸载掉原有mysql

因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面,我们可以通过如下命令来查看我们的操作系统上是否已经安装了mysql数据库,

方法1:

[root@larry ~]# rpm -qa | grep mysql  // 这个命令就会查看该操作系统上是否已经安装了mysql数据库

方法2:

[root@localhost ~]# whereis mysql

mysql:

以上都表示该机器没有安装mysql;

有的话,我们就通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉

[root@larry ~]# rpm -e mysql  // 普通删除模式 
[root@larry ~]# rpm -e --nodeps mysql  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

在删除完以后我们可以通过 rpm -qa | grep mysql 命令来查看mysql是否已经卸载成功!!

2.通过yum来进行mysql的安装

我是通过yum的方式来进行mysql的数据库安装,首先我们可以输入 yum list | grep mysql 命令来查看yum上提供的mysql数据库可下载的版本:

[root@larry ~]# yum list | grep mysql

就可以得到yum服务器上mysql数据库的可下载版本信息:

[root@localhost ~]# yum list | grep mysql
apr-util-mysql.x86_64                    1.3.9-3.el6_0.1             base       
bacula-director-mysql.x86_64             5.0.0-13.el6                base       
bacula-storage-mysql.x86_64              5.0.0-13.el6                base       
dovecot-mysql.x86_64                     1:2.0.9-22.el6              base       
freeradius-mysql.x86_64                  2.2.6-6.el6_7               base       
libdbi-dbd-mysql.x86_64                  0.8.3-5.1.el6               base       
mod_auth_mysql.x86_64                    1:3.0.0-11.el6_0.1          base       
mysql.x86_64                             5.1.73-7.el6                base       
mysql-bench.x86_64                       5.1.73-7.el6                base       
mysql-connector-java.noarch              1:5.1.17-6.el6              base       
mysql-connector-odbc.x86_64              5.1.5r1144-7.el6            base       
mysql-devel.i686                         5.1.73-7.el6                base       
mysql-devel.x86_64                       5.1.73-7.el6                base       
mysql-embedded.i686                      5.1.73-7.el6                base       
mysql-embedded.x86_64                    5.1.73-7.el6                base       
mysql-embedded-devel.i686                5.1.73-7.el6                base       
mysql-embedded-devel.x86_64              5.1.73-7.el6                base       
mysql-libs.i686                          5.1.73-7.el6                base       
mysql-libs.x86_64                        5.1.73-7.el6                base       
mysql-server.x86_64                      5.1.73-7.el6                base       
mysql-test.x86_64                        5.1.73-7.el6                base       
pcp-pmda-mysql.x86_64                    3.10.9-6.el6                base       
php-mysql.x86_64                         5.3.3-48.el6_8              updates    
qt-mysql.i686                            1:4.6.2-28.el6_5            base       
qt-mysql.x86_64                          1:4.6.2-28.el6_5            base       
rsyslog-mysql.x86_64                     5.8.10-10.el6_6             base       
rsyslog7-mysql.x86_64                    7.4.10-5.el6                base 

  然后我们可以通过输入 yum install -y mysql-server mysql mysql-devel 命令将 mysql mysql-server mysql-devel都安装好(注意:安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行)

[root@localhost ~]# yum install -y mysql-server mysql mysql-deve
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.unifiedlayer.com
 * extras: mirror.supremebytes.com
 * updates: repos.lax.quadranet.com
No package mysql-deve available.
Resolving Dependencies
--> Running transaction check
---> Package mysql.x86_64 0:5.1.73-7.el6 will be installed
--> Processing Dependency: mysql-libs = 5.1.73-7.el6 for package: mysql-5.1.73-7.el6.x86_64
--> Processing Dependency: libmysqlclient_r.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-7.el6.x86_64
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-7.el6.x86_64
--> Processing Dependency: libmysqlclient_r.so.16()(64bit) for package: mysql-5.1.73-7.el6.x86_64
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: mysql-5.1.73-7.el6.x86_64
---> Package mysql-server.x86_64 0:5.1.73-7.el6 will be installed
--> Processing Dependency: perl-DBI for package: mysql-server-5.1.73-7.el6.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.73-7.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-server-5.1.73-7.el6.x86_64
--> Running transaction check
---> Package mysql-libs.x86_64 0:5.1.73-7.el6 will be installed
---> 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.73-7.el6                   base                 894 k
 mysql-server                   x86_64                 5.1.73-7.el6                   base                 8.6 M
Installing for dependencies:
 mysql-libs                     x86_64                 5.1.73-7.el6                   base                 1.2 M
 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       5 Package(s)

Total download size: 12 M
Installed size: 33 M
Downloading Packages:
(1/5): mysql-5.1.73-7.el6.x86_64.rpm                                                      | 894 kB     00:00     
(2/5): mysql-libs-5.1.73-7.el6.x86_64.rpm                                                 | 1.2 MB     00:00     
(3/5): mysql-server-5.1.73-7.el6.x86_64.rpm                                               | 8.6 MB     00:01     
(4/5): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm                                              | 134 kB     00:00     
(5/5): perl-DBI-1.609-4.el6.x86_64.rpm                                                    | 705 kB     00:00     
-----------------------------------------------------------------------------------------------------------------
Total                                                                            4.6 MB/s |  12 MB     00:02     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mysql-libs-5.1.73-7.el6.x86_64                                                                1/5 
  Installing : perl-DBI-1.609-4.el6.x86_64                                                                   2/5 
  Installing : perl-DBD-MySQL-4.013-3.el6.x86_64                                                             3/5 
  Installing : mysql-5.1.73-7.el6.x86_64                                                                     4/5 
  Installing : mysql-server-5.1.73-7.el6.x86_64                                                              5/5 
  Verifying  : mysql-server-5.1.73-7.el6.x86_64                                                              1/5 
  Verifying  : mysql-5.1.73-7.el6.x86_64                                                                     2/5 
  Verifying  : mysql-libs-5.1.73-7.el6.x86_64                                                                3/5 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64                                                             4/5 
  Verifying  : perl-DBI-1.609-4.el6.x86_64                                                                   5/5 

Installed:
  mysql.x86_64 0:5.1.73-7.el6                         mysql-server.x86_64 0:5.1.73-7.el6                        

Dependency Installed:
  mysql-libs.x86_64 0:5.1.73-7.el6     perl-DBD-MySQL.x86_64 0:4.013-3.el6     perl-DBI.x86_64 0:1.609-4.el6    

Complete!

我们发现,通过yum方式安装mysql数据库省去了很多没必要的麻烦,当出现上面的结果时,就代表mysql数据库安装成功了!

此时我们可以通过如下命令,查看刚安装好的mysql-server的版本。

[root@localhost ~]# rpm -qa | grep mysql
mysql-libs-5.1.73-7.el6.x86_64
mysql-server-5.1.73-7.el6.x86_64
mysql-5.1.73-7.el6.x86_64

 

[root@localhost ~]# rpm -qi mysql-serverrpm -qi mysql-server
package mysql-serverrpm is not installed
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 7.el6                         Build Date: Wed May 11 02:31:38 2016
Install Date: Sun Sep 18 23:04:12 2016         Build Host: worker1.bsys.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-7.el6.src.rpm
Size        : 25883075                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Thu May 12 06:49:52 2016, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 7.el6                         Build Date: Wed May 11 02:31:38 2016
Install Date: Sun Sep 18 23:04:12 2016         Build Host: worker1.bsys.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-7.el6.src.rpm
Size        : 25883075                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Thu May 12 06:49:52 2016, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.

  

我们安装的mysql-server并不是最新版本,如果你想尝试最新版本,那就去mysql官网下载rpm包安装就行了,至此我们的mysql数据库已经安装完成了。

3.mysql数据库的初始化及相关配置

我们在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通过输入 service mysqld start 命令就可以启动我们的mysql服务。

注意:如果我们是第一次启动mysql服务,mysql服务器首先会进行初始化的配置,如:

[root@localhost ~]# service mysqld start
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'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

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 /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

[ OK ]
Starting mysqld: [ OK ]

这时我们会看到第一次启动mysql服务器以后会提示非常多的信息,目的就是对mysql数据库进行初始化操作,当我们再次重新启动mysql服务时,就不会提示这么多信息了,如:

[root@localhost ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

  我们在使用mysql数据库时,都得首先启动mysqld服务,我们可以 通过  chkconfig --list | grep mysqld 命令来查看mysql服务是不是开机自动启动,如:

[root@localhost ~]# chkconfig --list | grep mysqld
mysqld         	0:off	1:off	2:off	3:off	4:off	5:off	6:off

  我们发现mysqld服务并没有开机自动启动,我们当然可以通过 chkconfig mysqld on 命令来将其设置成开机启动,这样就不用每次都去手动启动了

[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig --list | grep mysql
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

  mysql数据库安装完以后只会有一个root管理员账号,但是此时的root账号还并没有为其设置密码,在第一次启动mysql服务时,会进行数据库的一些初始化工作,在输出的一大串信息中,我们看到有这样一行信息 :

/usr/bin/mysqladmin -u root password 'new-password'  // 为root账号设置密码
所以我们可以通过 该命令来给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号)
[root@localhost ~]# mysqladmin -u root password 'abc123@@'

  此时我们就可以通过 mysql -u root -p 命令来登录我们的mysql数据库了.

4.mysql数据库的主要配置文件

1./etc/my.cnf 这是mysql的主配置文件

我们可以查看一下这个文件的一些信息

[root@larry etc]# ls my.cnf 
my.cnf

[root@larry etc]# cat 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

  

2./var/lib/mysql   mysql数据库的数据库文件存放位置

我们的mysql数据库的数据库文件通常是存放在了/ver/lib/mysql这个目录下

[root@larry ~]# cd /var/lib/mysql/
[root@larry mysql]# ls -l
总用量 20488
-rw-rw----. 1 mysql mysql 10485760 4月   6 22:01 ibdata1
-rw-rw----. 1 mysql mysql  5242880 4月   6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 4月   6 21:59 ib_logfile1
drwx------. 2 mysql mysql     4096 4月   6 21:59 mysql  // 这两个是mysql数据库安装时默认的两个数据库文件
srwxrwxrwx. 1 mysql mysql        0 4月   6 22:01 mysql.sock
drwx------. 2 mysql mysql     4096 4月   6 21:59 test  // 这两个是mysql数据库安装时默认的两个数据库文件

 我们可以自己创建一个数据库,来验证一下该数据库文件的存放位置 

 

创建一个我们自己的数据库:
mysql> create database xiaoluo;
Query OK, 1 row affected (0.00 sec)

[root@xiaoluo mysql]# ls -l
总用量 20492
-rw-rw----. 1 mysql mysql 10485760 4月   6 22:01 ibdata1
-rw-rw----. 1 mysql mysql  5242880 4月   6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 4月   6 21:59 ib_logfile1
drwx------. 2 mysql mysql     4096 4月   6 21:59 mysql
srwxrwxrwx. 1 mysql mysql        0 4月   6 22:01 mysql.sock
drwx------. 2 mysql mysql     4096 4月   6 21:59 test
drwx------. 2 mysql mysql     4096 4月   6 22:15 xiaoluo  // 这个就是我们刚自己创建的xiaoluo数据库
[root@xiaoluo mysql]# cd xiaoluo/
[root@xiaoluo xiaoluo]# ls
db.opt

  

3./var/log mysql数据库的日志输出存放位置

我们的mysql数据库的一些日志输出存放位置都是在/var/log这个目录下

[root@localhost mysql]# cd /var/log
[root@localhost log]# ll
total 32
-rw------- 1 root  utmp    1152 Sep 18 04:48 btmp
-rw-r--r-- 1 root  root       0 Sep 18 05:01 dmesg
-rw-r--r-- 1 root  root       0 Sep 18 02:58 dmesg.old
-rw-r--r-- 1 root  root  146000 Sep 18 23:04 lastlog
-rw------- 1 root  root       0 Jan  8  2015 maillog
-rw------- 1 root  root       0 Jan  8  2015 messages
-rw-r----- 1 mysql mysql   2159 Sep 18 23:06 mysqld.log
drwx------ 2 root  root    4096 Mar 16  2015 ppp
-rw------- 1 root  root       0 Jan  8  2015 secure
-rw------- 1 root  root       0 Jan  8  2015 spooler
-rw------- 1 root  root       0 Jan  8  2015 tallylog
-rw-rw-r-- 1 root  utmp    4992 Sep 18 23:01 wtmp
-rw------- 1 root  root    3370 Sep 18 23:04 yum.log
[root@localhost log]# cat mysqld.log
160918 23:05:58 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
160918 23:05:58  InnoDB: Initializing buffer pool, size = 8.0M
160918 23:05:58  InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
160918 23:05:58  InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
160918 23:05:58  InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
160918 23:05:58  InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
160918 23:05:58  InnoDB: Started; log sequence number 0 0
160918 23:05:58 [Note] Event Scheduler: Loaded 0 events
160918 23:05:58 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.73'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution
160918 23:06:41 [Note] /usr/libexec/mysqld: Normal shutdown

160918 23:06:41 [Note] Event Scheduler: Purging the queue. 0 events
160918 23:06:41  InnoDB: Starting shutdown...
160918 23:06:43  InnoDB: Shutdown completed; log sequence number 0 44233
160918 23:06:43 [Note] /usr/libexec/mysqld: Shutdown complete

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

  其中mysqld.log 这个文件就是我们存放我们跟mysql数据库进行操作而产生的一些日志信息,通过查看该日志文件,我们可以从中获得很多信息,因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat -anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:

[root@localhost log]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:1723                0.0.0.0:*                   LISTEN      487/pptpd           
tcp        0      0 0.0.0.0:26345               0.0.0.0:*                   LISTEN      479/sshd            
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      952/mysqld          
tcp        0     64 144.168.58.243:26345        59.42.126.21:46263          ESTABLISHED 550/sshd            
tcp        0      0 :::26345                    :::*                        LISTEN      479/sshd            
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     2582487859 952/mysqld          /var/lib/mysql/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     2556875484 1/init              @/com/ubuntu/upstart
unix  2      [ ]         DGRAM                    2556875746 130/udevd           @/org/kernel/udev/udevd
unix  3      [ ]         DGRAM                    2556875750 130/udevd           
unix  3      [ ]         DGRAM                    2556875749 130/udevd           

结果如上所示,Linux系统监听的3306端口号就是我们的mysql数据库!!!!

本篇随笔详细记录了在CentOS6.4下通过yum安装mysql数据库以及对数据库进行基本配置,在后续的Linux学习中将会坚持记录自己的心得以及体会!!!!

OVER@@@