MySQL script installation and configuration

Source: Internet
Author: User
Tags chmod mysql client

#!/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

Set up the installation directory for MySQL

Basedir=/usr/local/mysql

Set up the data storage directory for the MySQL database

Datadir=/data/app

Maximum number of connections allowed

max_connections=200

The character set used by the service side defaults to the 8-bit encoded latin1 character set

Character-set-server=utf8

Default storage engine that will be used when creating a new table

Default-storage-engine=innodb
#表名存储在磁盘是小写的, but the comparison is case insensitive
Lower_case_table_names=1
#限制server接受的数据包大小
max_allowed_packet=20m

Turn on Slow query

#slow_query_log =on

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

SQL executed for more than 2 seconds will be log down

#long_query_time =2
#将查询返回较慢的语句进行记录
#log-slow-queries=/usr/local/mysql/log/slowquery.log

Update query

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

[MySQL]

Set the MySQL client default character set

Default-character-set=utf8
Socket=/var/lib/mysql/mysql.sock

#mysql配置-Operate on the Zabbix agent
# #添加帐户登录信息等, so Zabbix monitoring
#添加以下内容 (account password set by itself)
#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 "===================================== initialization and configuration ==================================="
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 "===================================== View database status ==================================="
Ps-ef|grep MySQL && NETSTAT-TUNPL

echo "===================================== Change password settings permission ==================================="
#设置mysql登陆密码, the password is 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
#设置远程主机登录, note: If the production environment does not mind to open the root remote login (security issues)
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 "===================================== restart MySQL ==================================="
/etc/init.d/mysql restart

MySQL script installation and configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.