mysql一键执行脚本 超方便!!!

mysql一键执行脚本

  • mysql一键执行脚本

mysql一键执行脚本

  • 在初始虚拟机上,直接复制就ok了

#!/bin/bash

echo "-------编译安装mysqld 服务--------"
#将安装mysql 所需软件包传到/opt目录下
#mysql-5.7.17.tar.gz
#boost_1_59_0.tar.gz		#支持c++的运行库

echo "安装环境依赖包"
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

echo "配置软件模块"

tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz

cd /opt
mv boost_1_59_0 /usr/local/boost

cd /opt/mysql-5.7.17/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1


echo "编译及安装"
make -j 4 && make install

echo "创建mysql用户"
useradd -M -s /sbin/nologin  mysql

echo "修改mysql 配置文件"

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

echo "[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES "> /etc/my.cnf



echo "更改mysql安装目录和配置文件的属主属组"
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

echo "设置路径环境变量"
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile

echo "初始化数据库"
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

echo "添加mysqld系统服务"
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/		#用于systemctl服务管理
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
netstat -anpt | grep 3306

#修改密码
function abc {
passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password $passwd
expect "Enter password:" 
send "\r"

expect eof
EOF
}
abc "abc123"


function bcd {
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter password:" {send "abc123\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\r"}
expect "mysql>" {send "show databases;\r"}
expect "mysql>" {send "quit\r"}
expect eof
EOF
}
bcd

echo "安装完成"

你可能感兴趣的:(mysql,linux,centos,数据库)