cmake参数选项;同一主机安装2个mysql服务;
http://blog.csdn.net/zubin006/article/details/7059362
#mysql Mysql从5.5开始,源代码安装将原来的configure改为cmake,因此在安装mysql 5.5.x时,需要先安装cmake,可以通过源码安装,也可以通过apt-get软件包安装.
在用cmake配置mysql过程中,找到一些主要的参数说明如下,供参考:
CMAKE_INSTALL_PREFIX mysql的安装路径;
DEFAULT_CHARSET 默认的服务器编码,
DEFAULT_COLLATION 也是关于编码的
MYSQL_DATADIR data目录
MYSQL_TCP_PORT TCP/IP端口,3306
MYSQL_UNIX_ADDR unix的socket文件,默认/tmp/mysql.sock
WITH_xxx_STORAGE_ENGINE 编译静态的存储引擎xxx
WITHOUT_xxx_STORAGE_ENGINE Exclude storage engine xxx from build
WITH_EXTRA_CHARSETS 扩展字符集
WIHT_ZLIB zlib支持
常用的选项有下边这些:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \
#数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \
#Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \
#安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
#安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \
#安装数据库分区
-DENABLED_LOCAL_INFILE=1 \
#允许从本地导入数据
-DWITH_READLINE=1 \
#快捷键功能
-DWITH_SSL=yes \
#支持 SSL
-DDEFAULT_CHARSET=utf8 \
#使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \
#校验字符
-DEXTRA_CHARSETS=all \
#安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \
#MySQL 监听端口
http://waynerqiu.com/7/153.html
MySQL5.5之后就开始使用cMake来代替传统的configure了,很多朋友还一个劲儿的找configure 呵呵, 找不见就对了。
秋伟光的这篇文章主要整理了cMake的安装方法及参数详解,同时提供新老版本的参数对比,方便习惯了configure的朋友顺利过渡到cMake上
首先说一下安装方法,当然我只是给不会的朋友提醒一下,(*^__^*) 嘻嘻……
MySQL5.5/MySQL5.6的安装方法
其实安装方法和之前差不对, 只是用cmake代替了configure,参数发生了变化而已,详情请看以下代码:
cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DMYSQL_DATADIR=/db/mysql/data \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 make make install
好了这就算安装结束了,当然需要配置一些环境变量及文件软连接,自己弄哦
下面介绍一下参数
MySQL cMake 常规参数介绍
-DCMAKE_INSTALL_PREFIX= 指向mysql安装目录
-DINSTALL_SBINDIR=sbin 指向可执行文件目录(prefix/sbin)
-DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录(/var/lib/mysql)
-DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)
-DINSTALL_PLUGINDIR=lib/mysql/plugin 指向插件目录(prefix/lib/mysql/plugin)
-DINSTALL_MANDIR=share/man 指向man文档目录(prefix/share/man)
-DINSTALL_SHAREDIR=share 指向aclocal/mysql.m4安装目录(prefix/share)
-DINSTALL_LIBDIR=lib/mysql 指向对象代码库目录(prefix/lib/mysql)
-DINSTALL_INCLUDEDIR=include/mysql 指向头文件目录(prefix/include/mysql)
-DINSTALL_INFODIR=share/info 指向info文档存放目录(prefix/share/info)
prefix官方推荐设为/usr
MySQL cMake Storage Engine 相关参数详解
类型csv,myisam,myisammrg,heap,innobase,archive,blackhole
若想启用某个引擎的支持:-DWITH_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
若想禁用某个引擎的支持:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
MySQL cMake Library相关参数详解
-DWITH_READLINE=1 启用readline库支持(提供可编辑的命令行)
-DWITH_SSL=system 启用ssl库支持(安全套接层)
-DWITH_ZLIB=system 启用libz库支持(zib、gzib相关)
-DWTIH_LIBWRAP=0 禁用libwrap库(实现了通用TCP包装的功能,为网络服务守护进程使用)
-DMYSQL_TCP_PORT=3306 指定TCP端口为3306
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock 指定mysql.sock路径
-DENABLED_LOCAL_INFILE=1 启用本地数据导入支持
-DEXTRA_CHARSETS=all 启用额外的字符集类型(默认为all)
-DDEFAULT_CHARSET=utf8 指定默认的字符集为utf8
-DDEFAULT_COLLATION=utf8_general_ci 设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)
-DWITH_EMBEDDED_SERVER=1 编译嵌入式服务器支持
-DMYSQL_USER=mysql 指定mysql用户(默认为mysql)
-DWITH_DEBUG=0 禁用debug(默认为禁用)
-DENABLE_PROFILING=0 禁用Profiling分析(默认为开启)
-DWITH_COMMENT='string' 一个关于编译环境的描述性注释
MySQL cMake/configure 编译参数即使用方法对比详解
下表列是MySQL编译工具configure的调用语法和等效的CMake命令。“.”表示你当前的工作目录路径,请根据你所在的目录,适当的替换掉路径“.”。
| configure命令 | CMake命令 |
| ./configure | cmake . |
| ./configure --help | cmake . -LH or ccmake . |
如果你需要重新配置或重新构建之前的安装,则需要先清除旧的对象文件和缓存信息,方法如下:
Autotools:
make clean rm config.cache
CMake (Uinx/Linux):
make clean rm CMakeCache.txt
CMake (Windows):
devenv MySQL.sln /clean del CMakeCache.txt
MySQL cMake/configure 安装参数详细对比及说明
在下表中,“CMAKE_INSTALL_PREFIX”的值表示的是安装根目录,其他参数值的路径都是相对于根目录的,当然你也可以直接使用绝对路径,具体如下:
| 参数值说明 | 配置选项 | CMak选项 |
| 安装根目录 | --prefix=/usr | -DCMAKE_INSTALL_PREFIX=/usr |
| mysqld目录 | --libexecdir=/usr/sbin | -DINSTALL_SBINDIR=sbin |
| 数据存储目录 | --localstatedir=/var/lib/mysql | -DMYSQL_DATADIR=/var/lib/mysql |
| 配置文件(my.cnf)目录 | --sysconfdir=/etc/mysql | -DSYSCONFDIR=/etc/mysql |
| 插件目录 | --with-plugindir=/usr/lib64/mysql/plugin | -DINSTALL_PLUGINDIR=lib64/mysql/plugin |
| 手册文件目录 | --mandir=/usr/share/man | -DINSTALL_MANDIR=share/man |
| 共享数据目录 | --sharedstatedir=/usr/share/mysql | -DINSTALL_SHAREDIR=share |
| Library库目录 | --libdir=/usr/lib64/mysql | -DINSTALL_LIBDIR=lib64/mysql |
| Header安装目录 | --includedir=/usr/include/mysql | -DINSTALL_INCLUDEDIR=include/mysql |
| 信息文档目录 | --infodir=/usr/share/info | -DINSTALL_INFODIR=share/info |
存储引擎是以插件的形式存在的,所以,该选项可以控制插件的构建,比如指定使用某个特定的引擎。
--with-plugins配置选项接受两种形式的参数值,它没有对应的CMake配置参数:
① 以逗号(,)分隔的引擎名称列表;
② a "group name" value that is shorthand for a set of engines
在CMake中,引擎被作为单个的选项来进行控制。假设有以下配置选项:
--with-plugins=csv,myisam,myisammrg,heap,innobase,archive,blackhole
上面的参数指定MySQL数据库可以支持哪些数据库引擎,将上述编译选项转换成CMake编译选项时,下面的几个引擎名字可以被省略,因为编译时,默认就支持:csv,myisam,myisammrg,heap
然后我们可以使用下面的编译参数,以启用InnoDB、ARCHIVE和BLACKHOLE引擎支持:
当然也可以使用“ON”来替代数字1,它们是等效的。
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1
如果你想除去对某种引擎的支持,则在CMake编译选项中使用-DWITHOUT_<ENGINE>_STORAGE_ENGINE,例如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1
库文件加载选项
该选项指明Mysql使用库的情况:
| 参数值说明 | 配置选项 | CMak选项 |
| readline库 | --with-readline | -DWITH_READLINE=1 |
| SSL库 | --with-ssl=/usr | -DWITH_SSL=system |
| zlib库 | --with-zlib-dir=/usr | -DWITH_ZLIB=system |
| libwrap库 | --without-libwrap | -DWITH_LIBWRAP=0 |
MySQL Cmake 编译其他选项
MySQL cMake编译选项大部分能与之前的configure地址相同,其编译选项的差别在于:之前的是小写,现在全部变成了大写,之前采用双横线,现在使用单横线,之前使用的破折号,现在取而代之的是使用下划线,下面是具体对比:
--with-debug => -WITH_DEBUG=1
--with-embedded-server => -WITH_EMBEDDED_SERVER
下面是编译MySQL的新老参数对照表:
| 参数值说明 | 配置选项 | CMak选项 |
| TCP/IP端口 | --with-tcp-port-=3306 | -DMYSQL_TCP_PORT=3306 |
| UNIX socket文件 | --with-unix-socket-path=/tmp/mysqld.sock | -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock |
| 启用加载本地数据 | --enable-local-infile | -DENABLED_LOCAL_INFILE=1 |
| 扩展字符支持 | --with-extra-charsets=all(默认:all) | -DEXTRA_CHARSETS=all(默认:all) |
| 默认字符集 | --with-charset=utf8 | -DDEFAULT_CHARSET=utf8 |
| 默认字符校对 | --with-collation=utf8_general_ci | -DDEFAULT_COLLATION=utf8_general_ci |
| Build the server | --with-server | 无 |
| 嵌入式服务器 | --with-embedded-server | -DWITH_EMBEDDED_SERVER=1 |
| libmysqld权限控制 | --with-embedded-privilege-control | 无 |
| 安装文档 | --without-docs | 无 |
| Big tables支持 | --with-big-tables, --without-big-tables | 无 |
| mysqld运行用户 | --with-mysqld-user=mysql | -DMYSQL_USER=mysql |
| 调试模式 | --without-debug(默认禁用) | -DWITH_DEBUG=0(默认禁用) |
| GIS支持 | --with-geometry | 无 |
| 社区功能 | --enable-community-features | 无 |
| Profiling | --disable-profiling(默认启用) | -DENABLE_PROFILING=0(默认启用) |
| pstack | --without-pstack | 无(新版移除该功能) |
| 汇编字符串函数 | --enable-assembler | 无 |
| 构建类型 | --build=x86_64-pc-linux-gnu | 没有等效参数 |
| 交叉编译主机 | --host=x86_64-pc-linux-gnu | 没有等效参数 |
| 客户端标志 | --with-client-ldflags=-lstdc++ | 无 |
| 线程安全标志 | --enable-thread-safe-client | 无 |
| 注释存储类型 | --with-comment='string' | -DWITH_COMMENT='string' |
| Shared/static binaries | --enable-shared --enable-static | 无 |
| 内存使用控制 | --with-low-memory | 无 |
本文为秋伟光原创,转载请注明出处及作者,原文地址:http://waynerQiu.com/7/153.html
http://who0168.blog.51cto.com/253401/469898
从mysql5.5起,mysql源码安装开始使用cmake了。下面是介绍configure选项如何映射到CMake的等值参数。
1. 命令语法:
重新编译时,需要清除旧的对象文件和缓存信息
# make clean
# rm -f CMakeCache.txt
2.安装选项
CMAKE_INSTALL_PREFIX值是安装的基本目录,其他cmake选项值是不包括前缀,是相对路径名,绝对路径包括CMAKE_INSTALL_PREFIX路径。如-DINSTALL_SBINDIR=sbin的绝对路径是/usr/local/mysql/sbin
3.存储引擎选项
mysql存储引擎是插件式的,因此插件控制选项可以指定那个存储引擎安装。
configure编译插件选项--with-plugins=csv,myisam,myisammrg,heap,innobase,
archive,blackhole在cmake中没有直接对应的相同选项。对于csv,myisam,myisammrg,heap在cmake中是不需要明确指定存储引擎的名称,因为它们是强制性安装。
可以使用以下选择来安装innodb,archive,blackhole存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
(1可以使用on代替)
如果既不是-DWITH_<ENGINE>_STORAGE_ENGINE 也不是 -DWITHOUT_<ENGINE>_STORAGE_ENGINE 来指定存储引擎,该存储引擎将安装成共享模块式的。如果不是共享模块式的将排除在外。共享模块安装时必须使用INSTALL PLUGIN语句或--plugin-load才可以使用。
有关插件的CMake的选项的其他信息,请查阅:
http://forge.mysql.com/wiki/MySQL_Internals_Support_for_Plug-Ins
4.lib库选项
5.其他选项
之前MySQL的编译选项大多数都支持。新旧版本之间的安装选项映射成大写字母,删除选项前面破折号,中间字符间的破折号替换成下划线。如:
--with-debug => WITH_DEBUG=1
--with-embedded-server => WITH_EMBEDDED_SERVER
6.调试配置过程
使用configure编译完将生成config.log和config.status文件。
使用cmake编译完在CMakeFiles目录下生成CMakeError.log 和CMakeOutput.log文件。
7.第三方接口工具
在之前的版本,第三方工具从MySQL顶层源目录中读取源configure.in文件来确定mysql版本。如:对5.5.7 - RC版本的AC_INIT线看起来像这样:
AC_INIT([MySQL Server], [5.5.7-rc], [], [mysql])
现在的版本可以直接读取版本文件。如:如果版本是5.5.8,文件看起来像这样的:
MYSQL_VERSION_MAJOR=5
MYSQL_VERSION_MINOR=5
MYSQL_VERSION_PATCH=8
MYSQL_VERSION_EXTRA=
如果源码包不是GA版,MYSQL_VERSION_EXTRA的值将非空。如:对于一个发布RC版本是这样的:
MYSQL_VERSION_EXTRA=rc
构建5位数字的版本号,使用下面公式:
MYSQL_VERSION_MAJOR*10000 + MYSQL_VERSION_MINOR*100 + MYSQL_VERSION_PATCH
本文出自 “燕雀安知鸿鹄之志哉” 博客,请务必保留此出处http://who0168.blog.51cto.com/253401/469898
=====================================
http://runnerfish.github.io/blog/2014/11/11/mysql-multi-cnf/
引子
最近研究Mysql集群的问题,遇到一个需求,需要在同一台机器上运行多个Mysql实例。主要有两种实现方式,区别在于是否共享my.cnf配置文件。
1.分别配置my.cnf
这种情况下,想要运行多个Mysql实例,就需要分别安装多个Mysql,无论是socket、port、pid-file、datadir还是my.cnf都是彼此独立的。具体实现较为简单,只需保证目录之间的差异性即可,但不利于后期数据库迁移。
2.共用my.cnf
如果采用共用my.cnf的方式实现Mysql多实例,需要在my.cnf额外配置[mysqld_multi]节点,该节点包含各个Mysql实例的连接信息和数据存储目录。
考虑到方便统一部署和后期数据迁移,本文主要介绍第二种方案来实现Mysql多实例。
具体步骤
安装Mysql
建议最好采用编译安装,可进行最大程度的定制化,方便对Mysql进行配置。关于编译安装的详细步骤,可参考编译安装Percona Server并使用Transfer实现主从数据库同步加速(上)。
获取mysqld_multi配置样例
安装完Mysql后,可以运行如下命令,获取官方提供的Mysql多实例配置样例:
[root@chenllcentos ~]# mysqld_multi --example
# This is an example of a my.cnf file for mysqld_multi.
# Usually this file is located in home dir ~/.my.cnf or /etc/my.cnf
#
# SOME IMPORTANT NOTES FOLLOW:
#
# 1.COMMON USER
#
# Make sure that the MySQL user, who is stopping the mysqld services, has
# the same password to all MySQL servers being accessed by mysqld_multi.
# This user needs to have the 'Shutdown_priv' -privilege, but for security
# reasons should have no other privileges. It is advised that you create a
# common 'multi_admin' user for all MySQL servers being controlled by
# mysqld_multi. Here is an example how to do it:
#
# GRANT SHUTDOWN ON *.* TO multi_admin@localhost IDENTIFIED BY 'password'
#
# You will need to apply the above to all MySQL servers that are being
# controlled by mysqld_multi. 'multi_admin' will shutdown the servers
# using 'mysqladmin' -binary, when 'mysqld_multi stop' is being called.
#
# 2.PID-FILE
#
# If you are using mysqld_safe to start mysqld, make sure that every
# MySQL server has a separate pid-file. In order to use mysqld_safe
# via mysqld_multi, you need to use two options:
#
# mysqld=/path/to/mysqld_safe
# ledir=/path/to/mysqld-binary/
#
# ledir (library executable directory), is an option that only mysqld_safe
# accepts, so you will get an error if you try to pass it to mysqld directly.
# For this reason you might want to use the above options within [mysqld#]
# group directly.
#
# 3.DATA DIRECTORY
#
# It is NOT advised to run many MySQL servers within the same data directory.
# You can do so, but please make sure to understand and deal with the
# underlying caveats. In short they are:
# - Speed penalty
# - Risk of table/data corruption
# - Data synchronising problems between the running servers
# - Heavily media (disk) bound
# - Relies on the system (external) file locking
# - Is not applicable with all table types. (Such as InnoDB)
# Trying so will end up with undesirable results.
#
# 4.TCP/IP Port
#
# Every server requires one and it must be unique.
#
# 5.[mysqld#] Groups
#
# In the example below the first and the fifth mysqld group was
# intentionally left out. You may have 'gaps' in the config file. This
# gives you more flexibility.
#
# 6.MySQL Server User
#
# You can pass the user=... option inside [mysqld#] groups. This
# can be very handy in some cases, but then you need to run mysqld_multi
# as UNIX root.
#
# 7.A Start-up Manage Script for mysqld_multi
#
# In the recent MySQL distributions you can find a file called
# mysqld_multi.server.sh. It is a wrapper for mysqld_multi. This can
# be used to start and stop multiple servers during boot and shutdown.
#
# You can place the file in /etc/init.d/mysqld_multi.server.sh and
# make the needed symbolic links to it from various run levels
# (as per Linux/Unix standard). You may even replace the
# /etc/init.d/mysql.server script with it.
#
# Before using, you must create a my.cnf file either in /usr/local/mysql/my.cnf
# or /root/.my.cnf and add the [mysqld_multi] and [mysqld#] groups.
#
# The script can be found from support-files/mysqld_multi.server.sh
# in MySQL distribution. (Verify the script before using)
#
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = multi_admin
password = my_password
[mysqld2]
socket = /tmp/mysql.sock2
port = 3307
pid-file = /data/mysql2/hostname.pid2
datadir = /data/mysql2
language = /usr/local/mysql/share/mysql/english
user = unix_user1
[mysqld3]
mysqld = /path/to/mysqld_safe
ledir = /path/to/mysqld-binary/
mysqladmin = /path/to/mysqladmin
socket = /tmp/mysql.sock3
port = 3308
pid-file = /data/mysql3/hostname.pid3
datadir = /data/mysql3
language = /usr/local/mysql/share/mysql/swedish
user = unix_user2
mysqld_multi --example会自动读取本机Mysql的安装信息,包括mysqld和mysqladmin目录,并初始化socket、portpid-file和datadir信息。此处以运行两个Mysql实例为例子,修改[mysqld_multi]如下:
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = root
password = root
[mysqld2]
socket = /tmp/mysql.sock2
port = 3307
pid-file = /data/mysql2/chenllcentos.localdomain.pid2
datadir = /data/mysql2
user = mysql
[mysqld3]
socket = /tmp/mysql.sock3
port = 3308
pid-file = /data/mysql3/chenllcentos.localdomain.pid3
datadir = /data/mysql3
user = mysql
添加多实例配置信息
打开my.cnf:
[root@chenllcentos ~]# vi /etc/my.cnf
将[mysqld_multi]配置信息添加至my.cnf结尾。
配置数据目录
由[mysqld_multi]配置信息可以知道,我们对两个Mysql实例分别配置了datadir,该目录的设置非常重要,直接影响到多实例的启动是否成功。
创建数据目录:
[root@chenllcentos ~]# mkdir -p /data/mysql2
[root@chenllcentos ~]# mkdir -p /data/mysql3
初始化数据库:
[root@chenllcentos ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql2 --user=mysql
[root@chenllcentos ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql3 --user=mysql
更改数据目录所属用户组:
[root@chenllcentos ~]# chown -R mysql.mysql /data/mysql2
[root@chenllcentos ~]# chown -R mysql.mysql /data/mysql3
启动和停止Mysql多实例
Mysql多实例的启动和停止使用mysqld_multi进行管理,例如需要同时启动mysqld2实例和mysqld3实例,可运行命令:
[root@chenllcentos ~]# mysqld_multi start 2-3
若要停止数据库实例,命令类似:
[root@chenllcentos ~]# mysqld_multi stop 2-3
查看数据库端口运行状态:
[root@chenllcentos ~]# netstat -ntlp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 16975/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 16976/mysqld
至此,同一主机下运行多个Mysql实例配置成功。
后续配置
为mysqld2分配用户:
[root@chenllcentos ~]# mysqladmin -u root password 'root' -hlocalhost -P3307 -S /tmp/mysql.sock2
为mysqld3分配用户:
[root@chenllcentos ~]# mysqladmin -u root password 'root' -hlocalhost -P3308 -S /tmp/mysql.sock3
这里需要注意,由于Mysql默认读取的socket文件是/tmp/mysql.sock,而在多实例环境下该文件是不存在的。因此,如果不通过-S指定socket文件的话,将报无法连接Mysql服务的错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
登录数据库,同样需要指定连接端口和socket文件。现以登录mysqld2为例:
[root@chenllcentos ~]# mysql -uroot -proot -P3307 -S /tmp/mysql.sock2
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.34-log Source distribution
Copyright (c) 2009-2013 Percona LLC and/or its affiliates
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Mysql不同版本多实例
以上介绍的是同一个Mysql版本单机启动多实例,那如果Mysql版本不同呢?回想下,在my.cnf配置文件[mysqld_multi]节点下并没有指定版本的信息入口。其实,只需要在启动时,指定不同的my.cnf即可。现在以同时启动Mysql5.1版本和编译安装的Percona Server版本多实例为例子,进行说明。
首先,为了避免配置文件冲突,修改Percona Server配置文件名称:
[root@chenllcentos ~]# mv /etc/my.cnf /etc/my.cnfp
接下来,yum安装Mysql,安装结果如下:
[root@chenllcentos ~]# rpm -aq | grep mysql
mysql-5.1.73-3.el6_5.x86_64
mysql-server-5.1.73-3.el6_5.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64
mysql-devel-5.1.73-3.el6_5.x86_64
可以知道,通过yum安装的Mysql版本为5.1。此时我们有两个Mysql配置文件:
[root@chenllcentos ~]# ls -al /etc/my*
-rw-r--r--. 1 root root 295 11月 12 10:41 /etc/my.cnf
-rw-------. 1 root root 5076 11月 11 18:37 /etc/my.cnfp
通过Service启动Mysql5.1:
[root@chenllcentos ~]# service mysqld start
通过mysqld_multi再启动Percona Server两个实例:
[root@chenllcentos ~]# mysqld_multi --defaults-file=/etc/my.cnfp start 2-3
查看网络端口,验证是否启动成功:
[root@chenllcentos ~]# netstat -ntlp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 20132/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 20137/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 18091/mysqld
可以知道,目前有三个Mysql实例在运行。其中3306为Mysql5.1,3307和3308为Percona Server。
通过ps命令查看进程:
[root@chenllcentos ~]# ps -ef | grep mysql
root 17983 1 0 Nov12 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 18091 17983 0 Nov12 pts/0 00:00:30 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 19678 1 0 11:08 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/tmp/mysql.sock2 --port=3307 --pid-file=/data/mysql2/hostname.pid2 --datadir=/data/mysql2 --user=mysql
root 19685 1 0 11:08 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/tmp/mysql.sock3 --port=3308 --pid-file=/data/mysql3/hostname.pid3 --datadir=/data/mysql3 --user=mysql
mysql 20132 19678 0 11:08 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql2 --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/data/mysql2/hostname.pid2 --socket=/tmp/mysql.sock2 --port=3307
mysql 20137 19685 0 11:08 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql3 --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/data/mysql3/hostname.pid3 --socket=/tmp/mysql.sock3 --port=3308
root 20217 22772 0 11:37 pts/0 00:00:00 grep mysql
Mysql不同版本多实例配置成功。
根据需求现在需要测试mysql主从配置,但是由于本地主机资源有限,现在需要在一台centos主机上面搭建两到三个Mysql实例,以方便测试mysql主从。
实验分三大步骤:
1,在Centos上面安装MySQL server
2,利用MySQL的mysqld_multi配置多个实例
3,搭建MySQL主从
开始实验:
2,利用MySQL的mysqld_multi配置多个实例
首先确保上一步安装的mysql server正常运行
|
1
2
3
4
|
[root@TEST ~]# mysqladmin pingmysqld is alive[root@TEST ~]# /etc/init.d/mysqld statusMySQL running (21075)[ OK ] |
在同一台主机上面运行多个mysql instance的时候需要用到mysql的一个特殊功能mysqld_multi,
在linux shell下输入 mysqld_multi --example 就可以得到配置多个实例的一些例子
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[root@localhost ~]# mysqld_multi --example### there are many commons[mysqld_multi]mysqld = /usr/bin/mysqld_safemysqladmin = /usr/bin/mysqladminuser = multi_adminpassword = my_password[mysqld2]socket = /tmp/mysql.sock2port = 3307pid-file = /var/lib/mysql2/hostname.pid2datadir = /var/lib/mysql2language = /usr/share/mysql/englishuser = unix_user1[mysqld3]mysqld = /path/to/mysqld_safeledir = /path/to/mysqld-binary/mysqladmin = /path/to/mysqladminsocket = /tmp/mysql.sock3port = 3308pid-file = /var/lib/mysql3/hostname.pid3datadir = /var/lib/mysql3language = /usr/share/mysql/swedishuser = unix_user2[mysqld4]socket = /tmp/mysql.sock4port = 3309pid-file = /var/lib/mysql4/hostname.pid4datadir = /var/lib/mysql4language = /usr/share/mysql/estoniauser = unix_user3 [mysqld6]socket = /tmp/mysql.sock6port = 3311pid-file = /var/lib/mysql6/hostname.pid6datadir = /var/lib/mysql6language = /usr/share/mysql/japaneseuser = unix_user4 |
此试验中需要再额外创建两个instance,所以根据上面的例子,把所需的配置信息添加到mysql的主配置文件/etc/my.cnf 后面,例如:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[mysqld_multi]mysqld = /usr/bin/mysqld_safemysqladmin = /usr/bin/mysqladminuser = rootpassword = root1203[mysqld2]socket = /tmp/mysql.sock2 port = 3307pid-file = /var/run/mysqld/mysqld.pid2log-slow-queries = /var/log/mysql2/slow-query-log ## mysql slow log#relay-log = /var/spool/mysqld2/mysqld-relay-bin ## relay for replicationdatadir = /var/lib/mysql2 ## mysql data file folderuser = mysql[mysqld3]socket = /tmp/mysql.sock3port = 3308pid-file = /var/run/mysqld/mysqld.pid3log-slow-queries = /var/log/mysql3/slow-query-log#relay-log = /var/spool/mysqld3/mysqld-relay-bindatadir = /var/lib/mysql3user = mysql |
接下来根据配置文件的信息创建对应的目录并设置对应的权限
|
1
2
3
4
5
6
7
8
|
## make mysql data file foldermkdir -p /var/lib/mysql2mkdir -p /var/lib/mysql3## make log foldermkdir -p /var/log/mysql2mkdir -p /var/log/mysql3mkdir -p /var/spool/mysql2mkdir -p /var/spool/mysql3 |
修改mysql的数据目录确保只有mysql用户才有访问权限
|
1
2
|
chown mysql:root /var/lib/mysql2chown mysql:root /var/lib/mysql2 |
利用 mysql_install_db 安装初始化库
|
1
2
|
mysql_install_db --user=mysql --datadir=/var/lib/mysql2mysql_install_db --user=mysql --datadir=/var/lib/mysql3 |
最关键的时刻到了,启动安装的instance
|
1
2
3
4
|
[root@localhost ~]# mysqld_multi --help |grep UsageUsage: mysqld_multi [OPTIONS] {start|stop|report} [GNR,GNR,GNR...][root@localhost ~]# mysqld_multi start 2[root@localhost ~]# mysqld_multi start 3 |
尝试登陆:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@localhost ~]# mysql -S /tmp/mysql.sock2 -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.1.71 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> exitBye[root@localhost ~]# mysql -S /tmp/mysql.sock3 -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.1.71-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> |
到此在同一台主机上面创建多个mysql instance的算是完成了。
但是最后需要说明的是,第一次当我尝试stop 这些个instance的时候,使用 ”mysqld_multi stop 2" 却不起作用,网上查了下说是需要grant一个 shutdown的权限即可,
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
mysql> show grants for root@'localhost';+---------------------------------------------------------------------------------------------------------------+| Grants for root@localhost |+---------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '08a9554c6a9d5b2a' WITH GRANT OPTION |+---------------------------------------------------------------------------------------------------------------+1 row in set (0.02 sec)mysql> grant SHUTDOWN on *.* to 'root'@'localhost' ;Query OK, 0 rows affected (0.00 sec)mysql> show grants for root@'localhost';+---------------------------------------------------------------------------------------------------------------+| Grants for root@localhost |+---------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '08a9554c6a9d5b2a' WITH GRANT OPTION |+---------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>mysql> exitBye[root@localhost ~]# mysqld_multi stop 3[root@localhost ~]# mysqld_multi stop 2[root@localhost ~]#[root@localhost ~]# mysql -uroot -p -S /tmp/mysql.sock2Enter password:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock2' (2)[root@localhost ~]# |
OK,第二步测试通过
本文出自 “ColinSpace” 博客,请务必保留此出处http://colinspace.blog.51cto.com/1043642/1401850
http://www.jb51.net/article/4879.htm
**********************************************************
第一部分, 在一台服务器构建多mysql 服务.
**********************************************************
一,绪言
在Mysql中有一mysqld_multi命令,可用于在一台物理服务器运行多个Mysql服务,今天参考一些文档,亲自测试并通过,真高兴,现将操作过程共享给大家!
操作系统:Linux 2.6.13 (Slackware),其它版的Linux应该也差不多.
数据库:Mysql 4.0.17 源程序安装(相信最新的5.1.*也差不多,过些天再试试)
规划:运行4个mysql服务:
假设服务器名:db-app (IP为192.168.0.100),
假设服务器名:db-app2 (IP为192.168.0.101),
下文直到第二部分,均为讲述db-app中的mysql,
二,准备
用mysql源程序安装,假设在安装时用的configura选择项氯?
./configure --prefix=/usr/local/mysql --datadir=/usr/local/mysql/data1 --sysconfdir=/etc
备注:--prefix将MYSQL安装到/usr/local/mysql,
--datadir将数据库生成/usr/local/mysql/data1
sysconfdir是指定mysql使用到的my.cnf配置文件的搜索路径为/etc
其他mysql安装过程略.
根据Mysql管理手册中提到:每个Mysql的服务都可为独立的,所以它都调用一个my.cnf中各自不同的启动选项--就是下文中将提到的GNR值,使用不同的端口,生成各自的套接文件,服务的数据库都是独立的(更多可查阅mysql官方网站的英文管理手册).
mysqld_multi是管理多个mysqld的服务进程,这些服务进程程序不同的unix socket或是监听于不同的端口。他可以启动、停止和监控当前的服务状态。
----程序在my.cnf(或是在--config-file自定义的配置文件)中搜索[mysqld#]段,"#"可以是任意的正整数。这个正整数就是在下面提及的段序列,即GNR。段的序号做为mysqld_multi的参数,来区别不同的段,这样你就可以控制特定mysqld进程的启动、停止或得到他的报告信息。这些组里的参数就像启动一个mysqld所需要的组的参数一样。但是,如果使用多服务,必须为每个服务指定一个unix socket或端口(摘自http://mifor.4dian.org中的使用mysqld_multi程序管理多个MySQL服务 )。
从上述文字可看到多Mysql服务中最重要的就是my.cnf配置文件了.
现我贴出我的my.cnf文件.-----------------------
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql (用一个帐号来启动所有的mysql服务器,因为是用一相同的帐号。那个么这帐号必须都是每个mysql服务都要用的帐号,最好是管理帐号,下面的口令与相同)
password = mypaswd
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
pid-file=/usr/local/mysql/data1/db-app1.pid
log=/usr/local/mysql/data1/db-app.log
datadir = /usr/local/mysql/data
user = mysql
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/db-app2.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/db-app.log
user = mysql
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
pid-file = /usr/local/mysql/data3/db-app3.pid3
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/db-app.log
user = mysql
[mysqld4]
port = 3309
socket = /tmp/mysql.sock4
pid-file = /usr/local/mysql/data3/db-app4.pid
datadir = /usr/local/mysql/data4
log=/usr/local/mysql/data4/db-app.log
user = mysql
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
我的配置文件中有mysqld1,mysqld2,mysqld3,mysqld4。就是说我将启动4个mysql服务在同一服务器的不同端口——3306-3309,每datadir所指定的数据库文件路径都是不相同的,都有各自不同的日志文件。其它一些设置可用my.cnf 原来的内容.
ok, 重要的my.cnf编好的,现在为配置中不同mysql 服务建立各自文件夹和初始数据库等.
[mysqld1]是一个默认的,在我们安装mysql时已经有了,所以不用管它.
[mysqld2],只要根据配置就的路径为它建立一个目录就可以了.将把该目录改为mysql管理权
db-app:/ # mkdir /usr/local/mysql/data2
建数据库,我们可以把默认的mysql数据库复制过来,以利用其它的mysql帐号,其它数据库根据应用再建立.
db-app:/ # cp /usr/local/mysql/data1/mysql /usr/local/mysql/data2 -R
db-app:/ # chmod mysql.mysql /usr/local/mysql/data2 -R
[mysqld3],[mysqld4], 相同.
我们可能看看这些目录是否都存在.
db-app:/ # ls -l /usr/local/mysql/
drwxr-xr-x 6 mysql mysql 4096 Apr 9 17:54 data4
drwxr-x--- 2 mysql mysql 4096 Apr 9 17:14 data1
drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data2
drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data3
现在可以通过mysqld_multi启动了。
三,mysqld_multi命令.
使用如下参数来启动mysqld_multi: (注:该命令在mysql的bin目录中,根据上面所提到./configure --prefix=/usr/local/mysql ,所以该文件应该在 /usr/local/mysq/bin, 这得根据你安装时所指定的路径 )
db-app:/ # mysqld_multi [options] {start|stop|report} [GNR[,GNR]...]
start,stop和report是指你想到执行的操作。你可以在单独的服务或是多服务上指定一个操作,区别于选项后面的GNR列表。如果没有指定GNR列表,那么mysqld_multi将在所有的服务中根据选项文件进行操作。
每一个GNR的值是组的序列号或是一个组的序列号范围。此项的值必须是组名字最后的数字,比如说如果组名为mysqld17,那么此项的值则为17.如果指定一个范围,使用"-"(破折号)来连接二个数字。如GNR的值为10-13,则指组mysqld10到组mysqld13。多个组或是组范围可以在命令行中指定,使用","(逗号)隔开。不能有空白的字符(如空格或tab),在空白字符后面的参数将会被忽略。 (注:GNR值就是我们定义my.cnf中mysqld#中的值,我这里只有1-4).
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1 只启动 第一个mysql服务,相关文件由my.cnf中mysql1设定.
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf stop 1 启止 第一个mysql服务
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4, 启动 第1至4mysql服务,其实就是我这里的全部.
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf report 1-4
查看启动:
db-app:/ # ps aux
root 10467 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3306 --socket=/tmp/mysql.sock1
root 10475 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3307 --socket=/tmp/mysql.sock2
root 10482 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3308 --socket=/tmp/mysql.sock3
root 10487 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3309 --socket=/tmp/mysql.sock4
.............................................
四,客户端访问
任何客户端访问都需要指定访问端口.方才能进入指定数据库服务.否则将使用到Mysql默认的端口(3306)所服务的MYSQL。
**********************************************************
第二部分, 在一台服务器构建多mysql主 服务.
**********************************************************
提示:建立一个帐号,专门用于下面主从复制,本例用的的帐号repl, 口令为:'1234567890', 可让其拥有select_priv, reload_priv,process_priv,grant_priv,super_priv,repl_slave_priv,repl_client_priv权限,并能通过任何客户进行访问,即访问客户为'%'。
主要是修改my.cnf中的内容,让每个mysql生成自己的bin-log文件及各自的运行环境,现贴上我的my.cnf全部内容,相关参数与请参考mysql 官文手册.
#[client]
#password = your_password
#port = 3306
#socket = /tmp/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql
password = mypasswd
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
skip-locking
pid-file=/usr/local/mysql/data/net-app1a.pid
datadir = /usr/local/mysql/data
log=/usr/local/mysql/data/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data/slowquery.log
long_query_time = 2
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache = 32
query_cache_size = 32M
thread_concurrency = 2
max_connections=500
log-bin
log-bin=/usr/local/mysql/data/app-net1_1-bin
server-id = 1
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/net-app1b.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data2/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
log-bin
log-bin=/usr/local/mysql/data2/app-net1_2-bin
server-id = 1
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
skip-locking
pid-file = /usr/local/mysql/data3/net-app1c.pid
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/net-app1.log
user = mysql
log-bin
log-bin=/usr/local/mysql/data3/app-net1_3-bin
server-id = 1
[mysqld4]
port = 3309
socket = /tmp/mysql.sock4
skip-locking
pid-file = /usr/local/mysql/data1/app-net1d.pid
datadir = /usr/local/mysql/data1
log=/usr/local/mysql/data1/net-app1.log
user = mysql
log-bin
log-bin=/usr/local/mysql/data1/app-net1_4-bin
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
**********************
启动多mysql服务都是相同的,
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
**********************************************************
第三部分, 在一台服务器构建多mysql 从服务.
**********************************************************
构建从服务器的预选准备: 建议用mysqld_multi 把主服务器的mysql全部停掉.删除数据目录中的所有除数据库目录以外的任何文件(此文中的数据目录库有4个,datadir = /usr/local/mysql/中的 data1 -- data1).建主从都用相同的数据目录路径.
用Tar 命令把每数据库封装起来,并通过sftp命令put/get到从服务器(db-app1 192.168.0.101).
下列操作供参考:
在db-app主机上的操作
db-app:/ # tar -cf data1.tar /usr/local/mysql/data1
db-app:/ # tar -cf data2.tar /usr/local/mysql/data2
db-app:/ # tar -cf data3.tar /usr/local/mysql/data3
db-app:/ # tar -cf data4.tar /usr/local/mysql/data4
在db-app1主机上的操作
db-app1:/ # tar xvf data1.tar
db-app1:/ # tar xvf data2.tar
db-app1:/ # tar xvf data3.tar
db-app1:/ # tar xvf data4.tar
同时,请确认系统帐号mysql是否对主/从服务器的中的mysql数据目录都有操作权限,如果无法确认,你直接更修改这些目录的所有权即可。
在db-app主机上的操作
db-app:/ # chown mysql.mysql /usr/local/mysql/data1 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data2 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data3 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data4 -R
在db-app1主机上的操作
db-app1:/ # chown mysql.mysql /usr/local/mysql/data1 -R
db-app2:/ # chown mysql.mysql /usr/local/mysql/data2 -R
db-app3:/ # chown mysql.mysql /usr/local/mysql/data3 -R
db-app4:/ # chown mysql.mysql /usr/local/mysql/data4 -R
下面就是从服务器上/etc/my.cnf的全部内容.
提示:下面的my.cnf中将会提到一个帐号:repl, 口令为:'1234567890', 这个帐号就是上面专门建立的。
其实都一样,主要是修改my.cnf中的内容,让每个从mysql通过主mysql的不同的端口,去获取各自bin-log来更新自生的数据库内容.现贴上我的my.cnf全部内容(从服务器),相关参数与请参考mysql 官文手册.
#[client]
#password = your_password
#port = 3306
#socket = /tmp/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql
password = netmoniit
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
skip-locking
pid-file=/usr/local/mysql/data/net-app1a.pid
datadir = /usr/local/mysql/data
log=/usr/local/mysql/data/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data/slowquery.log
long_query_time = 2
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache = 32
query_cache_size = 32M
thread_concurrency = 2
max_connections=500
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/net-app1b.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data2/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
pid-file = /usr/local/mysql/data3/net-app1c.pid
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data3/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld3]
port = 3308
socket = /tmp/mysql.sock4
pid-file = /usr/local/mysql/data4/net-app1d.pid
datadir = /usr/local/mysql/data4
log=/usr/local/mysql/data4/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data4/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
****************************************
在功告成,现在分别启动两台主机上的多mysql服务,这样,每个主服务的每个mysql有变化,都会自动复制/更新到从服务器对应的数据库中。
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
db-app1:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
********************
五,未来测试,
接下来我想做一个多主一从的Mysql服务器复制解决方案! 那位可以给点意见啊!结构如下.
就是有主服务器Server A , Server B和从服务器 Server C, A 和 B运行着不同的数据库应用, 假设数据库名都不同。Server C(假设这三台PC上都只运行了一个mysql服务),包括了A了B服务器的所有Mysql 用户及相同的访问权限, 并集成在一个Mysql服务中. C通过主/从方式复制A和B的数据库。
就是差不就是把两个主服务器的mysql合并到一个从服务器中。





浙公网安备 33010602011771号