centos7 安装 mysql

来源:互联网 发布:java 判断是不是汉字 编辑:程序博客网 时间:2024/03/29 04:09
1.创建相关目录
    Mysql目录安装位置:/usr/local/mysql
    数据库保存位置:/data/mysql         必须创建 不然会报错
    日志保存位置:/data/log/mysql      必须创建 不然会报错
    安装必要组件:yum -y install gcc*  libaio  重要
    关闭防火墙                       重要,不然外部没办法访问mysql
    mysql 启动、重启命令:service mysql start       后面没有带d
    mysql配置文件所在位置:/etc/my.cnf 
    mysql启动文件所在位置:/etc/init.d/mysql
2.下载mysql
    选择社区版,然后选择 mysql 版本 选择 linux 通用版
    可以直接使用 wget 下载

    下载完成以后,解压到 /usr/local/  目录下 将文件名修改为 mysql

    

3.创建 mysql 用户和mysql组
    useradd mysql
    groupadd mysql
4.改变mysql安装目录所有者
    cd /usr/local/mysql
    chown -R mysql .    (注意后面还有个点)  改变目录所有
    chgrp -R mysql .    改变目录所有组

5.配置参数
    cd /usr/local/mysql
    bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

    

    此处需要注意记录生成的临时密码,如上文:YLi>7ecpe;YP
    bin/mysql_ssl_rsa_setup  --datadir=/data/mysql

6.修改mysql配置文件和mysql启动文件
    cd  /usr/local/mysql/support-files
    cp my-default.cnf /etc/my.cnf           ( mysql 配置文件 )
    cp mysql.server /etc/init.d/mysql       ( mysql 启动脚本 )

    
    vim /etc/init.d/mysql   修改mysql启动文件


    vim /etc/my.cnf         修改mysql配置文件

    

# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[client]port = 3306socket = /usr/local/mysql/mysql.sock# character-set-server = utf8[mysql]no-auto-rehash[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....# socket = .....socket = /usr/local/mysql/mysql.sockbasedir = /usr/local/mysqlmax_allowed_packet = 32Mdatadir = /data/mysqlexplicit_defaults_for_timestamp = trueskip-sslsecure-file-priv = NULLlower_case_table_names = 1back_log = 300max_connections = 3000max_connect_errors = 100table_open_cache = 4096external-locking = FALSEmax_allowed_packet = 32Msort_buffer_size = 16Mjoin_buffer_size = 16Mthread_cache_size = 16query_cache_size = 128Mquery_cache_limit = 4Mft_min_word_len = 8thread_stack = 512Ktransaction_isolation = REPEATABLE-READtmp_table_size = 128Mmax_heap_table_size = 128M###*** slow query parameterslong_query_time = 6slow_query_logslow_query_log_file = /data/log/mysql/slow.log[mysqldump]quickmax_allowed_packet = 32M[mysqld_safe]open-files-limit = 8192log-error=/data/log/mysql/mysql_3306.err# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

7.启动mysql
    cd /usr/local/mysql
    bin/mysqld_safe --user=mysql &  (启动mysql)
    bin/mysql --user=root –p            (输入第5步生成的临时密码)

    此时会成功进入mysql
    mysql> set password=password('root');           (将root帐户密码设为root)
    mysql>grant all privileges on *.* to root@'%' identified by 'root';  (允许root执行所有命令)
    mysql> flush privileges;        (立刻生效)
8.将 mysql 命令 添加到环境变量
    vim /etc/profile
    添加:export PATH=/usr/local/mysql/bin:$PATH

    

    source /etc/profile    ( 添加的环境变量立刻生效 )
9.开机启动mysql
    chmod 755 /etc/init.d/mysql
    chkconfig --add mysql
    chkconfig --level 345 mysql on