1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux学习之centos(三):mysql数据库的安装和配置

linux学习之centos(三):mysql数据库的安装和配置

时间:2020-01-27 14:53:18

相关推荐

linux学习之centos(三):mysql数据库的安装和配置

前言:mysql简介

说到数据库,我们大多想到的是关系型数据库,比如mysql、oracle、sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱不得不首先推荐的是mysql数据库了,而且Mysql数据库的第一个版本就是发行在Linux系统上的。

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL的SQL语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策(本词条“授权政策”),它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库。由于其社区版的性能卓越,搭配PHP和Apache可组成良好的开发环境。

在Linux上安装mysql数据库,我们可以去其官网上下载mysql数据库的rpm包,/downloads/mysql/5.6.html#downloads,大家可以根据自己的操作系统去下载对应的数据库文件

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

一、卸载系统原有mysql

因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面

切换到root用户下操作:

[carsonzhu@localhost ~]$ su

查看该操作系统是否已经安装了mysql数据库:

[root@localhost carsonzhu]# rpm -qa | grep mysql

mysql-libs-5.1.71-1.el6.x86_64

然后我用下面的方式试图删除mysql(遇到了问题):

[root@localhost carsonzhu]# rpm -e mysql-libs-5.1.71-1.el6.x86_64error: Failed dependencies:libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64mysql-libs is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64[root@localhost carsonzhu]# rpm -e mysql-libs-5.1.71-1.el6.x86_64.rpm --nodepserror: package mysql-libs-5.1.71-1.el6.x86_64.rpm is not installed

说是卸载的时候有依赖包,需要先把postfix-2:2.6.6-2.2.el6_1.x86_64卸载掉,rpm -e postfix-2:2.6.6-2.2.el6_1.x86_64也是不行!然后我搜索解决方法(CentOS 6.3 64位下彻底卸载mysql5.5.25图解教程)有:

如果存在CentOS自带mysql-libs-5.1.71-1.el6.x86_64使用下面的命令卸载即可

[root@localhost carsonzhu]# rpm -ev --nodeps mysql-libs-5.1.71-1.el6.x86_64

[root@localhost carsonzhu]# rpm -qa|grep -i mysql //查看mysql是否已经卸载成功

发现什么都没有了

[root@localhost carsonzhu]# find / -name mysql //查找之前老版本mysql的目录

发现也没有内容

