1. 程式人生 > >MySQL腳本安裝及配置

MySQL腳本安裝及配置

oot rac basedir 設置mysql mysql 但是 改密 ali pack

#!/bin/bash
pkill -9 mysql
date +%Y-%m-%d-%H-%M
#卸載系統自帶的Mysql
/bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
/bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps
/bin/rm -f /etc/my.cnf

#安裝編譯代碼需要的包
/usr/bin/yum -y install make wget gcc-c++ cmake bison-devel ncurses-devel libaio

echo "============================Install MySQL 5.7.22=================================="

#編譯安裝mysql5.7.22
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql -M -s /sbin/nologin

#下載MySQL安裝的版本
cd /opt/ && rm -rf /usr/local/mysql
wget -c https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
/bin/tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/ && mv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

#配置my.cnf
cat > /etc/my.cnf << EOF
[mysqld]
#服務端設置
skip-name-resolve
#設置3306端口
port = 3306
socket=/var/lib/mysql/mysql.sock

設置mysql的安裝目錄

basedir=/usr/local/mysql

設置mysql數據庫的數據的存放目錄

datadir=/data/app

允許最大連接數

max_connections=200

服務端使用的字符集默認為8比特編碼的latin1字符集

character-set-server=utf8

創建新表時將使用的默認存儲引擎

default-storage-engine=INNODB

#表名存儲在磁盤是小寫的,但是比較的時候是不區分大小寫
lower_case_table_names=1
#限制server接受的數據包大小
max_allowed_packet=20M

開啟慢查詢

#slow_query_log=on

#log
log-error=/usr/local/mysql/log/error.log
#log=/usr/local/mysql/log/mysql.log

執行超過2秒的sql會被log下來

#long_query_time=2
#將查詢返回較慢的語句進行記錄
#log-slow-queries= /usr/local/mysql/log/slowquery.log

更新查詢

#log-update=/var/lib/mysql/update.log

[mysql]

設置mysql客戶端默認字符集

default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

#mysql配置-在zabbix agent上操作
##添加帳戶登錄信息等,以便zabbix監控
#添加以下內容(賬戶密碼自己設置)
#user = mysqlcheck
#password = chechk
#[client]
#user = check
#password = chechk
#[mysqladmin]
#user = check
#password = chechk
EOF

#啟動mysql服務
cd /usr/local/
/bin/mkdir -p /data/app
/bin/mkdir -p /usr/local/mysql/log/
/bin/chown -R mysql.mysql /usr/local/mysql
/bin/chown -R mysql.mysql /data

cd /data/app/ && rm -rf *
echo "===================================== 初始化及配置 ==================================="
cd /usr/local/mysql/
./bin/mysqld --defaults-file=/etc/my.cnf --initialize
#到此數據庫安裝完畢!
/bin/chmod 755 /etc/my.cnf
/bin/mkdir -p /var/lib/mysql/
/bin/chown -R mysql:mysql /var/lib/mysql/
/bin/chown -R mysql:mysql /data/
/bin/chown -R mysql:mysql /usr/local/mysql

#設置開機自啟動服務控制腳本
cp ./support-files/mysql.server /etc/rc.d/init.d/mysql
/bin/chmod +x /etc/rc.d/init.d/mysql
chkconfig --add mysql

#將mysql的bin目錄加入PATH環境變量
cat >> /etc/profile <<EOF
export PATH=$PATH:/usr/local/mysql/bin
EOF

#啟動msql
#查看狀態
ps -ef|grep mysql

/sbin/chkconfig mysql on
/bin/chown -R mysql:mysql /data/
cd /etc/init.d && chmod +x mysql
/etc/init.d/mysql start

echo "===================================== 查看數據庫狀態 ==================================="
ps -ef|grep mysql && netstat -tunpl

echo "===================================== 修改密碼設置權限 ==================================="
#設置mysql登陸密碼,密碼為a123456
for i in grep ‘temporary password‘ /usr/local/mysql/log/error.log|awk ‘NR==1{print}‘|awk -F: ‘{print $NF}‘;
do
mysql -uroot -p$i -e "SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘a123456‘);" -b --connect-expired-password
#設置遠程主機登錄,註意:如果是生產環境不介意開啟root遠程登陸(安全問題)
mysql -uroot -p‘a123456‘ -e "GRANT ALL PRIVILEGES ON . TO ‘root‘@‘%‘ IDENTIFIED BY ‘a123456‘ WITH GRANT OPTION;"
mysql -uroot -p‘a123456‘ -e "FLUSH PRIVILEGES;"

done

echo "===================================== 重啟MySQL ==================================="
/etc/init.d/mysql restart

MySQL腳本安裝及配置