1. HTTP3W博客首页
  2. 技术
  3. Linux

linux-centos7安装mysql5.6

网上好多资料在安装过程中容易出现各种问题,本实例亲测无坑傻瓜式安装,在此记录以备后用

1、下载mysql5.6免安装版本

# cd /root
# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz

2、查看并卸载老版本MySQL

查找并删除mysql相关文件

find / -name mysql

rm –rf 上边查找到的路径,多个路径用空格隔开

#或者下边一条命令即可

find / -name mysql | xargs rm –rf

说明:/usr/local/mysql 为我前期创建的所需安装路径目录

linux-centos7安装mysql5.6

备注:本服务器为新服务器,检测路径如上,即可忽略步骤1

3、解压mysql5.6到/usr/local/mysql

# tar xf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql

4、添加mysql用户组和mysql用户

先检查是否有mysql用户组和mysql用户

 groups mysql
linux-centos7安装mysql5.6

若无则添加

groupadd mysql
useradd -r -g mysql mysql
linux-centos7安装mysql5.6

若有,则跳过

5、创建mysql用户

# groupadd mysql 
# useradd -r -g mysql mysql
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysql /data/mysql5.6

6、初始化mysql

# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql5.6/ --basedir=/usr/local/mysql/

7、修改配置文件

vi /usr/local/mysql/my.cnf
  • 填入如下内容
[mysqld]
 basedir = /usr/local/mysql/
 datadir = /data/mysql5.6/
 port = 3307
 socket = /data/mysql5.6/mysql.sock
 innodb_file_per_table=1
 default-storage-engine=INNODB
 explicit_defaults_for_timestamp=true
 symbolic-links=0
 max_connections=1000

[mysqld_safe]
log-error=/data/mysql5.6/mysqld.log
pid-file=/data/mysql5.6/mysqld.pid
  • 备份配置my.cnf,将上面配置文件拷贝到/etc目录下
# mv /etc/my.cnf /etc/my.cnfbak
# cp /usr/local/mysql/my.cnf /etc/my.cnf
# vim /etc/profile
  • 配置环境变量/etc/profile,填写如下内容
export MYSQL_HOME="/usr/local/mysql/"
export PATH="$PATH:$MYSQL_HOME/bin"
  • 保存配置
# source /etc/profile

8、开启mysql进程

法一、

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql5.6
# chkconfig --add mysql5.6
# chkconfig mysql5.6 on
# service mysql5.6 start

法二、

# /usr/local/mysql/bin/mysqld_safe --user=mysql  --defaults-file=/usr/local/mysql/my.cnf --datadir=/data/mysql5.6/ --basedir=/usr/local/mysql5.6/  &

9、新建远程用户并修改密码

# ln -s /data/mysql5.6/mysql.sock /tmp/mysql.sock
# /usr/local/mysql/bin/mysql -uroot -P3307
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> update mysql.user set password=password('password') where User="root" and Host="localhost";
mysql> update mysql.user set password=password('password') where User="root" and Host="127.0.0.1";
mysql> update mysql.user set password=password('password') where User="root" and Host="hostname";
mysql> update mysql.user set password=password('password') where User="root" and Host="::1";
mysql> flush privileges;

附、一键安装脚本

#!/bin/bash
#=======================================================================#
#   System Supported:  CentOS 6+、7+                                    #
#   Description: mysql linux generic version Auto Installer             #
#   Author: Doubles <doubles159@163.com>                                #
#   Date: 2020-04-21                                                   #
#=======================================================================#

GROUP_NAME=mysql  
USER_NAME=mysql  
MYSQLDB_BASE_HOME="/usr/local/mysql"  
MYSQLDB_DATA_HOME="/data/mysql"
MYSQLDB_PORT=3307
MYSQL_VERSION="mysql-5.6.37-linux-glibc2.12-x86_64"
VERSION_NUM=`echo $MYSQL_VERSION|awk -F - '{print $2}'`
MYSQL_START_SCRIPTNAME=mysql
ERROR_EXIT=65

#==============================
# Function--> echo motd
#==============================
Echo_motd()
{
    echo "#=======================================================================#"
    echo "# Setup $MYSQL_VERSION on your system.                                  #"                               
    echo "# You will input mysql's root password later.                           #"
    echo "#=======================================================================#"
    sleep 1
}

