前言:mysql简介
说到数据库,我们大多想到的是关系型数据库,比如mysql、oracle、sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱不得不首先推荐的是mysql数据库了,而且Mysql数据库的第一个版本就是发行在Linux系统上的。
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL的SQL语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策(本词条“授权政策”),它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库。由于其社区版的性能卓越,搭配PHP和Apache可组成良好的开发环境。
在Linux上安装mysql数据库,我们可以去其官网上下载mysql数据库的rpm包,http://dev.mysql.com/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_64 error: Failed dependencies: libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64 libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64 mysql-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 --nodeps error: 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/my.cnf不会删除,需要进行手工删除
# rm -rf /etc/my.cnf)
二、通过yum来安装mysql
查看yum上提供下载的mysql的版本信息:
[root@localhost carsonzhu]# 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-19.el6_7.2 updates freeradius-mysql.x86_64 2.2.6-6.el6_7 updates 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-5.el6_7.1 updates mysql-bench.x86_64 5.1.73-5.el6_7.1 updates 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-5.el6_7.1 updates mysql-devel.x86_64 5.1.73-5.el6_7.1 updates mysql-embedded.i686 5.1.73-5.el6_7.1 updates mysql-embedded.x86_64 5.1.73-5.el6_7.1 updates mysql-embedded-devel.i686 5.1.73-5.el6_7.1 updates mysql-embedded-devel.x86_64 5.1.73-5.el6_7.1 updates mysql-libs.i686 5.1.73-5.el6_7.1 updates mysql-libs.x86_64 5.1.73-5.el6_7.1 updates mysql-server.x86_64 5.1.73-5.el6_7.1 updates mysql-test.x86_64 5.1.73-5.el6_7.1 updates php-mysql.x86_64 5.3.3-46.el6_7.1 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-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-deve Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * extras: mirrors.skyshe.cn * updates: mirrors.skyshe.cn base | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Setting up Install Process No 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 Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql x86_64 5.1.73-5.el6_7.1 updates 894 k mysql-server x86_64 5.1.73-5.el6_7.1 updates 8.6 M Installing for dependencies: mysql-libs x86_64 5.1.73-5.el6_7.1 updates 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-5.el6_7.1.x86_64.rpm | 894 kB 00:00 (2/5): mysql-libs-5.1.73-5.el6_7.1.x86_64.rpm | 1.2 MB 00:01 (3/5): mysql-server-5.1.73-5.el6_7.1.x86_64.rpm | 8.6 MB 00:08 (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 1.0 MB/s | 12 MB 00:11 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Importing GPG key 0xC105B9DE: Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org> Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: 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-libs Installing : 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_64 5/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_64 5/5 Installed: mysql.x86_64 0:5.1.73-5.el6_7.1 mysql-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.el6 Complete!
此时,我们通过如下命令查看刚安装好的mysql-server的版本:
[root@localhost carsonzhu]# rpm -qi mysql-server Name : mysql-server Relocations: (not relocatable) Version : 5.1.73 Vendor: CentOS Release : 5.el6_7.1 Build Date: 2016年03月23日 星期三 02时10分16秒 Install Date: 2016年05月22日 星期日 14时37分04秒 Build Host: c6b8.bsys.dev.centos.org Group : Applications/Databases Source RPM: mysql-5.1.73-5.el6_7.1.src.rpm Size : 25883011 License: GPLv2 with exceptions Signature : RSA/SHA1, 2016年03月23日 星期三 03时36分21秒, 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数据库的初始化和相关配置
我们在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通过输入 service mysqld start 命令就可以启动我们的mysql服务。
注:如果是第一次启动mysql服务,mysql服务器首先会进行初始化的配置,如:
[root@localhost carsonzhu]# service mysqld start 初始化 MySQL 数据库: 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! [确定] 正在启动 mysqld: [确定]
我们会看到第一次启动mysql服务器以后会提示非常多的信息,目的就是对mysql数据库进行初始化操作,再次重启mysql服务时,就不会提示那么多信息了:
[root@localhost carsonzhu]# service mysqld start 正在启动 mysqld: [确定]
查看mysql服务是否为开机自动启动:
[root@localhost carsonzhu]# chkconfig --list | grep mysqld mysqld 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
将其设置为开机启动:
[root@localhost carsonzhu]# chkconfig mysqld on [root@localhost carsonzhu]# chkconfig --list | grep mysqld mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
当然,想再次关闭开机启动也很简单:
[root@localhost carsonzhu]# chkconfig mysqld off [root@localhost carsonzhu]# chkconfig --list | grep mysqld mysqld 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 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.1.73 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> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.02 sec) mysql>
四、mysql数据库的主要配置文件
1、/etc/my.cnf 是mysql的主配置文件
[root@localhost carsonzhu]# cd [root@localhost ~]# ls anaconda-ks.cfg install.log install.log.syslog [root@localhost ~]# cd /home [root@localhost home]# ls carsonzhu [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 /]# ls bin dev lib media net root sr0 tmp var boot etc lib64 misc opt sbin srv Tools cdrom home lost+found mnt proc selinux sys usr [root@localhost /]# cd etc
[root@localhost /]# ls bin dev lib media net root sr0 tmp var boot etc lib64 misc opt sbin srv Tools cdrom home lost+found mnt proc selinux sys usr [root@localhost /]# cd etc [root@localhost etc]# ls abrt hp printcap acpi httpd profile adjtime idmapd.conf profile.d aliases init protocols aliases.db init.d pulse alsa inittab purple alternatives inputrc quotagrpadmins anacrontab ipa quotatab anthy-conf iproute2 rc asound.conf issue rc0.d at.deny issue.net rc1.d audisp java rc2.d audit jvm rc3.d autofs_ldap_auth.conf jvm-commmon rc4.d auto.master kde rc5.d auto.misc kdump-adv-conf rc6.d auto.net kdump.conf rc.d auto.smb krb5.conf rc.local avahi latrace.conf rc.sysinit bash_completion.d latrace.d readahead.conf bashrc ld.so.cache redhat-lsb blkid ld.so.conf redhat-release bluetooth ld.so.conf.d request-key.conf bonobo-activation libaudit.conf request-key.d cas.conf libreport resolv.conf centos-release libuser.conf rpc certmonger localtime rpm chkconfig.d login.defs rsyslog.conf ConsoleKit logrotate.conf rsyslog.d cron.d logrotate.d rwtab cron.daily lsb-release rwtab.d cron.deny lsb-release.d samba cron.hourly ltrace.conf sane.d cron.monthly lvm sasl2 crontab magic scl cron.weekly mailcap securetty crypttab mail.rc security csh.cshrc makedev.d selinux csh.login man.config services cups maven sestatus.conf dbus-1 mime.types setuptool.d default mke2fs.conf sgml depmod.d modprobe.d shadow dhcp motd shadow- DIR_COLORS mtab shells DIR_COLORS.256color mtools.conf skel DIR_COLORS.lightbgcolor my.cnf smartd.conf dnsmasq.conf nanorc snmp dnsmasq.d netconfig sos.conf dracut.conf NetworkManager sound dracut.conf.d networks ssh drirc nfsmount.conf ssl enscript.cfg nsswitch.conf sssd environment ntp statetab ethers ntp.conf statetab.d event.d obex-data-server sudo.conf exports oddjob sudoers favicon.png oddjobd.conf sudoers.d festival oddjobd.conf.d sudo-ldap.conf filesystems openldap sysconfig fonts opt sysctl.conf foomatic PackageKit system-release fprintd.conf pam.d system-release-cpe fstab pango terminfo gai.conf passwd tpvmlp.conf gconf passwd- Trolltech.conf gcrypt pbm2ppa.conf udev gdm pcmcia updatedb.conf ghostscript pinforc vimrc gnome-vfs-2.0 pkcs11 virc gnupg pki vmware-caf group plymouth vmware-tools group- pm warnquota.conf grub.conf pm-utils-hd-apm-restore.conf wgetrc gshadow pnm2ppa.conf wpa_supplicant gshadow- polkit-1 X11 gssapi_mech.conf popt.d xdg gtk-2.0 portreserve xinetd.d hal postfix xml host.conf ppp yp.conf hosts prelink.cache yum hosts.allow prelink.conf yum.conf hosts.deny prelink.conf.d yum.repos.d
[root@localhost etc]# ls my.cnf my.cnf [root@localhost 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数据库的数据库文件存放位置
[root@localhost etc]# cd /var [root@localhost var]# cd lib/mysql [root@localhost mysql]# ls -1 ibdata1 ib_logfile0 ib_logfile1 mysql //mysql数据库安装时默认的两个数据库文件 mysql.sock test //mysql数据库安装时默认的两个数据库文件
自己创建一个数据库,并验证该数据库文件的存放位置:
[root@localhost mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.73 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> create database carsontest ; Query OK, 1 row affected (0.01 sec) mysql> exit Bye [root@localhost mysql]# ls -1 carsontest ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test [root@localhost mysql]# cd carsontest [root@localhost carsontest]# ls db.opt
3、/var/log mysql数据库的日志输出存放位置
[root@localhost carsontest]# cd [root@localhost ~]# cd /var/log [root@localhost log]# ls anaconda.ifcfg.log gdm secure-20160517 anaconda.log httpd secure-20160522 anaconda.program.log lastlog spice-vdagent.log anaconda.storage.log maillog spooler anaconda.syslog maillog-20160305 spooler-20160305 anaconda.xlog maillog-20160517 spooler-20160517 anaconda.yum.log maillog-20160522 spooler-20160522 audit messages sssd boot.log messages-20160305 tallylog btmp messages-20160517 vmware-caf btmp-20160517 messages-20160522 vmware-install.log ConsoleKit mysqld.log vmware-vmsvc.log cron ntpstats wpa_supplicant.log cron-20160305 pm-powersave.log wtmp cron-20160517 ppp Xorg.0.log cron-20160522 prelink Xorg.0.log.old cups sa Xorg.9.log dmesg samba yum.log dmesg.old secure dracut.log secure-20160305
因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat -anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:
[root@localhost log]# netstat -anp | more 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:38029 0.0.0.0:* LISTEN 1788/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1742/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1961/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1845/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2131/master tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 20295/mysqld tcp 1 0 192.168.1.20:53892 61.213.168.17:80 CLOSE_WAIT 2723/clock-applet tcp 1 0 192.168.1.20:53891 61.213.168.17:80 CLOSE_WAIT 2723/clock-applet tcp 0 0 :::40843 :::* LISTEN 1788/rpc.statd tcp 0 0 :::111 :::* LISTEN 1742/rpcbind
结果如上所示,Linux系统监听的3306端口号就是我们的mysql数据库!
可参考文章:Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
另外一种安装方式:CentOS7安装mysql5.6.26