1. 程式人生 > >Mysql5.6資料庫安裝配置

Mysql5.6資料庫安裝配置

一. 安裝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、解壓mysql5.6到/usr/local/mysql5.6

# mkdir -p /usr/local/mysql5.6
# mkdir -p /data/mysql5.6
# 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/mysql5.6

3、建立mysql使用者

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

4、初始化mysql

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

5、修改配置檔案

cat /usr/local/mysql5.6/my.cnf

[mysqld]
 basedir = /usr/local/mysql5.6
/ 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 # mv /etc/my.cnf /etc/my.cnfbak
# cp /usr/local/mysql5.6/my.cnf /etc/my.cnf # vim /etc/profile export MYSQL_HOME="/usr/local/mysql5.6/" export PATH="$PATH:$MYSQL_HOME/bin" # source /etc/profile

6、開啟mysql程序

法一、
# cp /usr/local/mysql5.6/support-files/mysql.server /etc/init.d/mysql5.6
# chkconfig --add mysql5.6
# chkconfig mysql5.6 on
# service mysql5.6 start
法二、
# /usr/local/mysql5.6/bin/mysqld_safe --user=mysql  --defaults-file=/usr/local/mysql5.6/my.cnf --datadir=/data/mysql5.6/ --basedir=/usr/local/mysql5.6/  &

7、新建遠端使用者並修改密碼

# ln -s /data/mysql5.6/mysql.sock /tmp/mysql.sock
# /usr/local/mysql5.6/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 <[email protected]>                                #
#   Date: 2018-01-26                                                    #
#=======================================================================#

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;

相關推薦

Mysql5.6資料庫安裝配置

一. 安裝mysql5.6 1、下載mysql5.6免安裝版本 # cd /root # wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-

Mysql5.6 linux 安裝配置

安裝步驟 0.解除安裝老版本MySQL 查詢並刪除mysql有關的檔案 find / -name mysql rm -rf 上邊查詢到的路徑,多個路徑用空格隔開 或者下邊一條命令即可find / -name mysql|xargs rm -rf 1.在安裝包存放目錄下執行命令解壓

MySQL5.6 資料庫主從(Master/Slave)同步安裝配置詳解

安裝環境 作業系統 :CentOS 6.5 資料庫版本:MySQL 5.6.27 主機A:192.168.1.1 (Master) 主機B:192.168.1.2 (Slave) 這裡強調的資料庫的版本,是因為MySQL在5.6之前和之後的安裝方式是不

windows server 2008 64位MySQL5.6安裝版本配置說明

zip mage 系統 9.png 技術 版本 變量 成功 過程 1 通過官網下載MySQL5.6版本壓縮包,mysql-5.6.36-winx64.zip; 2 在D盤創建目錄,比如D:\MySQL,將mysql-5.6.36-winx64.zip解壓縮到該目錄下,如下

MySQL-5.6資料庫安裝配置

目錄 一、概述 一、概述   MySQL版本:5.7.17   客戶端工具:NavicatforMySQL  二、MySQL安裝  安裝條件:   如果Windows Server 2003 在安裝.net framewo

Redhat6.3上安裝配置mysql5.6.13版本配置埠及自定義資料檔案目錄問題

記錄一下自己在Redhat6.3上安裝配置mysql遇到的種種問題 首先下載檔案不用說了,網上鍊接很多 我用的的5.6.13 一共是三個檔案 MySQL-client-5.6.13-1.el6.x86_64.rpm MySQL-devel-5.6.13-1.el6.x8

CentOS安裝mysql5.6資料庫

下載安裝包 從這裡下載合適的版本http://dev.mysql.com/downloads/mysql/ wget http://cdn.mysql.com//Downloads/MySQL-5.6/MySQL-5.6.30-1.linux_glibc2.5.x86_64.rpm-

SonarQube5.6安裝+MySQL5.7資料庫安裝

重灌系統後,再一次裝sonarqube伺服器(以下簡稱SQ)。這裡記錄下步驟,供大家參考。 1.安裝 MySQL 為與SQ相容,我們使用MySQL5.6-5.7版本,若系統裡沒有MySQL,直接安裝即可;若有系統自帶的MySQL5.5或5.1等需要先進行解除安裝。 解除安裝