#==============================
# Function--> add user and group
#==============================
Add_user()
{
    # check if user is root
    if [ $(id -u) != "0" ];then  
        echo "Error: You must be root to run this script!"  
        exit 1  
    fi

    # addGroup    
    if [ -z $(cat /etc/group|awk -F: '{print $1}'| grep -w "$GROUP_NAME") ]  
    then  
         groupadd -g 27 $GROUP_NAME  
        if(( $? == 0 ))  
        then  
            echo "group $GROUP_NAME add sucessfully!"  
        fi     
    else  
        echo "$GROUP_NAME is exsits"  
    fi   

    # addUser    
    if [ -z $(cat /etc/passwd|awk -F: '{print $1}'| grep -w "$USE_NAME") ]  
    then  
        adduser  -u 27 -g $GROUP_NAME $USER_NAME  
        if (( $? == 0 ))  
        then  
            echo "user $USER_NAME add sucessfully!"  
        fi  
    else  
        echo "$USER_NAME is exsits"  
    fi
}

#==============================
# Function-->download mysql
#==============================
Down_mysql()
{
    # download mysql  
    rm -rf /tmp/${MYSQL_VERSION}*  
    wget https://dev.mysql.com/get/Downloads/MySQL-5.6/${MYSQL_VERSION}.tar.gz -P /tmp  
    if(( $? == 0 ))  
    then   
        echo "MySQL DownLoad sucessfully!"   
    else   
        echo "MySQL DownLoad failed!"  
        exit $ERROR_EXIT  
    fi  
}
#==============================
# Function-->create basedir and datadir
#==============================
Add_dir()
{    
    cd /tmp  
    tar xzvf ${MYSQL_VERSION}.tar.gz  
    # if the basedir ${MYSQLDB_BASE_HOME} is exsits.....
    if [ -d "${MYSQLDB_BASE_HOME}" ]
    then
        echo "The basedir ${MYSQLDB_BASE_HOME} is exsits"
        DIR_NUM=1
        MYSQLDB_BASE_HOME_TMP="${MYSQLDB_BASE_HOME}-$VERSION_NUM"
        MYSQLDB_DATA_HOME_TMP="${MYSQLDB_DATA_HOME}-$VERSION_NUM"
        MYSQL_START_SCRIPTNAME_TMP="${MYSQL_START_SCRIPTNAME}-$VERSION_NUM"
        MYSQLDB_PORT=$((MYSQLDB_PORT + 1))
    while [ -d  $MYSQLDB_BASE_HOME_TMP ]
    do
        echo "The basedir ${MYSQLDB_BASE_HOME_TMP} also exsits"
        MYSQLDB_BASE_HOME_TMP="${MYSQLDB_BASE_HOME_TMP}_${DIR_NUM}"
        MYSQLDB_DATA_HOME_TMP="${MYSQLDB_DATA_HOME_TMP}_${DIR_NUM}"
        MYSQL_START_SCRIPTNAME_TMP="${MYSQL_START_SCRIPTNAME_TMP}_${DIR_NUM}"
        DIR_NUM=$((DIR_NUM + 1))
    done
    MYSQLDB_BASE_HOME=$MYSQLDB_BASE_HOME_TMP
    MYSQLDB_DATA_HOME=$MYSQLDB_DATA_HOME_TMP
    MYSQL_START_SCRIPTNAME=$MYSQL_START_SCRIPTNAME_TMP
        mv ${MYSQL_VERSION} ${MYSQLDB_BASE_HOME}
    else
        mv ${MYSQL_VERSION} ${MYSQLDB_BASE_HOME}
    fi
    # if the datadir ${MYSQLDB_DATA_HOME} is exsits.....
    if [ -d "${MYSQLDB_DATA_HOME}" ]
    then
        echo "The datadir $MYSQLDB_DATA_HOME also exsits,please modify the datadir..."
    exit $ERROR_EXIT
    else
        mkdir -p ${MYSQLDB_DATA_HOME}
    fi

    # add privileges to the basedir and datadir
    chown -R $USER_NAME:$GROUP_NAME ${MYSQLDB_BASE_HOME}
    chown -R $USER_NAME:$GROUP_NAME ${MYSQLDB_DATA_HOME}
}

