在阿里云的CentOS环境中安装配置MySQL的教程
1常规错误的yum安装方法:
在前文中记述了CentOS6.5系统中通过yum方式快速地搭建了LNMP环境,那么是否也能在CentOS7或CentOS7.1系统中依葫芦画瓢安装MySql5.6.24呢?答案是否定的。
[root@typecodes~]#yum-yinstallmysqlmysql-servermysql-devel .....................。 ....省略部分安装过程.... .....................。 Installed: mariadb.x86_641:5.5.41-2.el7_0mariadb-devel.x86_641:5.5.41-2.el7_0 DependencyInstalled: keyutils-libs-devel.x86_640:1.5.8-3.el7krb5-devel.x86_640:1.12.2-14.el7libcom_err-devel.x86_640:1.42.9-7.el7libselinux-devel.x86_640:2.2.2-6.el7 libsepol-devel.x86_640:2.1.9-3.el7libverto-devel.x86_640:0.2.5-4.el7openssl-devel.x86_641:1.0.1e-42.el7.4pcre-devel.x86_640:8.32-14.el7 zlib-devel.x86_640:1.2.7-13.el7 Complete!
从上面的安装结果可以看出,没有安装mysql数据库,而是安装了mariadb数据!因为在CentOS7和CentOS7.1系统中,默认安装的mysql是它的分支mariadb。这里引用下百度百科关于mariadb的描述:
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
因此,下面这些正常的MySQL操作都是无效的:
#######无法把mysql服务加入系统启动 [root@typecodes~]#chkconfigmysqldon errorreadinginformationonservicemysqld:Nosuchfileordirectory #######启动不了mysql [root@typecodes~]#servicemysqldstart Redirectingto/bin/systemctlstartmysqld.service Failedtoissuemethodcall:Unitmysqld.servicefailedtoload:Nosuchfileordirectory. #######没有安装mysql相关包 [root@typecodes~]#rpm-qa|grepmysql php-mysql-5.4.16-23.el7_0.3.x86_64 [root@typecodes~]#
2正确的安装方法:
众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本。所以我们需要先安装带有当前可用的mysql5系列社区版资源的rpm包。
#######安装rpm包 [root@typecodes~]#rpm-Uvhhttp://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Retrievinghttp://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm Preparing...#################################[100%] Updating/installing... 1:mysql-community-release-el7-5#################################[100%]
这时查看当前可用的mysql安装资源:
[root@typecodes~]#yumrepolistenabled|grep"mysql.*-community.*"
mysql-connectors-community/x86_64MySQLConnectorsCommunity14 mysql-tools-community/x86_64MySQLToolsCommunity17 mysql56-community/x86_64MySQL5.6CommunityServer139
从上面的列表可以看出,mysql56-community/x86_64和MySQL5.6CommunityServer可以使用。
因此,我们就可以直接用yum方式安装了MySQL5.6版本了。
[root@typecodes~]#yum-yinstallmysql-community-server
Loadedplugins:axelget,langpacks Nometadataavailableforbase Nometadataavailableforepel Nometadataavailableforextras (Failedtoloadmodulefastestmirror:Nomodulenamedfastestmirror) repomd.xml|2.5kB00:00:00 updatemysql-connectors-communitymetadatasuccessfully (Failedtoloadmodulefastestmirror:Nomodulenamedfastestmirror) repomd.xml|2.5kB00:00:00 updatemysql-tools-communitymetadatasuccessfully (Failedtoloadmodulefastestmirror:Nomodulenamedfastestmirror) repomd.xml|2.5kB00:00:00 updatemysql56-communitymetadatasuccessfully Nometadataavailableforupdates mysql-connectors-community|2.5kB00:00:00 mysql-tools-community|2.5kB00:00:00 mysql56-community|2.5kB00:00:00 (1/3):mysql-connectors-community/x86_64/primary_db|7.3kB00:00:00 (2/3):mysql56-community/x86_64/primary_db|83kB00:00:01 mysql-tools-community/x86_64/pFAILED==============================================]19kB/s|92kB00:00:00ETA http://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/repodata/3301d2eb425f300dec5a46c4201d08f2cf9232a6-primary.sqlite.bz2:[Errno12]Timeoutonhttp://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/repodata/3301d2eb425f300dec5a46c4201d08f2cf9232a6-primary.sqlite.bz2:(28,'Operationtooslow.Lessthan1000bytes/sectransferredthelast3seconds') Tryingothermirror. (3/3):mysql-tools-community/x86_64/primary_db|15kB00:00:00 ResolvingDependencies -->Runningtransactioncheck --->Packagemysql-community-server.x86_640:5.6.24-3.el7willbeinstalled -->ProcessingDependency:mysql-community-common(x86-64)=5.6.24-3.el7forpackage:mysql-community-server-5.6.24-3.el7.x86_64 -->ProcessingDependency:mysql-community-client(x86-64)=5.6.24-3.el7forpackage:mysql-community-server-5.6.24-3.el7.x86_64 -->ProcessingDependency:perl(DBI)forpackage:mysql-community-server-5.6.24-3.el7.x86_64 -->Runningtransactioncheck --->Packagemariadb.x86_641:5.5.41-2.el7_0willbeobsoleted --->Packagemysql-community-client.x86_640:5.6.24-3.el7willbeobsoleting -->ProcessingDependency:mysql-community-libs(x86-64)=5.6.24-3.el7forpackage:mysql-community-client-5.6.24-3.el7.x86_64 --->Packagemysql-community-common.x86_640:5.6.24-3.el7willbeinstalled --->Packageperl-DBI.x86_640:1.627-4.el7willbeinstalled -->ProcessingDependency:perl(RPC::PlServer)>=0.2001forpackage:perl-DBI-1.627-4.el7.x86_64 -->ProcessingDependency:perl(RPC::PlClient)>=0.2000forpackage:perl-DBI-1.627-4.el7.x86_64 -->Runningtransactioncheck --->Packagemariadb-libs.x86_641:5.5.41-2.el7_0willbeobsoleted -->ProcessingDependency:mariadb-libs(x86-64)=1:5.5.41-2.el7_0forpackage:1:mariadb-devel-5.5.41-2.el7_0.x86_64 --->Packagemysql-community-libs.x86_640:5.6.24-3.el7willbeobsoleting --->Packageperl-PlRPC.noarch0:0.2020-14.el7willbeinstalled -->ProcessingDependency:perl(Net::Daemon)>=0.13forpackage:perl-PlRPC-0.2020-14.el7.noarch -->ProcessingDependency:perl(Net::Daemon::Test)forpackage:perl-PlRPC-0.2020-14.el7.noarch -->ProcessingDependency:perl(Net::Daemon::Log)forpackage:perl-PlRPC-0.2020-14.el7.noarch -->ProcessingDependency:perl(Compress::Zlib)forpackage:perl-PlRPC-0.2020-14.el7.noarch -->Runningtransactioncheck --->Packagemariadb-devel.x86_641:5.5.41-2.el7_0willbeobsoleted --->Packagemysql-community-devel.x86_640:5.6.24-3.el7willbeobsoleting --->Packageperl-IO-Compress.noarch0:2.061-2.el7willbeinstalled -->ProcessingDependency:perl(Compress::Raw::Zlib)>=2.061forpackage:perl-IO-Compress-2.061-2.el7.noarch -->ProcessingDependency:perl(Compress::Raw::Bzip2)>=2.061forpackage:perl-IO-Compress-2.061-2.el7.noarch --->Packageperl-Net-Daemon.noarch0:0.48-5.el7willbeinstalled -->Runningtransactioncheck --->Packageperl-Compress-Raw-Bzip2.x86_640:2.061-3.el7willbeinstalled --->Packageperl-Compress-Raw-Zlib.x86_641:2.061-4.el7willbeinstalled -->FinishedDependencyResolution DependenciesResolved ======================================================================================================================================================================= PackageArchVersionRepositorySize ======================================================================================================================================================================= Installing: mysql-community-clientx86_645.6.24-3.el7mysql56-community19M replacingmariadb.x86_641:5.5.41-2.el7_0 mysql-community-develx86_645.6.24-3.el7mysql56-community3.4M replacingmariadb-devel.x86_641:5.5.41-2.el7_0 mysql-community-libsx86_645.6.24-3.el7mysql56-community2.0M replacingmariadb-libs.x86_641:5.5.41-2.el7_0 mysql-community-serverx86_645.6.24-3.el7mysql56-community58M Installingfordependencies: mysql-community-commonx86_645.6.24-3.el7mysql56-community256k perl-Compress-Raw-Bzip2x86_642.061-3.el7base32k perl-Compress-Raw-Zlibx86_641:2.061-4.el7base57k perl-DBIx86_641.627-4.el7base802k perl-IO-Compressnoarch2.061-2.el7base260k perl-Net-Daemonnoarch0.48-5.el7base51k perl-PlRPCnoarch0.2020-14.el7base36k TransactionSummary ======================================================================================================================================================================= Install4Packages(+7Dependentpackages) Totaldownloadsize:83M Downloadingpackages: mysql-community-devel-5.6.24-3.el7.x86_64.rpm|3.4MB00:00:03 mysql-community-client-5.6.24-3.el7.x86_64.rpm|19MB00:00:05 mysql-community-server-5.6.24-3.el7.x86_64.rpm|58MB00:00:11 mysql-community-libs-5.6.24-3.el7.x86_64.rpm|2.0MB00:00:02 perl-DBI-1.627-4.el7.x86_64.rpm|0B00:00:01... perl-DBI-1.627-4.el7.x86_64.rpm|802kB00:00:00 (1/6):perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm|57kB00:00:00 (2/6):perl-Net-Daemon-0.48-5.el7.noarch.rpm|51kB00:00:00 (3/6):perl-PlRPC-0.2020-14.el7.noarch.rpm|36kB00:00:00 (4/6):perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm|32kB00:00:00 (5/6):perl-IO-Compress-2.061-2.el7.noarch.rpm|260kB00:00:00 warning:/var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.24-3.el7.x86_64.rpm:V3DSA/SHA1Signature,keyID5072e1f5:NOKEY00:00:01ETA Publickeyformysql-community-common-5.6.24-3.el7.x86_64.rpmisnotinstalled (6/6):mysql-community-common-5.6.24-3.el7.x86_64.rpm|256kB00:00:01 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total388kB/s|692kB00:00:01 Retrievingkeyfromfile:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql ImportingGPGkey0x5072E1F5: Userid:"MySQLReleaseEngineering<mysql-build@oss.oracle.com>" Fingerprint:a4a9406876fcbd3c456770c88c718d3b5072e1f5 Package:mysql-community-release-el7-5.noarch(installed) From:file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Runningtransactioncheck Runningtransactiontest Transactiontestsucceeded Runningtransaction Warning:RPMDBalteredoutsideofyum. Installing:mysql-community-common-5.6.24-3.el7.x86_641/14 Installing:mysql-community-libs-5.6.24-3.el7.x86_642/14 Installing:mysql-community-client-5.6.24-3.el7.x86_643/14 Installing:perl-Net-Daemon-0.48-5.el7.noarch4/14 Installing:1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_645/14 Installing:perl-Compress-Raw-Bzip2-2.061-3.el7.x86_646/14 Installing:perl-IO-Compress-2.061-2.el7.noarch7/14 Installing:perl-PlRPC-0.2020-14.el7.noarch8/14 Installing:perl-DBI-1.627-4.el7.x86_649/14 Installing:mysql-community-server-5.6.24-3.el7.x86_6410/14 Installing:mysql-community-devel-5.6.24-3.el7.x86_6411/14 Erasing:1:mariadb-devel-5.5.41-2.el7_0.x86_6412/14 Erasing:1:mariadb-5.5.41-2.el7_0.x86_6413/14 Erasing:1:mariadb-libs-5.5.41-2.el7_0.x86_6414/14 Verifying:perl-Compress-Raw-Bzip2-2.061-3.el7.x86_641/14 Verifying:mysql-community-common-5.6.24-3.el7.x86_642/14 Verifying:mysql-community-devel-5.6.24-3.el7.x86_643/14 Verifying:mysql-community-client-5.6.24-3.el7.x86_644/14 Verifying:perl-PlRPC-0.2020-14.el7.noarch5/14 Verifying:mysql-community-server-5.6.24-3.el7.x86_646/14 Verifying:1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_647/14 Verifying:mysql-community-libs-5.6.24-3.el7.x86_648/14 Verifying:perl-Net-Daemon-0.48-5.el7.noarch9/14 Verifying:perl-DBI-1.627-4.el7.x86_6410/14 Verifying:perl-IO-Compress-2.061-2.el7.noarch11/14 Verifying:1:mariadb-5.5.41-2.el7_0.x86_6412/14 Verifying:1:mariadb-devel-5.5.41-2.el7_0.x86_6413/14 Verifying:1:mariadb-libs-5.5.41-2.el7_0.x86_6414/14 Installed: mysql-community-client.x86_640:5.6.24-3.el7mysql-community-devel.x86_640:5.6.24-3.el7mysql-community-libs.x86_640:5.6.24-3.el7 mysql-community-server.x86_640:5.6.24-3.el7 DependencyInstalled: mysql-community-common.x86_640:5.6.24-3.el7perl-Compress-Raw-Bzip2.x86_640:2.061-3.el7perl-Compress-Raw-Zlib.x86_641:2.061-4.el7perl-DBI.x86_640:1.627-4.el7 perl-IO-Compress.noarch0:2.061-2.el7perl-Net-Daemon.noarch0:0.48-5.el7perl-PlRPC.noarch0:0.2020-14.el7 Replaced: mariadb.x86_641:5.5.41-2.el7_0mariadb-devel.x86_641:5.5.41-2.el7_0mariadb-libs.x86_641:5.5.41-2.el7_0 Complete!
3MySQL安装完成后,进行相关配置
安装完MySQL后,需要进行一些基础配置工作:
#######安装成功后,将其加入开机启动 [root@typecodes~]#systemctlenablemysqld #######启动mysql服务进程 [root@typecodes~]#systemctlstartmysqld #######配置mysql(设置密码等) [root@typecodes~]#mysql_secure_installation
NOTE:RUNNINGALLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMySQL SERVERSINPRODUCTIONUSE!PLEASEREADEACHSTEPCAREFULLY! InordertologintoMySQLtosecureit,we'llneedthecurrent passwordfortherootuser.Ifyou'vejustinstalledMySQL,and youhaven'tsettherootpasswordyet,thepasswordwillbeblank, soyoushouldjustpressenterhere. Entercurrentpasswordforroot(enterfornone): OK,successfullyusedpassword,movingon... SettingtherootpasswordensuresthatnobodycanlogintotheMySQL rootuserwithouttheproperauthorisation.
Setrootpassword?[Y/n]y[设置root用户密码] Newpassword: Re-enternewpassword: Passwordupdatedsuccessfully! Reloadingprivilegetables.. ...Success! Bydefault,aMySQLinstallationhasananonymoususer,allowinganyone tologintoMySQLwithouthavingtohaveauseraccountcreatedfor them.Thisisintendedonlyfortesting,andtomaketheinstallation goabitsmoother.Youshouldremovethembeforemovingintoa productionenvironment. Removeanonymoususers?[Y/n]y[删除匿名用户] ...Success! Normally,rootshouldonlybeallowedtoconnectfrom'localhost'.This ensuresthatsomeonecannotguessattherootpasswordfromthenetwork. Disallowrootloginremotely?[Y/n]y[禁止root远程登录] ...Success! Bydefault,MySQLcomeswithadatabasenamed'test'thatanyonecan access.Thisisalsointendedonlyfortesting,andshouldberemoved beforemovingintoaproductionenvironment. Removetestdatabaseandaccesstoit?[Y/n]y[删除test数据库] -Droppingtestdatabase... ERROR1008(HY000)atline1:Can'tdropdatabase'test';databasedoesn'texist ...Failed!Notcritical,keepmoving... -Removingprivilegesontestdatabase... ...Success! Reloadingtheprivilegetableswillensurethatallchangesmadesofar willtakeeffectimmediately. Reloadprivilegetablesnow?[Y/n]y[刷新权限] ...Success! Alldone!Ifyou'vecompletedalloftheabovesteps,yourMySQL installationshouldnowbesecure. ThanksforusingMySQL! Cleaningup...
4.可能有效的MySQL配置优化
在原始配置文件 /etc/my.cnf基础上,在[mysqld]节内增加配置参数。实际应用中,请按硬件及负载酌情修改。
#addbyfeng120418-------------------------- #skip-locking skip-name-resolve skip-external-locking key_buffer_size=256M #table_cache=3072 table_open_cache=3072 read_buffer_size=2M read_rnd_buffer_size=2M sort_buffer_size=2M myisam_sort_buffer_size=256M thread_cache_size=8 query_cache_size=512M query_cache_limit=5M tmp_table_size=1024M max_heap_table_size=3000M max_allowed_packet=16M innodb_buffer_pool_size=512M innodb_log_file_size=512M innodb_additional_mem_pool_size=512M innodb_log_buffer_size=64M max_connections=2000 max_user_connections=800 join_buffer_size=8M open_files_limit=65535 #tmpdir=/dev/shm max_connect_errors=1000 #addbyfeng120418end---------------------