本次配置采用cmake编译安装的mysql进行多实例的配置,使用不同的配置文件进行的配置,大体步骤分为以下:
安装MySQL软件
创建多实例数据文件目录
创建多实例的配置文件
修改多实例的文件权限
初始化mysql多实例
mysql多实例的连接及密码添加
mysql多实例的启动及关闭
打开远程连接访问
添加到开机启动
操作系统版本
CentOS release 6.5 (Final) x86_64
MySQL版本
5.5.45
1.安装MySQL软件
2.创建多实例数据文件目录
[root@mysql ~]# pkill mysqld [root@mysql ~]# rm /etc/init.d/mysqld [root@mysql ~]# mkdir -p /data/{3306,3307}/data [root@mysql ~]# tree /data/ [root@mysql ~]# rm /etc/my.cnf
3.创建多实例配置文件
[root@mysql ~]# /bin/cp /soft/mysql-5.5.45/support-files/my-small.cnf /data/3306/my.cnf [root@mysql ~]# /bin/cp /soft/mysql-5.5.45/support-files/my-small.cnf /data/3307/my.cnf
编辑修改配置文件
注意的参数有端口号、socket、各实例相关的文件位置以及server-id
vi /data/3306/my.cnf
[client] #password = your_password port = 3306 socket = /data/3306/mysql.sock # The MySQL server [mysqld] port = 3306 socket = /data/3306/mysql.sock 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 datadir = /data/3306/data log-error = /data/3306/mysql_3306.err 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 #skip-networking server-id = 1 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M [mysqlhotcopy] interactive-timeout
vi /data/3307/my.cnf
[client] #password = your_password port = 3307 socket = /data/3307/mysql.sock # The MySQL server [mysqld] port = 3307 socket = /data/3307/mysql.sock 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 datadir = /data/3307/data log-error = /data/3307/mysql_3307.err log-slow-queries = /data/3307/slow.log pid-file = /data/3307/mysql.pid log-bin = /data/3307/mysql-bin relay-log = /data/3307/relay-bin relay-log-info-file = /data/3307/relay-log.info #skip-networking server-id = 3 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M [mysqlhotcopy] interactive-timeout
4.修改多实例的文件权限
[root@mysql ~]# chown -R mysql.mysql /data/ [root@mysql ~]# find /data/ -name mysql [root@mysql ~]# find /data/ -name mysql -exec chmod 700 {} \; [root@mysql ~]# find /data -name mysql -exec ls -l {} \;
5.初始化数据库
[root@mysql ~]# cd /app/mysql/scripts [root@mysql scripts]# ./mysql_install_db --basedir=/app/mysql --datadir=/data/3306/data --user=mysql [root@mysql scripts]# ./mysql_install_db --basedir=/app/mysql --datadir=/data/3307/data --user=mysql [root@mysql ~]$ tree /data/
6.mysql多实例的连接及密码添加
mysql多实例的连接需要指定sock,oracle多实例连接时指定的是SID
[root@mysql ~]# mysql -S /data/3306/mysql.sock [root@mysql ~]# mysql -S /data/3307/mysql.sock [root@mysql ~]# mysqladmin -u root -S /data/3307/mysql.sock password ‘123456‘ [root@mysql ~]# mysqladmin -u root -S /data/3306/mysql.sock password ‘123456‘
7.MySQL多实例的启动、连接及关闭
启动多实例,可以使用单独的脚本启动,也可以使用mysqld_safe指定配置文件启动,也可以使用mysqld_multi进行启动,但是这种方式就不是各个实例单独的配置文件了,此次使用命令及单独的脚本进行操作。其实本质也就是mysql_safe以及mysqladmin的操作,只是使用脚本方便而已。
7.1 mysql_safe命令启动以及关闭
[root@mysql ~]# mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null & [root@mysql ~]# mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null & [root@mysql ~]# netstat -lntup | grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 36549/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 35835/mysqld [root@mysql ~]# mysqladmin -uroot -p123456 -S /data/3307/mysql.sock shutdown [root@mysql ~]# mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown [root@mysql ~]# netstat -lntup | grep 330
7.2 使用各自的脚本进行启动、关闭
vi /data/3306/mysql
需要注意的是mysqld_safe、mysqladmin命令全路径
#!/bin/bash ##### mysql_port=3306 mysql_username="root" mysql_password="123456" function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /app/mysql/bin/mysqld_safe --defaults-file=/data/${mysql_port}/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /app/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/${mysql_port}/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql function_start_mysql } function_kill_mysql() { kill -9 $(ps -ef | grep ‘bin/mysqld_safe‘ | grep ${mysql_port} | awk ‘{printf $2}‘) kill -9 $(ps -ef | grep ‘libexec/mysqld‘ | grep ${mysql_port} | awk ‘{printf $2}‘) } case $1 in start) function_start_mysql;; stop) function_stop_mysql;; kill) function_kill_mysql;; restart) function_stop_mysql function_start_mysql;; *) echo "Usage: /data/${mysql_port}/mysqld {start|stop|restart|kill}";; esac
vi /data/3307/mysql
#!/bin/bash ##### mysql_port=3307 mysql_username="root" mysql_password="123456" function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /app/mysql/bin/mysqld_safe --defaults-file=/data/${mysql_port}/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /app/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/${mysql_port}/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql function_start_mysql } function_kill_mysql() { kill -9 $(ps -ef | grep ‘bin/mysqld_safe‘ | grep ${mysql_port} | awk ‘{printf $2}‘) kill -9 $(ps -ef | grep ‘libexec/mysqld‘ | grep ${mysql_port} | awk ‘{printf $2}‘) } case $1 in start) function_start_mysql;; stop) function_stop_mysql;; kill) function_kill_mysql;; restart) function_stop_mysql function_start_mysql;; *) echo "Usage: /data/${mysql_port}/mysqld {start|stop|restart|kill}";; esac
添加可执行权限
chmod +x /data/3306/mysql chmod +x /data/3307/mysql
使用脚本启动、关闭MySQL多实例
[root@mysql ~]# /data/3306/mysql start Starting MySQL... [root@mysql ~]# lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 4617 mysql 13u IPv4 14657 0t0 TCP *:mysql (LISTEN) [root@mysql ~]# [root@mysql ~]# /data/3307/mysql start Starting MySQL... [root@mysql ~]# lsof -i:3307 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 4937 mysql 13u IPv4 14933 0t0 TCP *:opsession-prxy (LISTEN) [root@mysql ~]# /data/3307/mysql stop Stoping MySQL... [root@mysql ~]# lsof -i:3307 [root@mysql ~]# /data/3306/mysql stop Stoping MySQL... [root@mysql ~]# lsof -i:3306
8.打开远程连接访问
[root@mysql ~]# mysql -uroot -p123456 -S /data/3306/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.45-log Source distribution Copyright (c) 2000, 2015, 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> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘192.168.10.%‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [root@mysql ~]# mysql -uroot -p123456 -S /data/3307/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.45-log Source distribution Copyright (c) 2000, 2015, 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> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘192.168.10.%‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) mysql>
9.添加到开机启动
vi /etc/rc.d/rc.local /data/3306/mysql start /data/3307/mysql start
本文出自 “相守姑娘说” 博客,请务必保留此出处http://sugarlovecxq.blog.51cto.com/6707742/1695946
原文地址:http://sugarlovecxq.blog.51cto.com/6707742/1695946