CentOS 7.0 安装 mysql-5.7.14

前端之家收集整理的这篇文章主要介绍了CentOS 7.0 安装 mysql-5.7.14前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

初学linux 路上遇过各种坑 把正确安装MysqL-5.7.14分享一下

1.CentOs7.0 默认的数据库为MariaDB,先卸载MariaDB,否则安装MysqL,引起冲突

先查看当前安装的MariaDB

rpm-qa|grepmariadb

之后下面就会列出现在的版本 使用

rpm-e--nodepsmariadb........


2.之后 创建文件夹 我是放在/home/MysqL

mkdir/home/MysqL/data


3.创建MysqL用户组与用户

groupaddMysqL
useradd-gMysqL-d/home/MysqLMysqL

PS.如果已经存在提示错误了 可以先删除

userdelMysqL
groupdelMysqL


4.MysqL5.7之前使用

./bin/MysqL_install_db--user=MysqL--basedir=/home/MysqL--datadir=/home/MysqL/data

命令

但是 5.7以后已经放弃MysqL_install_db

所以使用新的

./bin/MysqLd--user=MysqL--basedir=/home/MysqL--datadir=/home/MysqL/data--initialize


这时有人会提示

./bin/MysqLd:errorwhileloadingsharedlibraries:libaio.so.1:cannotopensharedobjectfile:Nosuchfileordirectory


缺少libaio.so 文件 对此文件进行安装

yumsearchlibaio#查询文件
yuminstalllibaio#安装文件


后执行上面安装命令

2016-09-08T06:34:35.215617Z0[Warning]TIMESTAMPwithimplicitDEFAULTvalueisdeprecated.Pleaseuse--explicit_defaults_for_timestampserveroption(seedocumentationformoredetails).
2016-09-08T06:34:37.175558Z0[Warning]InnoDB:Newlogfilescreated,LSN=45790
2016-09-08T06:34:37.456478Z0[Warning]InnoDB:Creatingforeignkeyconstraintsystemtables.
2016-09-08T06:34:37.529873Z0[Warning]NoexistingUUIDhasbeenfound,soweassumethatthisisthefirsttimethatthisserverhasbeenstarted.GeneratinganewUUID:505a95f7-758e-11e6-a966-00163e0368de.
2016-09-08T06:34:37.531971Z0[Warning]Gtidtableisnotreadytobeused.Table'MysqL.gtid_executed'cannotbeopened.
2016-09-08T06:34:37.534742Z1[Note]Atemporarypasswordisgeneratedforroot@localhost:b/:q5Dd&mo-v

牢记上面的随机密码, 如上b/:q5Dd&mo-v,下面我们修改密码时需要用到。


PS.如果你的data文件内容 会报

[ERROR] --initialize specified but the data directory has files in it. Aborting.

这个错误 正确的是把data文件清空 rm -rf *


5.之后我们运行一下

cd/home/MysqL
/support-files/MysqL.serverstart

之后你会收到如下错误
./support-files/MysqL.server:line276:cd:/usr/local/MysqL:Nosuchfileordirectory
StartingMysqLCouldn'tfindMysqLserver(/usr/local/MysqL/[Failed]ld_safe)

因为默认的目录是/usr/local/MysqL我们换了别的目录只做继续修改
visupport-files/MysqL.server
按I进入编辑模式(这是新手的福音当年我就不知道)
找到
basedir=/home/MysqL
datadir=/home/MysqL/data
进行修改成现在的目录之后ESC,:号后WQ回车保存退出

继续运行我还不信了。。终于成功了
StartingMysqL.[OK]
不容易啊。。。。之后我们继续

6.创建软链接链接就是两个地方都可以用一个文件却不多占磁盘地方在linux里还是很有用的
#ln-s/home/MysqL/bin/MysqL/usr/bin/MysqL

7.创建配置文件
将默认生成的my.cnf备份
#mv/etc/my.cnf/etc/my.cnf.bak

进入MysqL的安装目录支持文件目录
#cd/home/MysqL/support-files

拷贝配置文件模板为新的MysqL配置文件,#cpmy-default.cnf/etc/my.cnf

可按需修改新的配置文件选项,不修改配置选项,MysqL则按默认配置参数运行.
如下是我修改配置文件/etc/my.cnf,设置编码为utf8以防乱码
#vim/etc/my.cnf

[MysqLd]

basedir=/home/MysqL
datadir=/home/MysqL/data

character_set_server=utf8
init_connect='SETNAMESutf8'


[client]
#default-character-set=utf8

8.配置MysqL服务开机自动启动

拷贝启动文件到/etc/init.d/下并重命令为MysqLd
#cp/home/MysqL/support-files/MysqL.server/etc/init.d/MysqLd

增加执行权限
#chmod755/etc/init.d/MysqLd

检查自启动项列表中没有MysqLd这个,如果没有就添加MysqLd:
#chkconfig--listMysqLd
#chkconfig--addMysqLd

设置MysqL在345等级自动启动
#chkconfig--level345MysqLdon

或用这个命令设置开机启动:
#chkconfigMysqLdon


9. MysqL服务的启动/重启/停止

启动MysqL服务