#==============================
# Function-->install mysql
#==============================
Install_mysql()
{
    yum install -y perl-Module-Install.noarch
    # initialize the mysql
    ${MYSQLDB_BASE_HOME}/scripts/mysql_install_db --user=$USER_NAME --datadir=${MYSQLDB_DATA_HOME} --basedir=${MYSQLDB_BASE_HOME}
    if(( $? == 0 ))
    then
        echo "MySQL Initialize sucessfully!"   
    else
        echo "MySQL Initialize failed!"  
        exit $ERROR_EXIT
    fi

    # modify the my.cnf
    cd ${MYSQLDB_BASE_HOME}
    if [ -s my.cnf ];then
        echo "the my.cnf is exsits,it will be overwrite!"
        echo "
[mysqld]
 basedir = $MYSQLDB_BASE_HOME
 datadir = $MYSQLDB_DATA_HOME
 port = $MYSQLDB_PORT
 socket = ${MYSQLDB_DATA_HOME}/mysql.sock
 innodb_file_per_table=1
 default-storage-engine=INNODB
 explicit_defaults_for_timestamp=true
 symbolic-links=0
 max_connections=1000

[mysqld_safe]
log-error=${MYSQLDB_DATA_HOME}/mysqld.log
pid-file=${MYSQLDB_DATA_HOME}/mysqld.pid" >./my.cnf
    fi
    [ $? -eq 0 ] && echo "my.cnf created successfull!!"
    # add the bin to the path
    echo "add the $MYSQLDB_BASE_HOME/bin to the path!"
    cat >> /etc/profile <<EOF  
export PATH="$PATH:${MYSQLDB_BASE_HOME}/bin"
EOF
    source /etc/profile
}
#==============================
# Function-->start mysql
#==============================
Start_mysql()
{
    # add the service to the system service directory
    echo "Add the service to the system as mysql5.6,you can start the service by the command 'service mysql5.6 start' !"
    cp ${MYSQLDB_BASE_HOME}/support-files/mysql.server /etc/init.d/$MYSQL_START_SCRIPTNAME
    sed -i "s!^basedir=.*!basedir=$MYSQLDB_BASE_HOME!g" /etc/init.d/$MYSQL_START_SCRIPTNAME
    sed -i "s!^datadir=.*!datadir=$MYSQLDB_DATA_HOME!g" /etc/init.d/$MYSQL_START_SCRIPTNAME
    # add to the system service control and it will auto start when the system restart!
    chkconfig --add $MYSQL_START_SCRIPTNAME
    chkconfig $MYSQL_START_SCRIPTNAME on
    service $MYSQL_START_SCRIPTNAME start
}
#==============================
# Function-->create mysql's user and set password
#==============================
Create_user()
{
    # add the symbol link for the mysql.sock
    ln -s ${MYSQLDB_DATA_HOME}/mysql.sock /tmp/mysql.sock
    if(( $? == 0 ))
    then
        MYSQL_CONNECT_PARAMETER="-uroot  -t"   
    else
        MYSQL_CONNECT_PARAMETER="-uroot  -t -S ${MYSQLDB_DATA_HOME}/mysql.sock"  
    fi
    # create mysql's user and set root's password
    read -p "Input Default Mysql user root's password: " MYSQL_PASSWORD
    HOST_NAME=`hostname`
    ${MYSQLDB_BASE_HOME}/bin/mysql $MYSQL_CONNECT_PARAMETER  <<EOF 
    select Host,User,Password from mysql.user;
    CREATE USER 'root'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';
    GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' WITH GRANT OPTION;
    update mysql.user set password=password('$MYSQL_PASSWORD') where User="root" and Host="localhost";
    update mysql.user set password=password('$MYSQL_PASSWORD') where User="root" and Host="127.0.0.1";
    update mysql.user set password=password('$MYSQL_PASSWORD') where User="root" and Host="::1";
    update mysql.user set password=password('$MYSQL_PASSWORD') where User="root" and Host="$HOST_NAME";
    delete from mysql.user where user='';
    flush privileges;
    select Host,User,Password from mysql.user;
EOF
}

#==============================
# Function-->main function,the entrance of the script
#==============================
Main()
{   
    Echo_motd;
    Add_user;
    Down_mysql;
    Add_dir;
    Install_mysql;
    Start_mysql;
    Create_user;
}

Main;

1、原创文章,作者:诺米,如若转载,请注明出处:https://www.http3w.com/archives/228

2、本站内容若有雷同从属巧合,若侵犯了您的权益,请联系本站删除,E-mail: wtao219@qq.com

发表评论

您的电子邮箱地址不会被公开。

联系我们

254007489

在线咨询:点击这里给我发消息

邮件:wtao219@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息