centos7安装mysql

来源:互联网 发布:淘客网站源码建立 编辑:程序博客网 时间:2024/06/11 11:27

一、系统环境

[root@bogon /]# yum search libaioCentOS Linux release 7.1.1503 (Core)

二、安装

[root@bogon /]# yum install mysql........Installed:  mariadb.x86_64 1:5.5.52-1.el7Dependency Installed:  mariadb-libs.x86_64 1:5.5.52-1.el7Complete!........[root@bogon /]# yum install mysql-server........Installed:  mariadb-devel.x86_64 1:5.5.52-1.el7Complete!........
安装mysql和mysql-devel都成功,但是安装mysql-server失败,如下所示

[root@bogon /]# yum install mysql-serverLoaded plugins: fastestmirrorRepository epel is listed more than once in the configurationLoading mirror speeds from cached hostfileNo package mysql-server available.Error: Nothing to do

查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。

有两种解决办法

方法1:安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

安装mariadb,大小59 M。

[root@bogon /]# yum install mariadb-server mariadb.........Installed:  mariadb-server.x86_64 1:5.5.52-1.el7Dependency Installed:  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7            perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7  perl-DBD-MySQL.x86_64 0:4.023-5.el7                     perl-DBI.x86_64 0:1.627-4.el7  perl-IO-Compress.noarch 0:2.061-2.el7                   perl-Net-Daemon.noarch 0:0.48-5.el7  perl-PlRPC.noarch 0:0.2020-14.el7Complete!.........

设置Mariadb

[root@bogon /]# systemctl start mariadb  #启动MariaDB[root@bogon /]# systemctl enable mariadb  #设置开机启动Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.[root@bogon /]# systemctl status mariadb  #查看MariaDB状态● mariadb.service - MariaDB database server   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)   Active: active (running) since Mon 2017-08-07 17:23:09 CST; 33s ago Main PID: 6676 (mysqld_safe)   CGroup: /system.slice/mariadb.service           ├─6676 /bin/sh /usr/bin/mysqld_safe --basedir=/usr           └─6834 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/p...Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: strongly recommended for production s...s.Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: See the MariaDB Knowledgebase at http...heAug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: MySQL manual for more instructions.Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: You can start the MariaDB daemon with:Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: cd '/usr' ; /usr/bin/mysqld_safe --da...l'Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: You can test the MariaDB daemon with ...plAug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mariadb-prepare-db-dir[6598]: cd '/usr/mysql-test' ; perl mysql-tes...plAug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mysqld_safe[6676]: 170807 17:23:07 mysqld_safe Logging to '/var/lo...g'.Aug 07 17:23:07 iz8vbjca1ezyfhdmm3uxovz mysqld_safe[6676]: 170807 17:23:07 mysqld_safe Starting mysqld dae...sqlAug 07 17:23:09 iz8vbjca1ezyfhdmm3uxovz systemd[1]: Started MariaDB database server.Hint: Some lines were ellipsized, use -l to show in full.
启动MySQL

[root@bogon /]# mysqlWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 2Server version: 5.5.52-MariaDB MariaDB ServerCopyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]>

安装mariadb后显示的也是 MariaDB [(none)]> ,可能看起来有点不习惯。下面是第二种方法。



方式2:从官网下载安装Mysql

安装环境:CentOS7 64位 MINI版,安装MySQL5.7

  • 配置YUM源

在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/

# 下载mysql源安装包shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安装mysql源shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查mysql源是否安装成功

# 下载mysql源安装包shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安装mysql源shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查mysql源安装是否正确
看到上图所示表示安装成功。
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。改完之后的效果如下所示:

  • 安装MySQL
shell> yum install mysql-community-server

  • 启动MySQL服务

shell> systemctl start mysqld

查看MySQL的启动状态


  • 开机启动
shell> systemctl enable mysqldshell> systemctl daemon-reload

  • 修改root本地登录密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

shell> grep 'temporary password' /var/log/mysqld.log


shell> mysql -uroot -pmysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; 

或者

mysql> set password for 'root'@'localhost'=password('MyNewPass4!'); 

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,如下图所示:密码策略提示

通过msyql环境变量可以查看密码策略的相关信息:

mysql> show variables like '%password%';

mysql密码策略
validate_password_policy:密码策略,默认为MEDIUM策略
validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
validate_password_length:密码最少长度
validate_password_mixed_case_count:大小写字符长度,至少1个
validate_password_number_count :数字至少1个
validate_password_special_char_count:特殊字符至少1个
上述参数是默认策略MEDIUM的密码检查规则。

共有以下几种密码策略:

策略检查规则0 or LOWLength1 or MEDIUMLength; numeric, lowercase/uppercase, and special characters2 or STRONGLength; numeric, lowercase/uppercase, and special characters; dictionary file

MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy

修改密码策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件validate_password_policy=0

如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

validate_password = off

重新启动mysql服务使配置生效:

validate_password = off

  • 添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

validate_password = off

  • 配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

[mysqld]character_set_server=utf8init_connect='SET NAMES utf8'

重新启动mysql服务,查看数据库默认编码如下所示:

mysql默认编码


默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid



三、卸载
  • 检查是否安装了MySQL
[root@bogon /]# yum list installed | grep mysqlmysql-community-client.x86_64        5.7.19-1.el7                      @mysql57-communitymysql-community-common.x86_64        5.7.19-1.el7                      @mysql57-communitymysql-community-libs.x86_64          5.7.19-1.el7                      @mysql57-communitymysql-community-libs-compat.x86_64   5.7.19-1.el7                      @mysql57-communitymysql-community-server.x86_64        5.7.19-1.el7                      @mysql57-communitymysql57-community-release.noarch     el7-11                            @/mysql57-community-release-el7-11.noarch
  • 卸载
[root@bogon /]# yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-server mysql57-community-release..........Running transaction  Erasing    : mysql57-community-release-el7-11.noarch                                                        1/5  Erasing    : mysql-community-server-5.7.19-1.el7.x86_64                                                     2/5  Erasing    : mysql-community-client-5.7.19-1.el7.x86_64                                                     3/5  Erasing    : mysql-community-libs-5.7.19-1.el7.x86_64                                                       4/5  Erasing    : mysql-community-common-5.7.19-1.el7.x86_64                                                     5/5  Verifying  : mysql-community-libs-5.7.19-1.el7.x86_64                                                       1/5  Verifying  : mysql-community-common-5.7.19-1.el7.x86_64                                                     2/5  Verifying  : mysql-community-client-5.7.19-1.el7.x86_64                                                     3/5  Verifying  : mysql57-community-release-el7-11.noarch                                                        4/5  Verifying  : mysql-community-server-5.7.19-1.el7.x86_64                                                     5/5Removed:  mysql-community-client.x86_64 0:5.7.19-1.el7            mysql-community-common.x86_64 0:5.7.19-1.el7  mysql-community-libs.x86_64 0:5.7.19-1.el7              mysql-community-server.x86_64 0:5.7.19-1.el7  mysql57-community-release.noarch 0:el7-11...........[root@bogon /]# rpm -qa |grep -i mysql[root@bogon ~]# find / -name mysql/usr/share/mysql/var/lib/mysql/var/lib/mysql/mysql[root@bogon /]# rm -rf /usr/share/mysql /var/lib/mysql /var/lib/mysql/mysql