快速实现MySQL的部署以及一机多实例部署

 更新时间:2016年4月22日 09:00  点击:2123

MySQL有三个版本:二进制,源码包,RPM。

下面讲讲二进制包的安装过程

下载地址:http://dev.mysql.com/downloads/mysql/

选择Linux-Generic

我这里选择的是mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz

解压后,里面有个文件INSTALL-BINARY,其实给出了二进制包的部署过程

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server 

相对于实际生产环境的部署,上面在初始化数据库的过程中少了一步-即指定配置文件,如果配置文件确认了,数据目录,日志目录都确认了,MySQL二进制版本的部署还是相当容易的一件事情。

下面写了一个脚本,基于后面提供的配置文件,执行格式如下:

sh 4.sh /root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz /mysql3306 3306

其中 4.sh是脚本,/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz是二进制包的绝对路径,/mysql3306是basedir,3306是需设置的端口,

利用该脚本,只需要预先定义好配置文件,就可进行MySQL数据库的快速部署以及一台服务器上多个实例的部署。

#!/bin/bash
#需传入三个参数,第一个是mysql二进制压缩包的路径(绝对路径),譬如/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz,
#第二个是mysql的basedir,即需要创建在哪个目录下,第三个是设置的端口号
filename=$1
basedir=$2
port=$3

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local
tar zxvf $filename
#file是获取mysql二进制包的名称,譬如mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz
#dir是mysql压缩包的路径,不含包名本身,譬如/root,因为后续的配置文件my.cnf也是放到这个路径下
file=`basename $filename`
dir=`dirname $filename`

#获取解压后的名字,即mysql-5.6.28-linux-glibc2.5-x86_64
after_tar_file=${file:0:-7}
#将二进制包改名为 mysql+端口号,这样也便于后续的区分
mv $after_tar_file mysql"$port"
cd mysql"$port"

