美文网首页安装部署
DEPLOY_MYSQL5.7.18_CentOS7.3

DEPLOY_MYSQL5.7.18_CentOS7.3

作者: 明训 | 来源:发表于2019-01-27 21:01 被阅读0次

    描述了系统CentOS7.3下MYSQL5.7.18部署过程和注意事项

    依赖组件

    1. libaio
    2. net-tools
    3. numactl

    下载

    Mysql安装依赖libaio和net-tools组件,下载地址整理:

    1. mysql安装包
    2. libaio
    3. net-tools
    4. numactl

    官网下载MySQL安装包:https://dev.MySQL.com/downloads/file/?id=469456

    wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar
    

    安装步骤

    环境检查

    CentOS7.X安装默认安装gnome版本时自带了mariadb,在安装时,需要检查mariadb是否存在,如果存在则需要移除mariadb

    [root@dev01 software]# rpm -qa|grep mariadb
     mariadb-libs-5.5.44-2.el7.centos.x86_64 
    [root@dev01 software]# rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps
    

    执行移除命令

    [root@localhost ~]# rpm -e --nodeps `rpm -qa|grep mariadb`
    [root@localhost ~]# rpm -qa|grep mariadb
    [root@localhost ~]# 
    

    关闭防火墙

    这一步骤非常重要,防火墙如果不关闭可能会导致开启mysql远程访问时,出现10060错误

    [root@localhost software]# systemctl stop firewalld.service 
    [root@localhost software]# systemctl disable firewalld.service 
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
    [root@localhost software]# firewall-cmd --state
    not running
    [root@localhost software]# 
    

    关闭SELINUX

    [root@sis01 zkui2.0] setenforce 0
    

    编辑配置文件/etc/selinux/config

    [root@sis01 zkui2.0] vim /etc/selinux/config
    

    原始文件内容

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    

    调整后的文件内容

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    

    或者执行命令

    [root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    

    安装依赖项

    [root@localhost software]# rpm -Uvh libaio-0.3.109-13.el7.x86_64.rpm
    [root@localhost software]# rpm -Uvh net-tools-2.0-0.22.20131004git.el7.x86_64.rpm
    [root@localhost software]# rpm -Uvh numactl-2.0.9-6.el7_2.x86_64.rpm
    

    CentOS7.X支持软件包指下载不安装,使用--downloadonly --downloaddir=DLDIR两个参数即可实现
    [root@localhost mysql]# yum install net-tools --downloadonly --downloaddir=./net-tools

    解压mysql安装包

    [root@localhost ~]# mkdir -p /xxx/soft/mysql
    [root@localhost ~]# cd /xxx/soft/mysql/
    [root@localhost mysql]# ls
    mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar
    [root@localhost mysql]# 
    [root@localhost mysql]# tar -xvf mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar
    mysql-community-server-5.7.18-1.el7.x86_64.rpm
    mysql-community-embedded-devel-5.7.18-1.el7.x86_64.rpm
    mysql-community-devel-5.7.18-1.el7.x86_64.rpm
    mysql-community-client-5.7.18-1.el7.x86_64.rpm
    mysql-community-common-5.7.18-1.el7.x86_64.rpm
    mysql-community-embedded-5.7.18-1.el7.x86_64.rpm
    mysql-community-embedded-compat-5.7.18-1.el7.x86_64.rpm
    mysql-community-libs-5.7.18-1.el7.x86_64.rpm
    mysql-community-server-minimal-5.7.18-1.el7.x86_64.rpm
    mysql-community-test-5.7.18-1.el7.x86_64.rpm
    mysql-community-minimal-debuginfo-5.7.18-1.el7.x86_64.rpm
    mysql-community-libs-compat-5.7.18-1.el7.x86_64.rpm
    

    安装mysql安装包

    common–>libs–>client–>server的顺序。若不按照此顺序,也会有一定“依赖”关系的提醒。

    [root@localhost mysql]# rpm -ivh mysql-community-common-5.7.18-1.el7.x86_64.rpm 
    [root@localhost mysql]# rpm -ivh mysql-community-libs-5.7.18-1.el7.x86_64.rpm
    [root@localhost mysql]# rpm -ivh mysql-community-client-5.7.18-1.el7.x86_64.rpm 
    [root@localhost mysql]# rpm -ivh mysql-community-server-5.7.18-1.el7.x86_64.rpm
    [root@localhost mysql]# 
    

    依次执行(几个包有依赖关系,所以执行有先后)

    初始化数据库

    为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化

    [root@dev01 software] mysqld --initialize --user=mysql
    
    1. 如果是以 mysql 身份运行,则可以去掉 --user 选项。另外 --initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,
    2. 登陆后你需要设置一个新的密码,而使用 --initialize-insecure 命令则不使用安全模式,则不会为 root 用户生成一个密码。
    3. 这里演示使用的 --initialize 初始化的,会生成一个 root 账户密码,密码在log文件里
    [root@localhost mysql]# cat /var/log/mysqld.log
    2019-01-27T20:05:26.613885Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2019-01-27T20:05:29.752270Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2019-01-27T20:05:30.026009Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2019-01-27T20:05:30.103420Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e558679d-226e-11e9-9bd7-000c291cde07.
    2019-01-27T20:05:30.104822Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2019-01-27T20:05:30.107221Z 1 [Note] A temporary password is generated for root@localhost: N)ShYEmDI7ww
    

    此时的数据库密码即为:N)ShYEmDI7ww

    启动数据库

    [root@localhost mysql]# systemctl start mysqld.service
    [root@localhost mysql]# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.16
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    

    修改用户密码

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxx';
    mysql> exit
    Bye
    

    以前的 password()函数将会被抛弃,官方建议使用上面的命令来修改密码
    或者

    mysql> SET PASSWORD = PASSWORD('xxx');
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    mysql> 
    

    如果出现错误ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    mysql> set global validate_password_policy=0; //0最低,1中级,2最强验证级别

    Query OK, 0 rows affected (0.00 sec)

    Policy Tests Performed
    0 or LOW Length
    1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
    2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file

    调整mysql配置文件

    修改my.cnf

    vim /etc/my.cnf
    

    在 [mysqld] 配置节下追加以下内容

    default-storage-engine=INNODB
    character-set-server=utf8
    collation-server=utf8_general_ci
    lower_case_table_names=1
    max_connections=10000
    max_connect_errors=100000
    wait_timeout=31536000
    interactive-timeout=31536000
    sql-mode="ALLOW_INVALID_DATES"
    max_allowed_packet=16M
    bind-address=0.0.0.0
    default-time_zone = '+8:00'
    

    bind-address也可以绑定真实的ip地址

    增加mysql配置节

    [mysql]
    default-character-set=utf8
    

    重启mysql服务

    方式一

    [root@localhost mysql]# systemctl restart mysqld.service
    

    方式二

    [root@sis01 my.cnf.d]# service mysqld restart
    Redirecting to /bin/systemctl restart  mysqld.service
    

    验证表名忽略大小写(MYSQL不区分表名大小写)

    忽略表名大小写主要是为了解决在Centos7 下mysql大小写敏感问题,会导致程序运行时找不到对应的表的问题,在linux下,mysql的表名区分大小写,而在windows下是不区分,从windows下导出的数据脚本中使用的是小写,而Hibernate生成的sql中表名是大写的,所以查不出数据,忽略大小写则需要改变配置项lower_case_table_names=1。

    mysql> show variables like "%case%";
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | lower_case_file_system | OFF   |
    | lower_case_table_names | 0     |
    +------------------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> 
    

    lower_case_table_names 是0 表示没有开启忽略表名大小写

    MYSQL在LINUX下数据库名、表名、列名、别名大小写规则如下
    1.数据库名与表名是严格区分大小写的 
    2.表的别名是严格区分大小写的 
    3.列名与列的别名在所有的情况下均是忽略大小写的 
    4.变量名也是严格区分大小写的 
    

    如果已经开启了表明忽略大小写,则查询出来的结果应该如下所述

    mysql> show variables like "%case%";
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | lower_case_file_system | OFF   |
    | lower_case_table_names | 1     |
    +------------------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> 
    

    赋予远程访问

    mysql>  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxx' WITH GRANT OPTION;
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.01 sec)
    

    查看用户设置,确认生效

    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.sys |
    | localhost | root      |
    +-----------+-----------+
    3 rows in set (0.00 sec)
    mysql> 
    

    卸载步骤

    查看是否安装了MySQL

    [root@localhost soft]#  rpm -qa | grep mysql
    mysql-community-libs-5.7.18-1.el7.x86_64
    mysql-community-server-5.7.18-1.el7.x86_64
    mysql-community-common-5.7.18-1.el7.x86_64
    mysql-community-client-5.7.18-1.el7.x86_64
    [root@localhost soft]# 
    

    查看MySQL服务是否启动

    [root@localhost soft]# service mysqld status
    Redirecting to /bin/systemctl status  mysqld.service
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2019-01-28 04:09:22 CST; 45min ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 18691 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
      Process: 18669 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 18694 (mysqld)
       CGroup: /system.slice/mysqld.service
               └─18694 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    
    Jan 28 04:09:21 localhost.localdomain systemd[1]: Starting MySQL Server...
    Jan 28 04:09:22 localhost.localdomain systemd[1]: Started MySQL Server.
    [root@localhost soft]# service mysqld stop
    Redirecting to /bin/systemctl stop  mysqld.service
    [root@localhost soft]# 
    

    卸载MySQL安装的组件

    [root@localhost soft]# rpm -ev mysql-community-server-5.7.18-1.el7.x86_64
    Preparing packages...
    mysql-community-server-5.7.18-1.el7.x86_64
    warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
    [root@localhost soft]# rpm -ev mysql-community-client-5.7.18-1.el7.x86_64
    Preparing packages...
    mysql-community-client-5.7.18-1.el7.x86_64
    [root@localhost soft]# rpm -ev mysql-community-libs-5.7.18-1.el7.x86_64
    Preparing packages...
    mysql-community-libs-5.7.18-1.el7.x86_64
    [root@localhost soft]# rpm -ev mysql-community-common-5.7.18-1.el7.x86_64
    Preparing packages...
    mysql-community-common-5.7.18-1.el7.x86_64
    [root@localhost soft]#
    

    删除配置文件

    查找相关文件位置

    [root@localhost soft]# whereis mysql
    mysql: /usr/share/mysql
    [root@localhost soft]# find / -name mysql
    find: ‘/run/user/1000/gvfs’: Permission denied
    /etc/selinux/targeted/active/modules/100/mysql
    /etc/selinux/targeted/tmp/modules/100/mysql
    /var/lib/mysql
    /var/lib/mysql/mysql
    /usr/share/mysql
    [root@localhost soft]#
    

    删除MySQL相关的文件

    [root@localhost soft]# rm -rf /usr/share/mysql/
    [root@localhost soft]# rm -rf /var/lib/mysql/
    [root@localhost soft]# rm -rf /var/log/mysqld.log    删除日志文件
    [root@localhost soft]# rm -rf /etc/my.cnf.rpmsave    删除备份的配置
    

    删除mysql用户及用户组

    [root@localhost soft]# more /etc/passwd | grep mysql
    mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
    [root@localhost soft]# more /etc/shadow | grep mysql
    mysql:!!:17923::::::
    [root@localhost soft]# more /etc/group | grep mysql
    mysql:x:27:
    [root@localhost soft]#
    [root@localhost soft]# userdel mysql
    [root@localhost soft]# groupdel mysql
    groupdel: group 'mysql' does not exist
    [root@localhost soft]# 
    

    查看是否已经删除

    [root@localhost soft]# rpm -qa | grep mysql
    [root@localhost soft]# whereis mysql
    mysql:[root@localhost soft]# find / -name mysql
    find: ‘/run/user/1000/gvfs’: Permission denied
    /etc/selinux/targeted/active/modules/100/mysql
    /etc/selinux/targeted/tmp/modules/100/mysql
    [root@localhost soft]# 
    [root@localhost soft]# id mysql
    id: mysql: no such user
    [root@localhost soft]# 
    

    相关文章

      网友评论

        本文标题:DEPLOY_MYSQL5.7.18_CentOS7.3

        本文链接:https://www.haomeiwen.com/subject/fqvnjqtx.html