Linux(CentOS7-Minimal【最小版】)安装MySQL5.7.19

1、下载MySQL5.7.19安装包

  下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz

        https://dev.mysql.com/downloads/mysql/#downloads

2、检查MySQL文件是否存在,如有删除,查看当前系统里面是否还有已经有安装文件

      查看命令  rpm -qa | grep mysql  删除命令: rpm -e mysql-libs-5.*.x86_64 --nodeps

3、检查mysql组和用户是否存在,如无创建(主要目的为MySQL用户和用户组与root分离 用于安全着想)。

      3.1、检查mysql用户组是否创建      

cat /etc/group | grep mysql
     mysql:x:1000:
cat /etc/passwd | grep mysq
     mysql:x:1000:1000::/home/mysql:/bin/bash

     3.2、以上为默认存在的情况,如无,执行添加命令:

[root@localhost ~]$groupadd mysql
[root@localhost ~]$useradd -r -g mysql mysql
//useradd -r参数表示mysql用户是系统用户,不可用于登录系统。

4、确定安装目录和数据目录

   4.1、创建自己安装MySQL的basedir 和data目录

        我的mysql安装目录:  /database/mysql/mysql5719,数据目录:  /database/mysql/mysql5719/data

      mkdir   /database/mysql/mysql5719 

        mkdir   /database/mysql/mysql5719/data

 5、解压MySQL的安装文件 mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz 到  /database/mysql/mysql5719 目录

       tar -zxvf  mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /database/mysql/mysql5719

       可以参考如下命令:https://jingyan.baidu.com/article/ceb9fb10bee9a58cad2ba00c.html

 最后结构目录如下图所示:

6、对mysql用户进行授权可访问的mysql目录

 6.1、将mysql目录及下面的文件、子目录文件主改成mysql用户所访问

#cd /database

#chown -R mysql:mysql mysql/

 6.2、给与mysql目录及下面的文件、子目录755权限

#chmod -R 755 mysql/

7、编译安装并初始化mysql,并记住临时密码

/database/mysql/mysql5719/bin/mysqld --initialize --user=mysql --datadir=/database/mysql/mysql5719/data --basedir=/database/mysql/mysql5719

当时安装的临时密码为:root@localhost: tq7U.B/sm3UF

8、创建日志文件目录

mkdir -p /database/mysql/mysql5719/log/mariadb
cd
/database/mysql/mysql5719/log/mariadb
touch mariadb.log
chmod -R 775 mariadb.log
chown -R mysql:mysql mariadb.log chown -R mysql:mysql /database/mysql

9、修改/etc/my.cnf文件

[mysqld]
basedir=/database/mysql/mysql5719
datadir=/database/mysql/mysql5719/data
socket=/tmp/mysql.sock
max_connections=1000
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/database/mysql/mysql5719/log/mariadb/mariadb.log
pid-file=/database/mysql/mysql5719/log/run/mariadb/mariadb.pid
socket=/tmp/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

10、启动mysql服务

      /database/mysql/mysql5719/support-files/mysql.server start

11、登录msyql,输入密码(密码为第7步骤的初始化密码)

     #mysql -u root -ptq7U.B/sm3UF

12、修改密码为“123456”

msql>alter user 'root'@'localhost' identified by '123456';
mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;
mysql>quit

13、安装成功后输入

       ps aux|grep mysql

14、CentOS环境开放3306端口

 14.1 可以停止防火墙  service firewall stop

 centos从7开始默认用的是firewalld,这个是基于iptables的,

   虽然有iptables的核心,但是iptables的服务是没安装的。所以你只要停止firewalld服务即可:

   systemctl stop firewalld.service && systemctl disable firewalld.service

  14.2 安装 iptables 方式

    yum install iptables-services
    systemctl enable iptables &&  systemctl enable ip6tables
    systemctl start iptables &&   systemctl start ip6tables  

     以iptables方式需要添加开放端口如3306 或者8080类似 

    安装完毕 iptables-services 后执行如下命令编辑iptables

   vi/etc/sysconfig/iptables #编辑防火墙配置文件

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

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

   本人编辑的如下所示:

可以使用如下命令 firewall-cmd --state 查看防火墙状态

可以参考 如下网站:http://www.linuxidc.com/Linux/2015-05/117473.htm http://www.linuxidc.com/Linux/2013-09/89840.htm

15、mysql如何远程连接

  mysql -uroot p123456

mysql> use mysql;

Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed

mysql> update user set host = '%' where user = 'root';

Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.01 sec)

//一定要重启才会生效。

/etc/init.d/mysqld restart 

16、使用Navicat连接数据库如下图所示

17、让mysql开机自动启动

  17.1 修改rc.local文件

          添加如下代码:/etc/rc.d/init.d/mysqld start 

  17.2 使用chkconfig命令实现

      先查看所有自动启动服务

          chkconfig --list //指定查看 chkconfig --list mysqld

          如果没有添加到chkconfig列表中           

          chkconfig --add mysqld  

      开启自动启动

          chkconfig mysqld on 

     查看是否启动了

          chkconfig --list mysqld

结果显示:

      mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

      表示在系统级别为:2、3、4、5时自动启动

本人的安装之后的结果如下所示:

       

 

posted @ 2017-09-23 22:45  蜀山剑侠  阅读(1018)  评论(0)    收藏  举报