博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Centos 7 可安装 mysql5.7
阅读量:6898 次
发布时间:2019-06-27

本文共 8525 字,大约阅读时间需要 28 分钟。

hot3.png

一、安装配置MySQL的yum源

安装MySQL的yum源,下面是RHEL6系列mysql5.6的下载地址:

wget 下面是RHEL6系列mysql5.7的下载地址: wget 安装yum源。 rpm -ivh mysql57-community-release-el6-8.noarch.rpm 清除缓存 yum clean all 下面几个命令可以顺便学习一下 复制代码 代码如下:

安装

yum-config-manager yum install yum-utils -y if centos yum报错Loaded plugins: fastestmirrorvim  /etc/yum/pluginconf.d/fastestmirror.conf enabled = 0vim /etc/yum.confplugins=0yum clean dbcache

禁用MySQL5.6的源

yum-config-manager --disable mysql56-community # 启用MySQL5.7的源 yum-config-manager --enable mysql57-community-dmr # 用下面的命令查看是否配置正确yum repolist enabled | grep mysql二、yum安装MySQL5.7本次安装的版本是5.7.14,测试环境上原来有5.1.73版本,是升级安装.yum install mysql

三、初始化并启动MySQL service mysqld start 直接启动服务会报错,如下: [root lib]# service mysqld start MySQL Daemon failed to start. 正在启动 mysqld: [失败] 解决方法:新版本要求先初始化mysql。 初始化mysql 复制代码 代码如下:

mysqld --initialize --user=mysql --datadir=/var/lib/mysql

或者 复制代码 代码如下:

mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql

另外,当你是以mysql的账户登录并执行程序的情况下,你可以将--user选项从命令中去掉。 通过官方文档我们可以知道,如果我是root身份登录Linux系 统,可以执行:mysqld --initialize --user=mysql或者mysqld --initialize-insecure --user=mysql。如果我是以mysql用户登录Linux系统,可以执行:mysqld --initialize或者mysqld --initialize-insecure。 不管在哪个平台上,使用--initialize选项就是以“默认安全模式”来进行安装的(即包含一个随机的root初始密码的生成)。在这种情况下,密 码是被标志为过期的,你需要选择一个新的密码。使用--initialize-insecure选项,没有root密码生成;如果是这样的话,在服务器投 入生产使用之前,你需要及时地为账户指定密码。 使用--initialize会为root账户生成一个随机的初始密码,我们可以使用命令:mysql -u root -p,然后输入密码来登录MySQL。使用--initialize-insecure不会为root账户生成一个随机的初始密码,我们可以使用命 令:mysql -u root --skip-password直接登录MySQL。 我初始化过程中遇到报错如下: [ERROR] --initialize specified but the data directory has files in it. Aborting. [ERROR] Aborting 解决方法: 复制代码 代码如下:

