欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux服务器下MariaDB 10自动化安装部署

程序员文章站 2022-07-04 17:59:41
去mariadb官网下载mariadb本文用的是mariadb 10.1.16 https://downloads.mariadb.org 选择二进制版本,下载到/ro...

去mariadb官网下载mariadb本文用的是mariadb 10.1.16
https://downloads.mariadb.org

选择二进制版本,下载到/root目录下
mariadb-10.1.16-linux-x86_64.tar.gz

开始安装
[root@he3 ~]# cat mariadb_auto_install.sh

###### 二进制自动安装数据库脚本root密码manager将脚本和安装包放在/root目录即可###############
######数据库目录/usr/local/mysql############
######数据目录/data/mysql############
######日志目录/log/mysql############
######端口号默认3306其余参数按需自行修改############
 
##################
#author:rrhelei@126.com#
##################
#!/bin/bash
path=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/bin
export path
 
# check if user isroot
if [ $(id -u) !="0" ]; then
  echo "error: you must be root to runthis script, please use root to install"
  exit 1
fi
 
clear
echo"========================================================================="
echo "a tool toauto-compile & install mariadb-10.1.16 on redhat/centos linux "
echo"========================================================================="
cur_dir=$(pwd)
 
#set mysql rootpassword
echo"==========================="
 
mysqlrootpwd="manager"
echo -e "please input the root password of mysql:"
read -p "(default password: manager):" mysqlrootpwd
if[ "$mysqlrootpwd" = "" ]; then
mysqlrootpwd="manager"
fi
echo"==========================="
echo"mysql root password:$mysqlrootpwd"
echo"==========================="
 
#which mysql versiondo you want to install?
echo"==========================="
 
isinstallmysql56="n"
echo"install mariadb-10.1.16,please input y"
read -p "(please input y , n):" isinstallmysql56
 
case"$isinstallmysql56" in
y|y|yes|yes|yes|yes|yes|yes|yes)
echo"you will install mariadb 10.1.16"
isinstallmysql56="y"
;;
*)
echo"input error,you will exit install mariadb 10.1.16"
isinstallmysql56="n"
  exit
esac
 
get_char()
{
savedstty=`stty-g`
stty-echo
sttycbreak
#ddif=/dev/tty bs=1 count=1 2> /dev/null
stty-raw
sttyecho
stty$savedstty
}
echo""
echo"press any key to start...or press ctrl+c to cancel"
char=`get_char`
 
# initialize the installation related content.
functioninitinstall()
{
cat/etc/issue
uname -a
memtotal=`free -m | grep mem | awk '{print $2}'` 
echo -e "\n memory is: ${memtotal} mb "
#settimezone
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/asia/shanghai /etc/localtime
 
 
  #delete old mysql program
rpm -qa|grep mysql
rpm -e mysql
 
#disableselinux
if[ -s /etc/selinux/config ]; then
sed -i 's/selinux=enforcing/selinux=disabled/g' /etc/selinux/config
fi
  setenforce 0
 
 
}
 
 
#installation ofdepend on and optimization options.
functioninstalldependsandopt()
{
cd $cur_dir
 
cat>>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
 
echo"fs.file-max=65535" >> /etc/sysctl.conf
}
 