mysql5.7資料庫安裝完成後如何配置環境變數

如果我們在windows10系統中安裝mysql後,沒有配置環境變數的話,在使用命令列進行mysql操作時就必須要進入mysql安裝目錄才行。這樣操作起來感覺會非常麻煩。這該怎麼辦呢?win10系統中配置mysql環境變數的方法。 方法/步驟: 鍵盤組合鍵:Win+i,開啟設定視窗,點選“系統”,進入系統設

CentOS6.5 Linux安裝mysql5.6資料庫 針對64位系統

1.  下載 1.  安裝 1.1. 檢測是否已經安裝了mysql rpm -qa | grep mysql   如果已經安裝了,將其解除安裝,如: rpm -e --nodeps  mysql-libs-5.1.71-1.el6.i686 1.1. 安裝m

Confluence 6安裝配置資料庫字符集編碼

Confluence 和你的資料庫必須配置使用相同的字符集。 Confluence 使用 UTF-8 字符集編碼,所以你的資料庫也必須配置為使用 UTF-8 (或者與 UTF-8 相同的編碼,例如在 Oracle 中使用的 AL32UTF8 編碼)。 當你在第一次安裝 Confluence 的時候,你

mysql5.6資料庫同步,單雙多主多從配置

windows下MySQL5.6實現主從資料庫同步資料  mysql5.6資料庫同步,單向雙向同步問題 一.單向同步 主資料庫(mysql5.6)192.168.1.104 從資料庫(mys

CentOS7.2 安裝mysql5.6,並配置自動啟動和遠端訪問

   安裝之前,需要先解除安裝maiiadb資料庫(centos7自帶的mysql的另一個版本) ## 檢視本機是否安裝了mariadb資料庫 $ rpm -qa|grep mariadb            ## 上一步查出來的檔名,直接複製下來就OK $ rpm -e

【0008】mysql5.6安裝版下載配置教程

1、下載 2、解壓 解壓zip壓縮檔案到你想要的位置。我的解壓目錄是:D:\Install\mysql\mysql-5.6.36-winx64 3、配置  將解壓目錄下預設檔案 my-default.ini 改名 my.ini 儲存。 開啟my.

Linux --- CentOS 7 搭建MySQL5.6資料庫伺服器與配置詳解

Centos7將預設資料庫mysql替換成了Mariadb,如果想繼續使用mysql 需要解除安裝Mariadb 再安裝mysql; 1、解除安裝 MariaDB 檢視已安裝的程式 rpm -qa |grep -i mariadb 解除

centos6.5 配置mysql5.6安裝

1、從mysql官網下載mysql5.6 2、解壓檔案並移動到/usr/local/mysql #tar -xzvf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz     #mv mysql-5.6.30-linux-gli

CentOS 6.4安裝配置LNMP服務器(Nginx+PHP+MySQL)

mysql- ket 是否 分號 使用 store 重啟 isa tpi 一 安裝篇 1. 安裝nginx yum check-update #更新yum源 yum remove httpd* php* #刪除系統自帶的軟件包 yum install nginx #安裝ng

1.MySQL5.7.19 安裝配置踩過的坑

edi nor 切換 normal table 無法啟動 sql安裝 span 安裝配置 這篇文章主要是分享 安裝MySQL時遇到的一些問題,以及解決方法。 第一步:下載MySQL 下載地址:https://dev.mysql.com/downloads/mysql/5.1

centos6.5下的mysql5.6.30安裝

ack ble max 啟動 nal html name root base http://www.cnblogs.com/fujinzhou/p/5750442.html centos6.5下的mysql5.6.30安裝 1.解壓mysql tar -

CentOS 6 yum安裝配置lnmp服務器

設置密碼 客戶端 inux listening logs and cat 服務 load 一、準備: 1.配置防火墻,開啟80端口、3306端口編輯 /etc/sysconfig/iptables,添加如下條目: -A INPUT -m state --state