一、安装说明
1.将所有的安装文件、配置文件、数据目录全部放存/data/mysql目录中,便于今后实现快速迁移、整体备份和快速复制;
2.在一台服务器上运行四个MySQL实例,分别绑定在3306、3307、3308、3309端口上
3.四个实例都开启binlog日志,数据目录分别存放在/data/mysql/data1、/data/mysql/data2、/data/mysql/data3、/data/mysql/data4
4.四个实例均采用InnoDB作为默认的存储引擎,字符编码采用UTF-8
5.四个实例均采用相同的性能优化配置参数
6.在编译安装时,将数据库的配置文件my.cnf以及data目录等均指向到/data/mysql目录中
7.通过mysqld_multi的方式来管理四个不同的实例,采用相同的配置文件共享性能优化配置参数
8.在同一个配置文件中,利用[mysqld1]、[mysqld2]、[mysqld3]、[mysqld4]标签实现不同实例的差异化配置
二、环境准备
1.安装mysql5.5依赖包
[root@test ~]# yum install -y autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool* openssl*
2.安装cmake
[root@test ~]# yum install -y cmake
三、安装MySQL
1.创建安装目录与数据存放目录
[root@test ~]# mkdir /data/mysql [root@test ~]# mkdir -p /data/mysql/data1
2.创建mysql用户与组
[root@test ~]# useradd myql
3.授权安装目录与数据目录
[root@test ~]# chown -R mysql.mysql /data/mysql/ [root@test ~]# chown -R mysql.mysql /data/mysql/data1
4.安装mysql
[root@test opt]# tar -zxvf mysql-5.5.14.tar.gz [root@test opt]# cd mysql-5.5.14 [root@test opt]# cmake -DCMAKE_INSTALL_PREFIX=/data/mysql -DSYSCONFDIR=/data/mysql/etc -DMYSQL_DATADIR=/data/mysql/data1 -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 [root@test opt]# make [root@test opt]# make install
mysql就安装完成!!
四、新建支持多实例的配置文件(四个实例)
1.删除默认的数据目录
[root@test mysql]# pwd /data/mysql [root@test mysql]# rm -rf data data1
2.创建多实例配置需要的目录
[root@test mysql]# mkdir etc tmp run log binlogs data1 data2 data3 data4 [root@test mysql]# chown -R mysql.mysql tmp run log binlogs data1 data2 data3 data4
3.提供配置文件
[root@test ~]# cp /opt/mysql-5.5.14/support-files/my-small.cnf /data/mysql/etc/my.cnf [root@test ~]# vi /data/mysql/etc/my.cnf # This server may run 4+ separate instances. So we use mysqld_multi to manage their services. [client] default-character-set = utf8 [mysqld_multi] mysqld = /data/mysql/bin/mysqld_safe mysqladmin = /data/mysql/bin/mysqladmin log = /data/mysql/log/mysqld_multi.log user = root #password = root # This is the general purpose database. # The locations are default. # They are left in [mysqld] in case the server is started normally instead of by mysqld_multi. [mysqld1] socket = /data/mysql/run/mysqld.sock port = 3306 pid-file = /data/mysql/run/mysqld.pid datadir = /data/mysql/data1 lc-messages-dir = /data/mysql/share/english # These support master - master replication #auto-increment-increment = 4 #auto-increment-offset = 1 # Since it is master 1 log-bin = /data/mysql/binlogs/bin-log-mysqld1 log-bin-index = /data/mysql/binlogs/bin-log-mysqld1.index #binlog-do-db = # Leave this blank if you want to control it on slave max_binlog_size = 1024M # This is exlusively for mysqld2 # It is on 3307 with data directory /data/mysqld/data2 [mysqld2] socket = /data/mysql/run/mysqld.sock2 port = 3307 pid-file = /data/mysql/run/mysqld.pid2 datadir = /data/mysql/data2 lc-messages-dir = /data/mysql/share/english # Disable DNS lookups #skip-name-resolve # These support master - slave replication log-bin = /data/mysql/binlogs/bin-log-mysqld2 log-bin-index = /data/mysql/binlogs/bin-log-mysqld2.index #binlog-do-db = # Leave this blank if you want to control it on slave max_binlog_size = 1024M # Relay log settings #relay-log = /data/mysql/log/relay-log-mysqld2 #relay-log-index = /data/mysql/log/relay-log-mysqld2.index #relay-log-space-limit = 4G # Slow query log settings #log-slow-queries = /data/mysql/log/slow-log-mysqld2 #long_query_time = 2 #log-queries-not-using-indexes # This is exlusively for mysqld3 # It is on 3308 with data directory /data/mysqld/data3 [mysqld3] socket = /data/mysql/run/mysqld.sock3 port = 3308 pid-file = /data/mysql/run/mysqld.pid3 datadir = /data/mysql/data3 lc-messages-dir = /data/mysql/share/english #Disable DNS lookups #skip-name-resolve # These support master - slave replication log-bin = /data/mysql/binlogs/bin-log-mysqld3 log-bin-index = /data/mysql/binlogs/bin-log-mysqld3.index #binlog-do-db = # Leave this blank if you want to control it on slave max_binlog_size = 1024M # This is exlusively for mysqld4 # It is on 3309 with data directory /data/mysqld/data4 [mysqld4] socket = /data/mysql/run/mysqld.sock4 port = 3309 pid-file = /data/mysql/run/mysqld.pid4 datadir = /data/mysql/data4 lc-messages-dir = /data/mysql/share/english # Disable DNS lookups #skip-name-resolve # These support master - slave replication log-bin = /data/mysql/binlogs/bin-log-mysqld4 log-bin-index = /data/mysql/binlogs/bin-log-mysqld4.index #binlog-do-db = # Leave this blank if you want to control it on slave max_binlog_size = 1024M # The rest of the my.cnf is shared # Here follows entries for some specific programs # The MySQL server [mysqld] basedir = /data/mysql tmpdir = /data/mysql/tmp socket = /data/mysql/run/mysqld.sock port = 3306 pid-file = /data/mysql/run/mysqld.pid datadir = /data/mysql/data1 lc-messages-dir = /data/mysql/share/english skip-external-locking key_buffer_size = 16K max_allowed_packet = 1M table_open_cache = 4 sort_buffer_size = 64K read_buffer_size = 256K read_rnd_buffer_size = 256K net_buffer_length = 2K thread_stack = 128K # Increase the max connections max_connections = 2 # The expiration time for logs, including binlogs expire_logs_days = 14 # Set the character as utf8 character-set-server = utf8 collation-server = utf8_unicode_ci # This is usually only needed when setting up chained replication #log-slave-updates # Enable this to make replication more resilient against server crashes and restarts # but can cause higher I/O on the server #sync_binlog = 1 # The server id, should be unique in same network server-id = 1 # Set this to force MySQL to use a particular engine/table-type for new tables # This setting can still be overridden by specifying the engine explicitly # in the CREATE TABLE statement default-storage-engine = INNODB # Enable Per Table Data for InnoDB to shrink ibdata1 innodb_file_per_table = 1 # Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /data/mysql/data #innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /data/mysql/data # You can set .._buffer_pool_size up to 50 - 80 % of RAM # but beware of setting memory usage too high innodb_buffer_pool_size = 16M innodb_additional_mem_pool_size = 2M # Set .._log_file_size to 25 % of buffer pool size innodb_log_file_size = 5M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M [mysqlhotcopy] interactive-timeout [mysql.server] user = mysql [mysqld_safe] log-error = /data/mysql/log/mysqld.log pid-file = /data/mysql/run/mysqld.pid open-files-limit = 8192
注,MySQL自带了几个不同的配置文件,放置在/data/mysql/support-files目录下,分别是my-huge.cnf,my-innodb-heavy-4G.cnf,my-large.cnf,my-medium.cnf,my-small.cnf,通过名称我们可以很直观的了解到他们是针对不同的服务器配置的,本文的配置文件是来自于my-small.cnf的,因为我是在虚拟机上进行的设置;在生产环境中,我们可以通过参考my-huge.cnf或my-innodb-heavy-4G.cnf中的部分参数配置,来对服务器进行优化;
4.修改my.cnf读写权限
[root@test ~]# chown -R root.root /data/mysql/etc [root@test ~]# chmod 600 /data/mysql/etc/my.cnf
配置文件就设置完成!!
五、初始化多实例数据库
1.切换到mysql的安装目录
[root@test ~]# cd /data/mysql/
2.初始化实例[mysqld1][mysqld2][mysqld3][mysqld4]
[root@test mysql]# scripts/mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data1 --user=mysql [root@test mysql]# scripts/mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data2 --user=mysql [root@test mysql]# scripts/mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data3 --user=mysql [root@test mysql]# scripts/mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data4 --user=mysql
初始化工作就完成!!
六、提供管理脚本 mysqld_multi.server
[root@test mysql]# mkdir /data/mysql/init.d [root@test mysql]# cp support-files/mysqld_multi.server init.d/ [root@test mysql]# vi /data/mysql/init.d/mysqld_multi.server #!/bin/sh # # A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen. # This script assumes that my.cnf file exists either in /etc/my.cnf or # /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the # mysqld_multi documentation for detailed instructions. # # This script can be used as /etc/init.d/mysql.server # # Comments to support chkconfig on RedHat Linux # chkconfig: 2345 64 36 # description: A very fast and reliable SQL database engine. # # Version 1.0 # basedir=/data/mysql bindir=/data/mysql/bin conf=/data/mysql/etc/my.cnf export PATH=$PATH:$bindir if test -x $bindir/mysqld_multi then mysqld_multi="$bindir/mysqld_multi"; else echo "Can't execute $bindir/mysqld_multi from dir $basedir"; exit; fi case "$1" in 'start' ) "$mysqld_multi" --defaults-extra-file=$conf start $2 ;; 'stop' ) "$mysqld_multi" --defaults-extra-file=$conf stop $2 ;; 'report' ) "$mysqld_multi" --defaults-extra-file=$conf report $2 ;; 'restart' ) "$mysqld_multi" --defaults-extra-file=$conf stop $2 "$mysqld_multi" --defaults-extra-file=$conf start $2 ;; *) echo "Usage: $0 {start|stop|report|restart}" >&2 ;; esac
所有的配置就全部完成!!
七、整体备份方便后续迁移
[root@test ~]# cd /data/ [root@test data]# tar czvf mysql-5.5.14-full.tar.gz /data/mysql/
备份完成后,直接将mysql-5.5.350-full.tar.gz拿到其他服务器上,解压后便可以直接启用!!
八、管理MySQL多实例
1.同时启动四个mysql实例
[root@test mysql]# /data/mysql/init.d/mysqld_multi.server start 1,2,3,4
查看一下启动的实例:
[root@test mysql]# netstat -ntulp | grep mysqld tcp 0 0 :::3306 :::* LISTEN 2964/mysqld tcp 0 0 :::3307 :::* LISTEN 3114/mysqld tcp 0 0 :::3308 :::* LISTEN 3101/mysqld tcp 0 0 :::3309 :::* LISTEN 2970/mysqld [root@test mysql]#
2.同时关闭四个mysql实例
[root@test init.d]# /data/mysql/init.d/mysqld_multi.server stop 1,2,3,4
3.单独启动或关闭mysql实例
(1).启动一个实例
[root@test init.d]# /data/mysql/init.d/mysqld_multi.server start 1 [root@test init.d]# netstat -ntulp | grep mysqld tcp 0 0 :::3306 :::* LISTEN 20139/mysqld [root@test init.d]#
(2).关闭一个实例
[root@test init.d]# /data/mysql/init.d/mysqld_multi.server stop 1
九、登录MySQL多实例
[root@test init.d]# /data/mysql/bin/mysql -uroot -h127.0.0.1 -P3306 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.14-log Source distribution Copyright (c) 2000, 2010, 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>
其他实例类似!
十、其它管理配置
1.为mysql的root用户创建密码
[root@test ~]# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3306 password 'root' [root@test ~]# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3307 password 'root' [root@test ~]# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3308 password 'root' [root@test ~]# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3309 password 'root'
2.删除匿名连接的空密码帐号
注,分别登录实例[mysqld1]、[mysqld2]、[mysqld3]、[mysqld4],执行以下命令:
mysql>use mysql; #选择系统数据库mysql mysql>select Host,User,Password from user; #查看所有用户 mysql>delete from user where password="";#删除无密码账户 mysql>flush privileges; #刷新权限 mysql>select Host,User,Password from user; #确认密码为空的用户是否已全部删除 mysql>exit;
十一、总结
1.采用源码编译安装MySQL,可能在第一次会花费较多的时间,但却是非常值得的,因为我们可以自己组织所有MySQL相关文件的位置;并且经过源码编译安装后的MySQL,可以直接复制到其它服务器上运行,大大方便了我们以后的迁移、备份和新服务器的配置。
2.本文中仅仅用了四个实例[mysqld1]、[mysqld2]、[mysqld3]、[mysqld4]来举例,实际上我们可以通过这样的方式,实现[mysqld5]、[mysqld6]...等更多的实例,前提是你的服务器硬件配置得根得 上,但是一般我们这边不会超过6个实例。
3.在单机运行多实例的情况下,切忌不要使用 mysql -hlocalhost 或 直接忽略-h参数登录服务器,这应该算是MySQL的一个bug,就是如果使用localhost或忽略-h参数,而不是指定127.0.0.1的话,即使选择的端口是3307,还是会登陆到3306中去,因此应尽量避免这种混乱的产生,统一用127.0.0.1绑定端口或采用socket来登录,在mysql5.5中你不指定-h127.0.0.1选项,你是无法登录的。