(注:假设查找结果如下:

[root@localhost ~]# find / -name mysql

# /var/lib/mysql

# /var/lib/mysql/mysql

# /usr/lib64/mysql

删除对应的mysql目录

# rm -rf /var/lib/mysql

# rm -rf /var/lib/mysql

# rm -rf /usr/lib64/mysql

这里卸载后/etc/f不会删除,需要进行手工删除

# rm -rf /etc/f)

二、通过yum来安装mysql

查看yum上提供下载的mysql的版本信息:

[root@localhost carsonzhu]# yum list | grep mysqlapr-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-19.el6_7.2updatesfreeradius-mysql.x86_642.2.6-6.el6_7 updateslibdbi-dbd-mysql.x86_640.8.3-5.1.el6 base mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1base mysql.x86_64 5.1.73-5.el6_7.1 updatesmysql-bench.x86_64 5.1.73-5.el6_7.1 updatesmysql-connector-java.noarch 1:5.1.17-6.el6base mysql-connector-odbc.x86_64 5.1.5r1144-7.el6 base mysql-devel.i686 5.1.73-5.el6_7.1 updatesmysql-devel.x86_64 5.1.73-5.el6_7.1 updatesmysql-embedded.i6865.1.73-5.el6_7.1 updatesmysql-embedded.x86_64 5.1.73-5.el6_7.1 updatesmysql-embedded-devel.i686 5.1.73-5.el6_7.1 updatesmysql-embedded-devel.x86_64 5.1.73-5.el6_7.1 updatesmysql-libs.i686 5.1.73-5.el6_7.1 updatesmysql-libs.x86_64 5.1.73-5.el6_7.1 updatesmysql-server.x86_645.1.73-5.el6_7.1 updatesmysql-test.x86_64 5.1.73-5.el6_7.1 updatesphp-mysql.x86_64 5.3.3-46.el6_7.1 updatesqt-mysql.i6861: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-3.el6_7.1 updates

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

等待一番时间后,yum会帮我们选择好安装mysql数据库所需要的软件以及其它附属的一些软件

[root@localhost carsonzhu]# yum install -y mysql-server mysql mysql-deveLoaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfile* base: mirrors.* extras: * updates: base | 3.7 kB00:00extras | 3.4 kB00:00updates| 3.4 kB00:00Setting up Install ProcessNo package mysql-deve available.Resolving Dependencies--> Running transaction check---> Package mysql.x86_64 0:5.1.73-5.el6_7.1 will be installed--> Processing Dependency: mysql-libs = 5.1.73-5.el6_7.1 for package: mysql-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: libmysqlclient_r.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: libmysqlclient_r.so.16()(64bit) for package: mysql-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: mysql-5.1.73-5.el6_7.1.x86_64---> Package mysql-server.x86_64 0:5.1.73-5.el6_7.1 will be installed--> Processing Dependency: perl-DBI for package: mysql-server-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.73-5.el6_7.1.x86_64--> Processing Dependency: perl(DBI) for package: mysql-server-5.1.73-5.el6_7.1.x86_64--> Running transaction check---> Package mysql-libs.x86_64 0:5.1.73-5.el6_7.1 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 ResolutionDependencies Resolved================================================================================Package Arch Version RepositorySize================================================================================Installing:mysqlx86_64 5.1.73-5.el6_7.1 updates 894 kmysql-server x86_64 5.1.73-5.el6_7.1 updates 8.6 MInstalling for dependencies:mysql-libs x86_64 5.1.73-5.el6_7.1 updates 1.2 Mperl-DBD-MySQL x86_64 4.013-3.el6 base134 kperl-DBI x86_64 1.609-4.el6 base705 kTransaction Summary================================================================================Install 5 Package(s)Total download size: 12 MInstalled size: 33 MDownloading Packages:(1/5): mysql-5.1.73-5.el6_7.1.x86_64.rpm | 894 kB00:00(2/5): mysql-libs-5.1.73-5.el6_7.1.x86_64.rpm | 1.2 MB00:01(3/5): mysql-server-5.1.73-5.el6_7.1.x86_64.rpm| 8.6 MB00:08(4/5): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm | 134 kB00:00(5/5): perl-DBI-1.609-4.el6.x86_64.rpm | 705 kB00:00--------------------------------------------------------------------------------Total 1.0 MB/s | 12 MB00:11warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEYRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6Importing GPG key 0xC105B9DE:Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@>Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-11272149.x86_64/6.5)From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning TransactionWarning: RPMDB altered outside of yum.** Found 3 pre-existing rpmdb problem(s), 'yum check' output follows:2:postfix-2.6.6-2.2.el6_1.x86_64 has missing requires of libmysqlclient.so.16()(64bit)2:postfix-2.6.6-2.2.el6_1.x86_64 has missing requires of libmysqlclient.so.16(libmysqlclient_16)(64bit)2:postfix-2.6.6-2.2.el6_1.x86_64 has missing requires of mysql-libsInstalling : mysql-libs-5.1.73-5.el6_7.1.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-5.el6_7.1.x86_64 4/5 Installing : mysql-server-5.1.73-5.el6_7.1.x86_645/5 Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 1/5 Verifying : perl-DBI-1.609-4.el6.x86_64 2/5 Verifying : mysql-libs-5.1.73-5.el6_7.1.x86_64 3/5 Verifying : mysql-5.1.73-5.el6_7.1.x86_64 4/5 Verifying : mysql-server-5.1.73-5.el6_7.1.x86_645/5 Installed:mysql.x86_64 0:5.1.73-5.el6_7.1mysql-server.x86_64 0:5.1.73-5.el6_7.1 Dependency Installed:mysql-libs.x86_64 0:5.1.73-5.el6_7.1 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6Complete!

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

[root@localhost carsonzhu]# rpm -qi mysql-server Name : mysql-server Relocations: (not relocatable)Version: 5.1.73 Vendor: CentOSRelease: 5.el6_7.1 Build Date: 03月23日 星期三 02时10分16秒Install Date: 05月22日 星期日 14时37分04秒Build Host: c6b8.bsys.Group : Applications/Databases Source RPM: mysql-5.1.73-5.el6_7.1.src.rpmSize : 25883011License: GPLv2 with exceptionsSignature : RSA/SHA1, 03月23日 星期三 03时36分21秒, Key ID 0946fca2c105b9dePackager : CentOS BuildSystem <>URL : Summary: The MySQL server and related filesDescription :MySQL is a multi-user, multi-threaded SQL database server. MySQL is aclient/server implementation consisting of a server daemon (mysqld)and many different client programs and libraries. This package containsthe MySQL server and some accompanying files and directories.

三、mysql数据库的初始化和相关配置

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

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

[root@localhost carsonzhu]# service mysqld start初始化 MySQL 数据库: Installing MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE 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_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default. This isstrongly 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.plcd /usr/mysql-test ; perl mysql-test-run.plPlease report any problems with the /usr/bin/mysqlbug script![确定]正在启动 mysqld: [确定]

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

[root@localhost carsonzhu]# service mysqld start正在启动 mysqld: [确定]

查看mysql服务是否为开机自动启动:

[root@localhost carsonzhu]# chkconfig --list | grep mysqldmysqld 0:关闭1:关闭2:关闭3:关闭4:关闭5:关闭6:关闭

将其设置为开机启动:

[root@localhost carsonzhu]# chkconfig mysqld on[root@localhost carsonzhu]# chkconfig --list | grep mysqldmysqld 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭

当然,想再次关闭开机启动也很简单:

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

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

/usr/bin/mysqladmin -u root password 'new-password'// 为root账号设置密码

设置mysql数据库的root账号((:这个root账号是mysql的root账号,非Linux的root账号)):

[root@localhost carsonzhu]# mysqladmin -u root password 'carsonzhu'

(将root账号密码设置为carsonzhu)

登录mysql数据库:

[root@localhost carsonzhu]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.1.73 Source distributionCopyright (c) 2000, , Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases ;+--------------------+| Database |+--------------------+| information_schema || mysql || test|+--------------------+3 rows in set (0.02 sec)mysql>

四、mysql数据库的主要配置文件

1、/etc/f是mysql的主配置文件

[root@localhost carsonzhu]# cd[root@localhost ~]# lsanaconda-ks.cfg install.log install.log.syslog[root@localhost ~]# cd /home[root@localhost home]# lscarsonzhu[root@localhost home]# find / -name etc/usr/local/etc/usr/lib/vmware-tools/lib32/libconf/etc/usr/lib/vmware-tools/lib64/libconf/etc/usr/etc/usr/share/doc/oddjob-0.30/sample/etc/usr/share/festival/lib/etc/usr/lib64/festival/etc/tmp/vmware-tools-distrib/lib/lib32/libconf/etc/tmp/vmware-tools-distrib/lib/lib64/libconf/etc/tmp/vmware-tools-distrib/caf/etc/tmp/vmware-tools-distrib/etc/etc[root@localhost home]# cd /[root@localhost /]# lsbin dev lib media net rootsr0 tmp varboot etc lib64 misc opt sbinsrv Toolscdrom home lost+found mnt proc selinux sys usr[root@localhost /]# cd etc

[root@localhost /]# lsbin dev lib media net rootsr0 tmp varboot etc lib64 misc opt sbinsrv Toolscdrom home lost+found mnt proc selinux sys usr[root@localhost /]# cd etc[root@localhost etc]# lsabrt hp printcapacpi httpdprofileadjtime idmapd.conf profile.daliases initprotocolsaliases.dbinit.d pulsealsa inittab purplealternatives inputrc quotagrpadminsanacrontabipa quotatabanthy-confiproute2 rcasound.conf issuerc0.dat.deny rc1.daudisp javarc2.dauditjvm rc3.dautofs_ldap_auth.conf jvm-commmon rc4.dauto.master kde rc5.dauto.misckdump-adv-conf kdump.confrc.dauto.smb krb5.conf rc.localavahilatrace.conf rc.sysinitbash_completion.d latrace.d readahead.confbashrc ld.so.cache redhat-lsbblkidld.so.confredhat-releasebluetoothld.so.conf.d request-key.confbonobo-activation libaudit.conf request-key.dcas.conf libreport resolv.confcentos-release libuser.conf rpccertmongerlocaltime rpmchkconfig.d login.defsrsyslog.confConsoleKitlogrotate.confrsyslog.dcron.d logrotate.d rwtabcron.dailylsb-release rwtab.dcron.denylsb-release.d sambacron.hourly ltrace.conf sane.dcron.monthly lvm sasl2crontab magicsclcron.weekly mailcap securettycrypttab mail.rc securitycsh.cshrcmakedev.d selinuxcsh.loginman.configservicescups mavensestatus.confdbus-1 mime.typessetuptool.ddefault mke2fs.conf sgmldepmod.d modprobe.dshadowdhcp motdshadow-DIR_COLORSmtabshellsDIR_COLORS.256colormtools.conf skelDIR_COLORS.lightbgcolor f smartd.confdnsmasq.conf nanorc snmpdnsmasq.dnetconfig sos.confdracut.conf NetworkManagersounddracut.conf.d networks sshdrircnfsmount.conf sslenscript.cfg nsswitch.conf sssdenvironment ntp statetabethers ntp.conf statetab.devent.d obex-data-server sudo.confexports oddjob sudoersfavicon.png oddjobd.conf sudoers.dfestival oddjobd.conf.dsudo-ldap.conffilesystems openldap sysconfigfontsopt sysctl.conffoomatic PackageKitsystem-releasefprintd.conf pam.dsystem-release-cpefstabpangoterminfogai.conf passwd tpvmlp.confgconfpasswd- Trolltech.confgcrypt pbm2ppa.conf udevgdm pcmcia updatedb.confghostscript pinforc vimrcgnome-vfs-2.0 pkcs11 vircgnupgpki vmware-cafgroupplymouth vmware-toolsgroup- pm warnquota.confgrub.confpm-utils-hd-apm-restore.conf wgetrcgshadow pnm2ppa.conf wpa_supplicantgshadow- polkit-1 X11gssapi_mech.conf popt.d xdggtk-2.0 portreserve xinetd.dhal postfix xmlhost.confppp yp.confhostsprelink.cache yumhosts.allow prelink.conf yum.confhosts.denyprelink.conf.dyum.repos.d

[root@localhost etc]# ls f[root@localhost etc]# cat f[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid

2、/var/lib/mysqlmysql数据库的数据库文件存放位置

[root@localhost etc]# cd /var[root@localhost var]# cd lib/mysql[root@localhost mysql]# ls -1ibdata1ib_logfile0ib_logfile1mysql //mysql数据库安装时默认的两个数据库文件mysql.socktest //mysql数据库安装时默认的两个数据库文件

自己创建一个数据库,并验证该数据库文件的存放位置:

[root@localhost mysql]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.1.73 Source distributionCopyright (c) 2000, , Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>create database carsontest ;Query OK, 1 row affected (0.01 sec)mysql> exitBye[root@localhost mysql]# ls -1carsontestibdata1ib_logfile0ib_logfile1mysqlmysql.socktest[root@localhost mysql]# cd carsontest[root@localhost carsontest]# lsdb.opt

3、/var/logmysql数据库的日志输出存放位置

[root@localhost carsontest]# cd[root@localhost ~]# cd /var/log[root@localhost log]# lsanaconda.ifcfg.log gdmsecure-0517anaconda.loghttpd secure-0522anaconda.program.log lastlog spice-vdagent.loganaconda.storage.log maillog spooleranaconda.syslog maillog-0305 spooler-0305anaconda.xlog maillog-0517 spooler-0517anaconda.yum.logmaillog-0522 spooler-0522audit messages sssdboot.log messages-0305 tallylogbtmp messages-0517 vmware-cafbtmp-0517 messages-0522 vmware-install.logConsoleKit mysqld.log vmware-vmsvc.logcron ntpstats wpa_supplicant.logcron-0305 pm-powersave.log wtmpcron-0517 pppXorg.0.logcron-0522 prelink Xorg.0.log.oldcups sa Xorg.9.logdmesg samba yum.logdmesg.old securedracut.log secure-0305

因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过netstat -anp命令来查看一下,Linux系统是否在监听 3306 这个端口号:

[root@localhost log]# netstat -anp | moreActive Internet connections (servers and established)Proto Recv-Q Send-Q Local AddressForeign Address State PID/Program name tcp 00 0.0.0.0:380290.0.0.0:* LISTEN1788/rpc.statdtcp 00 0.0.0.0:111 0.0.0.0:* LISTEN1742/rpcbind tcp 00 0.0.0.0:22 0.0.0.0:* LISTEN1961/sshd tcp 00 127.0.0.1:6310.0.0.0:* LISTEN1845/cupsdtcp 00 127.0.0.1:250.0.0.0:* LISTEN2131/master tcp 00 0.0.0.0:33060.0.0.0:* LISTEN20295/mysqld tcp 10 192.168.1.20:5389261.213.168.17:80 CLOSE_WAIT 2723/clock-applet tcp 10 192.168.1.20:5389161.213.168.17:80 CLOSE_WAIT 2723/clock-applet tcp 00 :::40843:::* LISTEN1788/rpc.statdtcp 00 :::111 :::* LISTEN1742/rpcbind

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

可参考文章:Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

另外一种安装方式:CentOS7安装mysql5.6.26

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。