#将原始的配置文件(需和mysql压缩包放到同层目录下,在本例中是/root/my.cnf)copy到解压并改名后的mysql二进制目录下,修改为my+端口号.cnf
cp $dir/my.cnf ./my"$port".cnf
user_cnf=my"$port".cnf
#下面主要是将原始配置文件中的路径修改为自己设定的路径,即传入的第二个参数
#整个的挑战在于传入的路径带有"/",在sed替换时会有问题,所有用了一个取巧的思路,即先将"/"替换为"|",进行sed替换,然后再将文件中的"|"修改回"/"
basedir_new=${basedir/\//|}
sed -i "s/\/project\/class2/$basedir_new/g" $user_cnf
sed -i "s/|/\//g" $user_cnf

#设置server_id,取当前的秒值
server_id=`date +%s`
sed -i /^server_id/s/.*/server_id="$server_id"/ $user_cnf
#设置端口号
sed -i /^port/s/.*/port="$port"/ $user_cnf

#创建必要的目录并修改权限
mkdir -p "$basedir"/mysql/{run,data,share,log,tmp}

chown -R mysql $basedir
chgrp -R mysql $basedir

#下面这个是非必要的,具体看后面的总结
cp share/english/errmsg.sys "$basedir"/mysql/share/

#初始化时--force也是非必要的,具体可见后面的总结
scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --force
bin/mysqld_safe --defaults-file="$user_cnf" --user=mysql &

下面给出了配置文件的一个参考,大家可根据实际情况进行相应的修改

[mysqld_safe]
pid-file=/project/class2/mysql/run/mysqld.pid

[mysql]
port=3306
prompt=\\u@\\d \\r:\\m:\\s>
default-character-set=utf8
no-auto-rehash

[client]
port=3306
socket=/project/class2/mysql/run/mysql.sock

[mysqld]
#dir
basedir=/project/class2/mysql
datadir=/project/class2/mysql/data
tmpdir=/tmp
lc_messages_dir=/project/class2/mysql/share
log-error=/project/class2/mysql/log/alert.log
slow_query_log_file=/project/class2/mysql/log/slow.log
general_log_file=/project/class2/mysql/log/general.log
socket=/project/class2/mysql/run/mysql.sock

#innodb
innodb_data_home_dir=/project/class2/mysql/data
innodb_log_group_home_dir=/project/class2/mysql/data
innodb_data_file_path=ibdata1:2G;ibdata2:16M:autoextend
innodb_buffer_pool_size=10G
innodb_buffer_pool_instances=4
innodb_log_files_in_group=2
innodb_log_file_size=1G
innodb_log_buffer_size=200M
innodb_flush_log_at_trx_commit=1
innodb_additional_mem_pool_size=20M
innodb_max_dirty_pages_pct=60
innodb_io_capacity=1000
innodb_thread_concurrency=16
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_open_files=60000
innodb_file_format=Barracuda
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_change_buffering=inserts
innodb_adaptive_flushing=1
innodb_old_blocks_time=1000
innodb_stats_on_metadata=0
innodb_read_ahead=0
innodb_use_native_aio=0
innodb_lock_wait_timeout=5
innodb_rollback_on_timeout=0
innodb_purge_threads=1
innodb_strict_mode=1
transaction-isolation=READ-COMMITTED

#myisam
key_buffer=64M
myisam_sort_buffer_size=64M
concurrent_insert=2
delayed_insert_timeout=300

#replication
master-info-file=/project/class2/mysql/log/master.info
relay-log=/project/class2/mysql/log/relaylog
relay_log_info_file=/project/class2/mysql/log/relay-log.info
relay-log-index=/project/class2/mysql/log/mysqld-relay-bin.index
slave_load_tmpdir=/project/class2/mysql/tmp
slave_type_conversions="ALL_NON_LOSSY"
slave_net_timeout=4
skip-slave-start
sync_master_info=1000
sync_relay_log_info=1000

#binlog
log-bin=/project/class2/mysql/log/mysql-bin
server_id=2552763370
binlog_cache_size=32K
max_binlog_cache_size=2G
max_binlog_size=500M
binlog-format=ROW
sync_binlog=1000
log-slave-updates=1
expire_logs_days=0

#server
default-storage-engine=INNODB
character-set-server=utf8
lower_case_table_names=1
skip-external-locking
open_files_limit=65536
safe-user-create
local-infile=1
#sqlmod="STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"

log_slow_admin_statements=1
log_warnings=1
long_query_time=1
slow_query_log=1
general_log=0

query_cache_type=0
query_cache_limit=1M
query_cache_min_res_unit=1K

table_definition_cache=65536

thread_stack=512K
thread_cache_size=256
read_rnd_buffer_size=128K
sort_buffer_size=256K
join_buffer_size=128K
read_buffer_size=128K

port=3306
skip-name-resolve
skip-ssl
max_connections=4500
max_user_connections=4000
max_connect_errors=65536
max_allowed_packet=128M
connect_timeout=8
net_read_timeout=30
net_write_timeout=60
back_log=1024

总结:

在初始化的过程中,如果报以下错误:

FATAL ERROR: Neither host 'keepalived02' nor 'localhost' could be looked up with
/mysql3306/mysql/bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option

但是在bash终端上执行hostname命令又确实有值返回,可加--force参数,如下所示:

复制代码 代码如下:
scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --force

如果报以下错误:

复制代码 代码如下:
[ERROR] Can't find messagefile '/mysql3306/mysql/share/errmsg.sys'

可将二进制版本中share/english/errmsg.sys文件COPY到/mysql3306/mysql/share/下。

后续:这两个错误的原因都是因为basedir修改了,它默认是在二进制包中查找的。

如何利用脚本实现MySQL的快速部署以及一机多实例的部署,通过这篇文章希望对大家学习MySQL的部署有所帮助。

[!--infotagslink--]

相关文章

  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • 详解Mysql中的JSON系列操作函数

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • 深入研究mysql中的varchar和limit(容易被忽略的知识)

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • mysql的3种分表方案

    一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Centos5.5中安装Mysql5.5过程分享

    这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅Mysql5.5.37安装需要cmake,5.6版本开始都需要cmake来编译,5.5以后的版本应该也要装这个。安装cmake复制代码 代码如下: [root@local ~]# wget http://www.cm...2015-03-15
  • 用VirtualBox构建MySQL测试环境

    宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Atheros AR8131 PCI-E Gigabit Ethernet Controller上网没问题。 宿主机使用无线的时候,客户机在Bridged Adapter模式下,使用可选项里唯一一个WIFI选项,Microsoft Virtual Wifi Miniport Adapter也无法上网,故弃之。...2013-09-19
  • 忘记MYSQL密码的6种常用解决方法总结

    首先要声明一点,大部分情况下,修改MySQL密码是需要有mysql里的root权限的...2013-09-11
  • MySQL数据库备份还原方法

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:/Program Files/MySQL/MySQL Server 4.1/bin (或者直接将windows的环境变量path中添加该目录) ...2013-09-26
  • Mysql命令大全(详细篇)

    一、连接Mysql格式: mysql -h主机地址 -u用户名 -p用户密码1、连接到本机上的MYSQL。首先打开DOS窗口,然后进入目录mysql/bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是密...2015-11-08
  • Navicat for MySQL 11注册码\激活码汇总

    Navicat for MySQL注册码用来激活 Navicat for MySQL 软件,只要拥有 Navicat 注册码就能激活相应的 Navicat 产品。这篇文章主要介绍了Navicat for MySQL 11注册码\激活码汇总,需要的朋友可以参考下...2020-11-23
  • mysql IS NULL使用索引案例讲解

    这篇文章主要介绍了mysql IS NULL使用索引案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-14
  • node.js如何操作MySQL数据库

    这篇文章主要介绍了node.js如何操作MySQL数据库,帮助大家更好的进行web开发,感兴趣的朋友可以了解下...2020-10-29
  • 基于PostgreSQL和mysql数据类型对比兼容

    这篇文章主要介绍了基于PostgreSQL和mysql数据类型对比兼容,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-25
  • RHEL6.5编译安装MySQL5.6.26教程

    一、准备编译环境,安装所需依赖包yum groupinstall 'Development' -y yum install openssl openssl-devel zlib zlib-devel -y yum install readline-devel pcre-devel ncurses-devel bison-devel cmake -y二、编译安...2015-10-21
  • Mysql中 show table status 获取表信息的方法

    这篇文章主要介绍了Mysql中 show table status 获取表信息的方法的相关资料,需要的朋友可以参考下...2016-03-12
  • 20分钟MySQL基础入门

    这篇文章主要为大家分享了20分钟MySQL基础入门教程,快速掌握MySQL基础知识,真正了解MySQL,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-12-02
  • mongodb与mysql命令详细对比

    传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关...2013-09-11
  • Delphi远程连接Mysql的实现方法

    这篇文章主要介绍了Delphi远程连接Mysql的实现方法,需要的朋友可以参考下...2020-06-30