#install mysql
functioninstallmysql56()
{
echo"============================install mariadb 10.1.16=================================="
cd $cur_dir
 
#backup old my.cnf
#rm -f /etc/my.cnf
if [ -s /etc/my.cnf]; then
  mv /etc/my.cnf /etc/my.cnf.`date+%y%m%d%h%m%s`.bak
fi
 
#mysql directoryconfiguration
groupadd mysql -g512
useradd -u 512 -gmysql -s /sbin/nologin -d /home/mysql mysql
tar xvf/root/mariadb-10.1.16-linux-x86_64.tar.gz
mv /root/mariadb-10.1.16-linux-x86_64 /usr/local/mysql
mkdir -p /data/mysql
mkdir -p /log/mysql
chown -r mysql:mysql/data/mysql
chown -r mysql:mysql/usr/local/mysql
chown -r mysql:mysql/log
 
serverid=`ifconfigeth0 | grep "inet addr" | awk '{ print $2}'| awk -f. '{ print$4"3306"}'`
cat>>/etc/my.cnf<<eof
[client]
port    =3306
socket    =/tmp/mysql.sock
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
port    =3306
socket    =/tmp/mysql.sock
basedir    =/usr/local/mysql
datadir    =/data/mysql
open_files_limit  = 3072
back_log = 103
max_connections =800
max_connect_errors =100000
table_open_cache =512
external-locking =false
max_allowed_packet =32m
sort_buffer_size =2m
join_buffer_size =2m
thread_cache_size =51
query_cache_size =32m
tmp_table_size = 96m
max_heap_table_size= 96m
slow_query_log = 1
slow_query_log_file= /log/mysql/slow.log
log-error =/log/mysql/error.log
long_query_time = 1
server-id =$serverid
log-bin =/log/mysql/mysql-bin
sync_binlog = 1
binlog_cache_size =4m
max_binlog_cache_size= 8m
max_binlog_size =1024m
expire_logs_days =60
key_buffer_size =32m
read_buffer_size =1m
read_rnd_buffer_size= 16m
bulk_insert_buffer_size= 64m
character-set-server=utf8
default-storage-engine= innodb
binlog_format = row
innodb_buffer_pool_dump_at_shutdown= 1
innodb_buffer_pool_load_at_startup= 1
binlog_rows_query_log_events= 1
explicit_defaults_for_timestamp= 1
 
#log_slave_updates=1
#gtid_mode=on
#enforce_gtid_consistency=1
 
#innodb_write_io_threads= 8
#innodb_read_io_threads= 8
#innodb_thread_concurrency= 0
 
transaction_isolation= repeatable-read
innodb_additional_mem_pool_size= 16m
innodb_buffer_pool_size= 512m
#innodb_data_home_dir=
innodb_data_file_path= ibdata1:1024m:autoextend
innodb_flush_log_at_trx_commit= 1
innodb_log_buffer_size= 16m
innodb_log_file_size= 512m
innodb_log_files_in_group= 2
innodb_max_dirty_pages_pct= 50
innodb_file_per_table= 1
innodb_locks_unsafe_for_binlog= 0
wait_timeout = 14400
interactive_timeout= 14400
skip-name-resolve
[mysqldump]
quick
max_allowed_packet =32m
 
eof
 
 
 
 
/usr/local/mysql/scripts/mysql_install_db--basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf--user=mysql
 
cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig --add mysql
chkconfig --leve l2345 mysqld on
 
cat >>/etc/ld.so.conf.d/mysql-x86_64.conf<<eof
/usr/local/mysql/lib
eof
ldconfig
 
if [ -d"/proc/vz" ];then
ulimit -s unlimited
fi
 
/etc/init.d/mysqldstart
 
 
cat >>/etc/profile <<eof
export path=$path:/usr/local/mysql/bin
export ld_library_path=/usr/local/mysql/lib
eof
 
 
/usr/local/mysql/bin/mysqladmin-u root password $mysqlrootpwd
 
cat >/tmp/mysql_sec_script<<eof
use mysql;
delete frommysql.user where user!='root' or host!='localhost';
flush privileges;
eof
 
/usr/local/mysql/bin/mysql-u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
 
#rm -f/tmp/mysql_sec_script
 
 
/etc/init.d/mysqldrestart
 
source /etc/profile
 
 
echo"============================mariadb 10.1.16 installcompleted========================="
}
 
 
functioncheckinstall()
{
echo"===================================== check install==================================="
clear
ismysql=""
echo"checking..."
 
if [ -s/usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ]&& [ -s /etc/my.cnf ]; then
 echo "mysql: ok"
 ismysql="ok"
 else
 echo "error: /usr/local/mysql notfound!!!mysql install failed."
fi
 
if ["$ismysql" = "ok" ]; then
echo "mariadb 10.1.16 completed! enjoy it."
echo"========================================================================="
netstat -ntl
else
echo"sorry,failed to install mysql!"
echo "you cantail /root/mysql-install.log from your server."
fi
}
 
#the installationlog
initinstall2>&1 | tee /root/mysql-install.log
checkanddownloadfiles2>&1 | tee -a /root/mysql-install.log
installdependsandopt2>&1 | tee -a /root/mysql-install.log
installmysql562>&1 | tee -a /root/mysql-install.log
checkinstall2>&1 | tee -a /root/mysql-install.log

[root@he3 ~]# mysql -uroot -p

enter password:
welcome to the mariadb monitor.  commands end with ; or \g.
your mariadb connection id is 6
server version: 10.1.16-mariadb mariadb server
copyright (c) 2000, 2016, oracle, mariadb corporation ab and others.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mariadb [(none)]> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

至此,mariadb-10.1.16安装成功