linux安装多实例mysql
linux系统centos 6.5,使用二进制安装多实例mysql 5.5.60
所需安装包mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz、ncurses-devel-5.7-4.20090207.el6.x86_64.rpm、libaio-devel-0.3.107-10.el6.x86_64.rpm
在一台机器上开启多个不同的服务端口,运行多个mysql服务进程,这些mysql多实例共用一套mysql安装程序,使用不同(也可以相同)的my.cnf配置文件、启动程序、数据文件,多个实例根据配置文件对应的设定值来取得对应数量的服务器相关硬件资源。多实例类似合租的各个房间,硬件资源相当于公用的厨房、卫生间和客厅
创建更新包目录,并将三个安装包上传至该目录下,赋予执行权限
[root@oldboy ~]# mkdir -p /home/oldboy/tools [root@oldboy ~]# cd /home/oldboy/tools [root@oldboy tools]# chmod +x * [root@oldboy tools]# rpm -ivh --nodeps ncurses-devel-5.7-4.20090207.el6.x86_64.rpm [root@oldboy tools]# rpm -ivh libaio-devel-0.3.107-10.el6.x86_64.rpm
创建用户和组、解压二进制mysql安装包、并创建软连接
[root@oldboy tools]# groupadd mysql [root@oldboy tools]# useradd -M mysql -g mysql -s /sbin/nologin [root@oldboy tools]# tar zxf mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz [root@oldboy tools]# mkdir -p /application/ [root@oldboy tools]# mv mysql-5.5.60-linux-glibc2.12-x86_64 /application/mysql-5.5.60 [root@oldboy tools]# ln -s /application/mysql-5.5.60/ /application/mysql
以/data目录作为mysql多实例的总根目录,在/data下创建mysql实例端口号3306、3307的二级目录,不同的端口号代表不同的实例,各个实例目录下又包含各自的数据文件、配置文件及启动文件。
[root@oldboy tools]# mount /dev/cdrom /mnt mount: block device /dev/sr0 is write-protected, mounting read-only [root@oldboy tools]# cd /mnt/Packages/ [root@oldboy Packages]# rpm -ivh tree-1.5.3-2.el6.x86_64 [root@oldboy Packages]# mkdir -p /data/{3306,3307}/data [root@oldboy Packages]# tree /data /data ├── 3306 │ └── data └── 3307 └── data 4 directories, 0 files
配置mysql配置文件
[root@oldboy Packages]# mv /etc/my.cnf /etc/my.cnf.bak [root@oldboy Packages]# cd /application/mysql/support-files/ [root@oldboy support-files]# cp my-small.cnf /etc/my.cnf
再将事先编辑好的两个my.cnf配置文件分别放置到/data/3306/和/data/3307/下,两个 mysql配置文件分别放置到/data/3306/和/data/3307/下(3306和3307的两对配置文件除了端口3306和3307不同,其他完全相同),修改所属组和权限
[root@oldboy ~]# chown -R mysql.mysql /data [root@oldboy ~]# chmod +x /data/{3306,3307}/mysql
3306的my.cnf内容如下
[client] port=3306 socket= /data/3306/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3306 socket = /data/3306/mysql.sock basedir = /application/mysql datadir = /data/3306/data open_files_limit=1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k #default_table_type = InnoDB thread_stack = 192K #transaction_isolation = READ-COMMITTED tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 #log_long_format #log-error = /data/3306/error.log #log-slow-queries =/data/3306/slow.log pid-file = /data/3306/mysql.pid log-bin = /data/3306/mysql-bin relay-log = /data/3306/relay-bin relay-log-info-file = /data/3306/relay-log.info binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M expire_logs_days = 7 key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M #myisam_sort_buffer_size = 1M #myisam_max_sort_file_size = 10G #myisam_max_extra_sort_file_size = 10G #myisam_repair_threads = 1 #myisam_recover lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db=mysql server-id = 1 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/data/3306/mysql3306.err pid-file=/data/3306/mysqld.pid
3306的mysql文件内容如下
#!/bin/sh ###################################### ###################################### #init port=3306 mysql_user="root" mysql_pwd="oldboy" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & else printf "MySQL is running...\n" exit fi } #stop function function_stop_mysql() { if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown fi } #restart function function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) printf "Usage: /data/${port}/mysql {start|stop|restart}\n" esac
设置PATH环境变量
[root@oldboy ~]# echo 'export PATH=/application/mysql/bin:$PATH'>>/etc/profile [root@oldboy ~]# source /etc/profile [root@oldboy ~]# echo $PATH /application/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
安装两个mysql实例,出现两个ok表示此步骤没有问题
[root@oldboy ~]# cd /application/mysql/scripts/ [root@oldboy scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql ………………… Installing MySQL system tables... 180727 2:45:31 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 180727 2:45:31 [Note] /application/mysql/bin/mysqld (mysqld 5.5.60) starting as process 60182 ... OK Filling help tables... 180727 2:45:31 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 180727 2:45:31 [Note] /application/mysql/bin/mysqld (mysqld 5.5.60) starting as process 60189 ... OK ………………… [root@oldboy scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql ………………… Installing MySQL system tables... 180727 2:47:01 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 180727 2:47:01 [Note] /application/mysql/bin/mysqld (mysqld 5.5.60) starting as process 60234 ... OK Filling help tables... 180727 2:47:01 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 180727 2:47:01 [Note] /application/mysql/bin/mysqld (mysqld 5.5.60) starting as process 60241 ... OK …………………
启动mysql实例,先启动3306实例
[root@oldboy scripts]# /data/3306/mysql start Starting MySQL... [root@oldboy scripts]# 180727 02:49:17 mysqld_safe error: log-error set to '/data/3306/mysql3306.err', however file don't exists. Create writable for user 'mysql'.
上面报log-error '/data/3306/mysql3306.err'不能存在,那么创建
[root@oldboy scripts]# cd /data [root@oldboy data]# touch 3306/mysql3306.err [root@oldboy data]# touch 3307/mysql3307.err [root@oldboy data]# chown mysql.mysql 3306/mysql3306.err [root@oldboy data]# chown mysql.mysql 3307/mysql3307.err
再分别启动和登录登录两个实例,启动后通过netstat -lntup查看启动情况,多实例登录时需要通过-S指定sock文件,且初次登录没有密码
[root@oldboy data]# /data/3306/mysql start Starting MySQL... [root@oldboy data]# /data/3307/mysql start Starting MySQL... [root@oldboy 3306]# netstat -lntup|grep 33 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 771/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 803/mysqld [root@oldboy data]# mysql -S /data/3306/mysql.sock (无密码登录) [root@oldboy data]# mysql -S /data/3307/mysql.sock (无密码登录)
通过/application/mysql/bin下的mysqladmin命令给两个实例设置root密码,再通过密码登录mysql
[root@oldboy data]# mysqladmin -u root -S /data/3306/mysql.sock password "oldboy" [root@oldboy data]# mysqladmin -u root -S /data/3307/mysql.sock password "oldboy" [root@oldboy data]# mysql -uroot -poldboy -S /data/3306/mysql.sock(有密码登录) mysql> system mysql -uroot -poldboy -S /data/3307/mysql.sock(在3306实例中通过system系统命令切换到3307实例)
设置多实例开机自启动
[root@oldboy data]# echo "#mysql multi instance start when system starting" >>/etc/rc.local [root@oldboy data]# echo "/data/3306/mysql start" >>/etc/rc.local [root@oldboy data]# echo "/data/3307/mysql start" >>/etc/rc.local [root@oldboy data]# tail -3 /etc/rc.local tail: inotify cannot be used, reverting to polling #mysql multi instance start when system starting /data/3306/mysql start /data/3307/mysql start