zoukankan      html  css  js  c++  java
  • centos mysql 实战 第一节课 安全加固 mysql安装

    centos mysql  实战  第一节课   安全加固  mysql安装

    percona名字的由来=consultation 顾问+performance 性能=per  con  a

    mysql官方手册:http://dev.mysql.com/doc/refman/5.7/en/datetime.html

    mysql_install_db是一个perl脚本

    http://mp.weixin.qq.com/s?__biz=MzI3MTAxMTY5OA==&mid=2671913078&idx=1&sn=6cf415e8c8137244aa46af48305dc356&scene=0#wechat_redirect

     
    MySQL里面show processlist  每一个process就是一个线程 

    mysql  /opt/mysql/xxx   一般安装在/opt下面


    ln -s /opt/mysql/mysql-5.6.29-linux-glibc2.5-x86_64  /usr/local/mysql  做一个软链接过去/usr/local/mysql
    原来已经有mysql软链接 ,需要unlink /usr/local/mysql

    mysql
    mysqlbinlog
    mysqld
    mysqld_safe
    perror
    mysqldump



    看一下系统中是否有mysql这个用户
    id   mysql


    建立一个mysql用户
    useradd  -s /sbin/nologin  -M  mysql


    不要用root启动mysql,系统不会限制root的资源 ,ulimit,最好用普通用户去跑mysql

    添加环境变量,两种方法
    echo  "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
    echo  "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile.d/mysql.sh

    要掌握基本命令,screen,vim,要求能在Linux下输入20个命令不重复


    总结

    如果缺少libaio库需要安装 :yum install -y libaio*
    Linux开发包: yum groupinstall  -y  'Development tools'


    1. 下载MySQL
    wget  -c https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

    地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads
    选择Linux Generic





    2. 解压
    tar zxvf mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz

    -------------------------------------------------------------------------
    centos7需要安装
    yum -y install numactl
    -------------------------------------------------------------------------


    3. 创建用户
    useradd  -M -s /sbin/nologin mysql

    4. 创建必须的目录
    mkdir -p /usr/local/mysql
    mkdir -p /data/mysql/mysql3306/{data,tmp,logs}


    5. 拷贝mysql文件
    mv  /data/download/mysql-5.6.30-linux-glibc2.5-x86_64/*   /usr/local/mysql/
    cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql



    6. 环境变量
    echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    source /etc/profile

    ====== 启动起来 ========================

    7. 权限
    chown -R mysql:mysql /data/mysql/mysql3306/
    chown -R mysql:mysql /usr/local/mysql


    8. 创建一个配置文件  如果是多个实例分开多个my.cnf  my3306.cnf  my3307.cnf
    vim %s/3306/3306/g
    socket = /tmp/mysql3376.sock
    innodb_data_file_path = ibdata1:1024M:autoextend


    9. 初始化
    5.1,5.5,5.6
    cd /usr/local/mysql/
    ./scripts/mysql_install_db  --defaults-file=/etc/my.cnf
    看到两次ok

    也可以使用/usr/local/mysql/bin/mysql_secure_installation 来初始化mysql,进行安全加固


    5.7
    --设置随机密码
    /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf  --initialize

    cat  /data/mysql/mysql3306/logs/error.log|grep -i  'password'
    2016-11-30T13:28:37.276714Z 1 [Note] A temporary password is generated for root@localhost: 6fT?TYzJI*Dg

    --不设置随机密码 (建议不设置随机密码 )
    /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf  --initialize-insecure


    cat  /data/mysql/mysql3306/logs/error.log|grep -iE 'ERROR'
    没有error


    10. 启动 & 关闭  如果是多实例 分开多个启动脚本 /etc/init.d/mysql3306  /etc/init.d/mysql3307
    修改启动脚本 vi  /etc/init.d/mysql
    datadir=/data/mysql/mysql3306/data
    basedir=/usr/local/mysql





    /etc/init.d/mysql start or service mysql start
    /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
    /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &

    启动了之后才生成auto.cnf文件

    关闭:
    /etc/init.d/mysql stop
    /usr/local/mysql/bin/mysqladmin -S   /data/mysql/mysql3306/tmp/mysql.sock  shutdown

    11. 安全加固
    连接mysql:mysql -uroot -p -S /data/mysql/mysql3306/tmp/mysql.sock
    --不设置随机密码跟5.6一样,不输入密码直接登录mysql
    --设置随机密码,连接mysql的时候要输入随机密码才能登录mysql,并且需要修改密码才能让你进行下一步操作
    -------------------------------------------------------------------------
    5.7不用做
    delete from mysql.user where user!='root' or host!='localhost';
    truncate table mysql.db; #对test库没有授权,直接可以连接进来test库
    drop database test;
    -------------------------------------------------------------------------
    修改密码:
    mysqladmin -u USER -p password PASSWORD
    或者
    use mysql;
    UPDATE user SET password=PASSWORD('123456') WHERE user='root'; 5.6
    flush privileges;
    或者
    use mysql;
    UPDATE user SET authentication_string=PASSWORD('123456') WHERE user='root'; 5.7
    flush privileges;
    或者
    5.7新增方法
    alter user user() identified by '123456' ;

    alter user root@localhost identified by '123456' ;


    select user();
    +----------------+
    | user()         |
    +----------------+
    | root@localhost |
    +----------------+

    12. iptables开放mysql端口




    怎么确认mysql是启动起来的?
    A 查看进程是否存在
    ps axu |grep mysqld

    ps -eo ppid,pid,user,args |grep mysql



    B 查看端口是不是监听正确
    netstat -nalp |grep mysqld
    ss -lnpt |grep mysqld

    C 看看错误日志有无异常
    vim shit+g
    tail



    sock 是unix domain的一种通信方式,进程间通信


    rpm,二进制包,源码包 安装优劣性   rpm包和二进制包都是从源码包得来的
    生产环境用二进制,源码包的性能提升基本上没有提升相对于二进制,所以可以用二进制
    mysql官网都推荐用二进制

     

    mysql5.7.20之前默认会生成两个用户
    root@localhost
    mysql.sys@localhost

    mysql5.7.20以及之后默认会生成三个用户
    root@localhost
    mysql.sys@localhost
    mysql.session@localhost


    个性化设置 MySQL prompt 提示符
     http://blog.itpub.net/26515977/viewspace-1208304/

    itpub文章里的附录

    附录:
    Option Description
    c A counter that increments for each statement you issue
    D The full current date
    d The default database
    h The server host
    l The current delimiter (new in 5.0.25)
    m Minutes of the current time
    A newline character
    O The current month in three-letter format (Jan, Feb, …)
    o The current month in numeric format
    P am/pm
    p The current TCP/IP port or socket file
    R The current time, in 24-hour military time (0–23)
    The current time, standard 12-hour time (1–12)
    S Semicolon
    s Seconds of the current time
    A tab character
    U
    Your full user_name@host_name account name
    u Your user name
    v The server version
    w The current day of the week in three-letter format (Mon, Tue, …)
    Y The current year, four digits
    y The current year, two digits
    \_ A space
    A space (a space follows the backslash)
    \' Single quote
    " Double quote
    \ A literal “” backslash character
    x
    x, for any “x” not listed above


    cd /usr/local/mysql/再执行./scripts/mysql_install_db的原因

    ./ 表示 你在mysql目录里,如果你不 cd到mysql目录,那就找不到./bin/my_print_defaults文件
    所以
    先  cd /usr/local/mysql/
    再  ./scripts/mysql_install_db  --defaults-file=/etc/my.cnf

    f

  • 相关阅读:
    CSS-常用hack
    CSS触发haslayout的方法
    CSS最大最小宽高兼容
    CSS-文字超出自动显示省略号
    [LeetCode][JavaScript]Number of Islands
    [LeetCode][JavaScript]Search a 2D Matrix II
    [LeetCode][JavaScript]Search a 2D Matrix
    [LeetCode][JavaScript]Candy
    [LeetCode][JavaScript]Wildcard Matching
    [LeetCode][JavaScript]Sliding Window Maximum
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/5359311.html
Copyright © 2011-2022 走看看