当前位置: > CentOS > CentOS教程 >

CentOS 7.1下rpm安装 MySQL

时间:2017-05-16 15:59来源:linux.it.net.cn 作者:IT

1. 下载MySQL的repo源

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

2. 安装mysql57-community-release-el7-8.noarch.rpm包

rpm -ivh mysql57-community-release-el7-8.noarch.rpm –nodeps –force 
安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo 
3.安装mysql

yum install mysql-server 
4. 启动mysql服务

查看MySQL服务是否已启动 
service mysqld status 
启动服务 
systemctl start mysqld 
5. 重置root密码

MySQL5.7会在安装后为root用户生成一个随机密码,而不是像以往版本的空密码。 
可以安全模式修改root登录密码或者用随机密码登录修改密码。下面用随机密码方式 
MySQL为root用户生成的随机密码通过mysqld.log文件可以查找到: 
grep ‘temporary password’ /var/log/mysqld.log 
修改root用户密码:(MySQL的密码策略比较复杂,过于简单的密码会被拒绝) 
mysql -u root -p 
mysql> Enter password: (输入刚才查询到的随机密码) 
mysql> SET PASSWORD FOR ‘root’@’localhost’= “123”; 
mysql> exit 
用root新密码登录: 
mysql -u root vmroot!@#456VMROOT 
如果上面的方式不能修改可以使用下面安全模式修改root: 
关闭服务 
systemctl stop mysqld.service 
vi /etc/my.cnf 
mysqld下面添加skip-grant-tables 保存退出启动服务 
systemctl start mysqld.service 
mysql -u root 不用密码直接回车 
use mysql 
update user set authentication_string=password(‘Root-123’) where User=’root’ and Host=’localhost’; 
flush privileges; 
exit; 
vi /etc/my.cnf 把 skip-grant-tables 一句删除保存退出重启mysql服务 
systemctl restart mysqld.service 
再次登录即可 
mysql -u root -pRoot-123

如果进行操作出现下面的提示: 
You must reset your password using ALTER USER statement before executing this statement. 
就再设置一遍密码 
set password = password(‘Root-123’); 
6. 开放3306端口

允许使用用户名root密码Root-123456从任何主机连接到mysql服务器 
mysql>grant all on root.* to root@’%’ identified by ‘vmroot!@#456VMROOT’; 
mysql>FLUSH PRIVILEGES; 
mysql>exit; 
开启防火墙mysql 3306端口的外部访问 
firewall-cmd –zone=public –add-port=3306/tcp –permanent 
firewall-cmd –reload

7.建立MySQL的用户组和用户名

用户可以建立一个专门用于MySQL的用户组和用户名,用于启动和关闭MySQL数据库,以及对数据库的一些操作。建立用户组可以使用如下命令:

linux 命令 groupadd mysql //创建mysql组 
命令执行完毕后用户可以查看/etc/group文件,正确添加用户组后,用户可以在该文件中看到类似于以下的一行内容:

mysql :x :101: 
添加了用户组后,接着应该添加用户。在Linux下添加用户使用useradd命令。此处,添加一个名为mysql的用户,使用的命令如下:

useradd -g mysql mysql 
该命令用于创建mysql用户,并放到mysql组里。-g参数选项用于指定一个组名,并将新建的用户添加到该组,作为该组的一个成员。
原文:http://blog.csdn.net/ZhongGuoZhiChuang/article/details/71640469



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容