#serviceMysqLdstart

重启MysqL服务

#serviceMysqLdrestart

停止MysqL服务

#serviceMysqLdstop

10. 初始化MysqL用户root的密码

先将MysqL服务停止

#serviceMysqLdstop

进入MysqL安装目录, 执行:

cd/home/MysqL
./bin/MysqLd_safe--skip-grant-tables--skip-networking&

[1]6225[root@localhostMysqL]#15111002:46:08MysqLd_safeLogging
to'/home/MysqL/data/localhost.localdomain.err'.15111002:46:08MysqLd_safeStartingMysqLd
daemonwithdatabasesfrom/home/MysqL/data12345
另外打开一个终端(p.s.如果是ssh连接登录的,另外创建一个ssh连接即可),执行操作如下:
#MysqL-urootMysqL
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A

WelcometotheMysqLmonitor.Commandsendwith;or\g.
YourMysqLconnectionidis2Serverversion:5.7.9MysqLCommunityServer(GPL)

Copyright(c)2000,2015,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective

owners.Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
#MysqL>useMysqL;
Databasechanged

#MysqL>UPDATEuserSETpassword=PASSWORD('123456')WHEREuser='root';

ERROR1054(42S22):Unknowncolumn'password'in'fieldlist'

MysqL>updateusersetauthentication_string=PASSWORD('123456')whereuser='root';

QueryOK,1rowaffected,1warning(0.02sec)
Rowsmatched:1Changed:1Warnings:1

MysqL>flushprivileges;
QueryOK,0rowsaffected(0.00sec)

MysqL>\s
--------------
MysqLVer14.14Distrib5.7.9,forlinux-glibc2.5(x86_64)usingEditLinewrapper
Connectionid:2
Currentdatabase:MysqL
Currentuser:root@
SSL:Notinuse
Currentpager:stdout
Usingoutfile:''
Usingdelimiter:;
Serverversion:5.7.9MysqLCommunityServer(GPL)
Protocolversion:10
Connection:LocalhostviaUNIXsocket
Servercharacterset:utf8
Dbcharacterset:utf8
Clientcharacterset:utf8
Conn.characterset:utf8
UNIXsocket:/tmp/MysqL.sock
Uptime:4min47sec
Threads:1Questions:43Slowqueries:0Opens:127Flushtables:1Opentables:122Queriespersecondavg:0.149--------------

MysqL>exit;
Bye

到此, 设置完MysqL用户root的密码且确保MysqL编码集是utf8,注意上面, 新版本的MysqL.user表里的密码字段是authentication_string

快捷键ctrl + c停止# ./bin/MysqLd_safe ...命令, 重新启动MysqL服务, 用新密码连接MysqL

#serviceMysqLdstartStartingMysqLSUCCESS!
[root@localhostbin]#MysqL-uroot-pEnterpassword:
WelcometotheMysqLmonitor.Commandsendwith;or\g.
YourMysqLconnectionidis3Serverversion:5.7.9Copyright(c)2000,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/oritsaffiliates.
Othernamesmaybetrademarksoftheirrespective
owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

MysqL>useMysqL;
ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatement
beforeexecutingthisstatement.
MysqL>exit;
Bye
咦?又要我改密码,我们通过MysqLadmin来修改密码,先输入原密码,再设置新密码,总算可以了吧!!!
#cd/home/MysqL#./bin/MysqLadmin-uroot-ppasswordEnterpassword:
Newpassword:
Confirmnewpassword:
Warning:Sincepasswordwillbesenttoserverinplaintext,usesslconnectionto
ensurepasswordsafety.#MysqL-uroot-pEnterpassword:
WelcometotheMysqLmonitor.Commandsendwith;or\g.
YourMysqLconnectionidis6Serverversion:5.7.9MysqLCommunityServer(GPL)

Copyright(c)2000,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/oritsaffiliates.
Othernamesmaybetrademarksoftheirrespective
owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

MysqL>useMysqL;
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A

Databasechanged
MysqL>
或直接:
#./bin/MysqLadmin-uroot-p'b/:q5Dd&mo-v'password'123456'


MysqLadmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text,use ssl connection to ensure password safety.1234

其中,

b/:q5Dd&mo-v

就是我们在使用MysqLd --initialize时牢记下的随机密码

11. MysqL远程授权

格式如下:

MysqL>grantall[privileges]ondb_name.table_nameto'username'@'host'identifiedby'password';

示例如下:

MysqL>grantallprivilegeson*.*to'root'@'%'identifiedby'123456';

QueryOK,0rowsaffected,1warning(0.04sec)MysqL>FLUSHPRIVILEGES;QueryOK,0rowsaffected(0.00sec)MysqL>12345678

或用

MysqL>grantallon*.*to'root'@'%'identifiedby'123456';

打完收功!

ps.contos7使用firewall防火墙 可以先关闭进行远程连接

停用firewall:

systemctl stop firewalld

systemctl mask firewalld

试试远程开没开

之后再开启

systemctl start firewalld

开启防火墙端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent


重启防火墙

systemctl restart firewalld

原文链接:https://www.f2er.com/centos/380807.html

猜你在找的CentOS相关文章