MySQL编译安装及初始化

目录

编译安装cmake

编译安装MySQL

初始化配置

1.生成server_id

2.生成随机密码

3.生成初始化SQL

4.生成账号配置文件

5.初始化和启动MySQL


编译安装cmake

tar -zxvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
./configure --prefix=${target_dir}/cmake/tool
gmake -j8
make install

export PATH=$PATH:${target_dir}/cmake/tool/bin/

编译安装MySQL

#====================================================
# MySQL 5.6版本编译
# ${basepath}为MySQL安装的目录
#====================================================

export CFLAGS="-O3 -g"
export CXXFLAGS="-O3 -g"
cmake .. -DCMAKE_INSTALL_PREFIX=${basepath}\
    -DMYSQL_DATADIR=${data_path}\
    -DINSTALL_SBINDIR=libexec\
    -DINSTALL_LIBDIR=lib/mysql\
    -DSYSCONFDIR=${basepath}/etc\
    -DMYSQL_UNIX_ADDR=${basepath}/tmp/mysql.sock\
    -DINSTALL_PLUGINDIR=lib/plugin\
    -DINSTALL_SCRIPTDIR=bin\
    -DINSTALL_MYSQLSHAREDIR=share\
    -DINSTALL_SUPPORTFILESDIR=share/mysql\
    -DCMAKE_C_FLAGS='-O3 -g' \
    -DCMAKE_CXX_FLAGS='-O3 -g' \
    -DCMAKE_C_FLAGS_RELEASE='-O3 -g' \
    -DCMAKE_CXX_FLAGS_RELEASE='-O3 -g' \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_EXTRA_CHARSETS=all \
    -DWITH_UNIT_TESTS=0\
    -DWITH_DEBUG=0\
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1\
    -DWITH_INNODB_MEMCACHED=1\
    -DWITH_MYISAM_STORAGE_ENGINE=1\
    -DWITH_INNOBASE_STORAGE_ENGINE=1\
    -DWITH_ARCHIVE_STORAGE_ENGINE=0\
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DENABLED_PROFILING=1\
    -DWITH_ZLIB=bundled\
    -DENABLED_LOCAL_INFILE=1\
    -DCMAKE_SKIP_BUILD_RPATH=ON

make -j8
make install
export LD_LIBRARY_PATH=$basepath/lib/mysql:$LD_LIBRARY_PATH

初始化配置

1.生成server_id

4字节uint型,取值范围0-4294967295

server_id="$(ping -c 1 `hostname`|head -n 1|awk '{print $3}'|sed -r 's/[().\s]//g')$(date +%S)"
server_id=$(($server_id%4294967295))

2.生成随机密码

function generate_pwd()
{
    local MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@%^&*()_+="
    local LENGTH="30"
    local PASS=""
    while [ "${n:=1}" -le "$LENGTH" ]
    do
        PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
        let n+=1
    done
        echo "$PASS"
    exit 0
}

3.生成初始化SQL

local root_pwd=$(generate_pwd)
local admin_pwd=$(generate_pwd)

cat >$sql_file <<_EOF_
SET SQL_LOG_BIN=0;

-- clear dirty users
DELETE FROM mysql.user WHERE user='';
DELETE FROM mysql.db   WHERE user='';
DELETE FROM mysql.user WHERE host LIKE '%-%';
DELETE FROM mysql.db   WHERE host LIKE '%-%';

-- change password for root&&admin
UPDATE mysql.user SET password=PASSWORD('${root_pwd}') WHERE user='root';
UPDATE mysql.user SET password=PASSWORD('${admin_pwd}') WHERE user='admin';

-- create admin users
GRANT SELECT,RELOAD,PROCESS,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'admin'@'localhost' IDENTIFIED BY '${admin_pwd}' WITH GRANT OPTION;
GRANT SELECT,RELOAD,PROCESS,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '${admin_pwd}' WITH GRANT OPTION;
GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'mysqlsync'@'' IDENTIFIED BY 'mysqlsync' WITH GRANT OPTION;

-- reset privileges and replication status;
flush privileges;

-- clear dirty binlog
reset master;
reset slave;
_EOF_

4.生成账号配置文件

cat >etc/user.root.cnf <<_EOF_
[client]
user=root
password=$root_pwd
socket=$basepath/tmp/mysql.sock
_EOF_

cat >etc/user.admin.cnf <<_EOF_
[client]
user=admin
password=$admin_pwd
socket=$basepath/tmp/mysql.sock
_EOF_

5.初始化和启动MySQL

mkdir -pv ${target_dir}/{etc,log,tmp}

#==========================================================
# 生成配置文件etc/my.cnf和etc/mysqld.cnf
# 并根据需要配置MySQL参数,在此先略过具体参见MySQL配置
#==========================================================

chmod 644 $target_dir/etc/my.cnf 2>/dev/null
chmod 644 $target_dir/etc/my*.cnf 2>/dev/null
chmod 600 $target_dir/etc/user.*.cnf 2>/dev/null
chmod 700 $target_dir/var
chmod 755 $target_dir/{bin,log,etc,libexec}

cp -v $target_dir/share/mysql/mysql.server $bpath/bin/

$target_dir/bin/mysql_install_db --defaults-file=$target_dir/etc/my.cnf

#==========================================================
# 启动MySQL
#==========================================================
bin/mysqld_safe --defaults-file=etc/my.cnf --user="`whoami`" >/dev/null 2>&1 &
until [ -f var/mysql.pid ]
do
        sleep 1s
        echo "[...] wait mysql start up"
done
bin/mysql -uroot  < $sql_file

echo "[succ]Bingo (:"

 

你可能感兴趣的:(MySQL教程)