rm -rf /var/lib/mysql/* 原因分析: mysqld服务会检查数据目录是否存在,如下: 如果数据目录不存在,mysqld则会创建它。 如果数据目录存在,且不是空目录(即包含有文件或子目录),mysqld会显示一条错误信息并中止: [ERROR] --initialize specified but the data directory exists. Aborting. 遇到这种情况,就将数据目录删除或重命名后,重新再试一次。 总之: 初始化之前,先检查一下数据目录是否存在,执行命令:ls -l /var/lib|grep mysql。若有,则执行命令: rm -rf /var/lib/mysql,将其删除; 或者执行命令:mv /var/lib/mysql /var/lib/newname,将其重命名为newname,就可以了。 再次执行初始化命令,成功执行. [Note] A temporary password is generated for root: ihey0gFhTT;f 记住初始化生成的临时的root密码,登录时会用到 初始化完成之后,直接启动service mysqld start 四、连接MySQL并修改密码 初始化时会生成一个临时的root密码,且登录之后必须先改密码,然后才能执行命令. 复制代码 代码如下:

mysql -uroot -p 临时密码登录之后,如果不立即改密码,执行命令时报如下错误:mysql> show databases ;ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.修改密码: set password=password('123[@Zxc](https://my.oschina.net/u/82595)');或者:复制代码 代码如下:

alter user 'root'@'localhost' identified by '123';

再次执行成功. 从官方文档中我们了解到要用ALTER USER语句可以修改密码。登录MySQL后,执行语句: 复制代码 代码如下:

alter user 'root'@'localhost' identified by '123';

就将root账户的密码修改为123@Zxc了。 MySQL对用户密码安全性有所加强,所以设置的密码必须包含有数字,大写字母,小写字母,特殊符号,如果你设置的密码过于简单,会提示: ERROR 1819 (HY000): Your password does NOT satisfy the CURRENT policy requirements。

卸载mysqlyum remove mysqlyum remove mysql-libs  使用yum安装MySQL(CentOS 6.2): 第一次先安装repositoryhttp://dev.mysql.com/doc/refman/5.6/en/linux-installation-yum-repo.html#yum-repo-setup mkdir /home/toolsmkdir /home/tools/mysqlcd /home/tools/mysqlwget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpmsudo yum localinstall mysql57-community-release-el6-8.noarch.rpmsudo yum install mysql-community-server4、启动    4.1、启动服务:    service mysqld start     4.2、开机启动    chkconfig --levels 235 mysqld on获取mysql初始密码    grep 'temporary password' /var/log/mysqld.log5、配置 在/etc/my.cnf中添加 validate-password=OFF不然简单密码将不能被mysql接受    5.1、MySQL安全安装

mysql -uroot -p 临时密码 登录之后,如果不立即改密码,执行命令时报如下错误: mysql> show databases ; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 修改密码: set password=password('123'); 或者: 复制代码 代码如下:

如果mysql启动失败 ls -ld /var/run/mysqld/

chown mysql.mysql /var/run/mysqld/ [root@spark01 ~]# /etc/init.d/mysqld start

执行下面的命令:    /usr/bin/mysql_secure_installation    涉及内容包括:    设置或更改root密码 ( 初次安装,初始密码:1. ```grep 'temporary password' /var/log/mysqld.log```)    移除匿名用户    禁止远程登录root    删除测试数据库test    重新加载授权表
set password=password('密码要足够复杂');
******** 完成上面的步骤,即成功完成安装 常见异常 Mysql:is not allowed to connect to this MySQL server如果你想连接你的mysql的时候发生这个错误:ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server解决方法:1。 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"mysql -u root -p mysql>use mysql;mysql>update user set host = '%' where user = 'root';//刷新系统权限表mysql>flush privileges;mysql>select host, user from user;2. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123ABCabc' WITH GRANT OPTION;    重置密码方法  可尝试这样启动  mysqld_safe -skip-grant-tables 或者在/etc/my.ini的[mysqld]字段加入:skip-grant-tables重启mysql服务,这时的mysql不需要密码即可登录数据库然后进入mysqlmysql>use mysql;mysql>update user set password=password('新密码') WHERE User='root';mysql>flush privileges;运行之后最后去掉my.ini中的skip-grant-tables,重启mysqld即可。5.7 中用:update user set authentication_string=password('123ABCabc') WHERE User='root';ALTER USER USER() IDENTIFIED BY password('123ABCabc');  You must reset your password using ALTER USER statement before executing this statement通过"skip-grant-tables"配置,客户端连接 ,修改 user 表的字段 password_expired 为  'N'; 方法1: 用SET PASSWORD命令  mysql -u root  mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');方法2:用mysqladmin  mysqladmin -u root password "newpass"  如果root已经设置过密码,采用如下方法  mysqladmin -u root password oldpass "newpass"方法3: 用UPDATE直接编辑user表  mysql -u root  mysql> use mysql;  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';  mysql> FLUSH PRIVILEGES;在丢失root密码的时候,可以这样  mysqld_safe --skip-grant-tables&  mysql -u root mysql  mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';  mysql> FLUSH PRIVILEGES;经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详 6、自定义MySQL数据文件目录    有时需要自定义MySQL的数据文件夹,即datadir;    6.1、建立目标目录,这里假定为 /home/data/mysql;     mkdir /home/datamv /var/lib/mysql /home/data/         6.2、授权目录读写权限给MySQL的运行用户,上面安装后的MySQL会以用户mysql运行,则授权语句如下: chown -R         mysql:mysql /home/data/mysql    6.3、修改/etc/my.cnf文件,[mysqld]增加datadir、socket配置,[client]也需要增加socket配置            [mysqld]            datadir=/home/data/mysql            socket=/home/data/mysql/mysql.sock            [client]            socket=/home/data/mysql/mysql.sock                6.4、如果启动失败,可通过cat /var/log/mysqld.log 查看启动日志,如果出现如下错误,则是由于SELINUX限制引起        InnoDB: Operating system error number 13 in a file operation.        InnoDB: The error means mysqld does not have the access rights to        InnoDB: the directory.        解决方法: chcon -Rt mysqld_db_t /home/data/mysql7、一、Warning: World-writable config file '/etc/my.cnf' is ignoredTo fix this problem, use the following command to change file’s permissions要修改该问题,使用以下命令更改该文件的权限。 chmod 644 /etc/my.cnf 4、找到my.cnf配置文件    如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf)中。命令如下:    [root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

1.mysql -u root -p mysql # 第1个mysql是执行命令,第2个mysql是系统数据名称

在mysql控制台执行:

1.grant all privileges on . to 'root'@'%' identified by '123456' with grant option;

2.# root是用户名,%代表任意主机,'123456'指定的登录密码(这个和本地的root密码可以设置不同的,互不影响)

3.flush privileges; # 重载系统权限

4.exit;

允许3306端口

1.iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

2.# 查看规则是否生效

3.iptables -L -n # 或者: service iptables status

4.# 此时生产环境是不安全的,远程管理之后应该关闭端口,删除之前添加的规则

5.iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

PS,上面iptables添加/删除规则都是临时的,如果需要重启后也生效,需要保存修改: service iptables save # 或者: /etc/init.d/iptables save

另外,

vi /etc/sysconfig/iptables # 加上下面这行规则也是可以的

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

远程管理数据库的软件,Windows系统下可以使用Navicat,用了几种远程软件,感觉这个用起来蛮不错的。

#先检查是否安装了iptables  service iptables status  #安装iptables  yum install -y iptables  #升级iptables  yum update iptables   #安装iptables-services  yum install iptables-services  #停止firewalld服务  systemctl stop firewalld  #禁用firewalld服务  systemctl mask firewalld  service iptables save  systemctl restart iptables.service  #注册iptables服务  #相当于以前的chkconfig iptables on  systemctl enable iptables.service  #开启服务  systemctl start iptables.service  #查看状态  systemctl status iptables.service

转载于:https://my.oschina.net/u/1052192/blog/834557

你可能感兴趣的文章
我的友情链接
查看>>
js去掉字符串前后空格的五种方法
查看>>
忆秦娥·娄山关
查看>>
p2p
查看>>
Vue生命周期钩子函数
查看>>
通过MAVEN将JAVA工程中引用的外部JAR包一起打war包
查看>>
Tomcat源码学习探索笔记
查看>>
微信小程序image图片实现高度自适应
查看>>
基于jax-ws的webservice 简单实例
查看>>
33、C#里面的进度条和时钟组件的使用
查看>>
041、Linux服务器监控CPU温度
查看>>
进程管理工具htop/glances/dstat的使用
查看>>
支付宝H5和微信公众号H5支付
查看>>
配置Apache日志----防盗链
查看>>
我的友情链接
查看>>
JPA的实体
查看>>
我的友情链接
查看>>
MDT 2012 批量部署(六)——MDT捕获镜像
查看>>
RMAN--obsolete 和 expired的区别
查看>>
windows 2003集群中安装sharepoint 2007配置时用户名无效
查看>>