本文共 7423 字,大约阅读时间需要 24 分钟。
二进制安装:[root@lufengcentos ~]# mkdir /home/lufeng/tools -p[root@lufengcentos ~]# cd /home/lufeng/tools [root@lufengcentos tools]# rz[root@lufengcentos tools]# tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz[root@lufengcentos tools]# mkdir -p /application/mysql-5.5.49[root@lufengcentos tools]# mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49创建mysql用户|组[root@lufengcentos tools]# groupadd mysql[root@lufengcentos tools]# useradd mysql -g mysql -s /sbin/nologin -M[root@lufengcentos tools]# ln -s /application/mysql-5.5.49/ /application/mysqlroot@lufengcentos mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe创建多实例数据文件目录[root@Mysql-server_2 ~]# mkdir -p /data/{3306,3307}/data配置相应的配置文件:[root@Mysql-server_2 ~]# cat /data/3306/my.cnf ####################################################[client]#password = your_passwordport = 3306socket = /data/3306/mysql.sock[mysqld]user = mysqlport = 3306socket = /data/3306/mysql.sockbasedir = /application/mysqldatadir = /data/3306/datapid-file = /data/3306/mysql.pidrelay-log = /data/3306/relay-binrelay-log-info-file = /data/3306/relay-log.infoskip-external-lockingkey_buffer_size = 16Kmax_allowed_packet = 1Mtable_open_cache = 4sort_buffer_size = 64Kread_buffer_size = 256Kread_rnd_buffer_size = 256Knet_buffer_length = 2Kthread_stack = 128Kserver-id = 1[mysqldump]quickmax_allowed_packet = 2M[mysql]no-auto-rehash[mysql_safe]log-error=/data/3306/mysql_lufeng3306.errpid-file=/data/3306/mysqlf.pid##############################################[root@Mysql-server_2 ~]# cat /data/3307/my.cnf #############################################[client]#password = your_passwordport = 3307socket = /data/3307/mysql.sock[mysqld]user = mysqlport = 3307socket = /data/3307/mysql.sockbasedir = /application/mysqldatadir = /data/3307/datapid-file = /data/3307/mysql.pidrelay-log = /data/3307/relay-binrelay-log-info-file = /data/3307/relay-log.infoskip-external-lockingkey_buffer_size = 16Kmax_allowed_packet = 1Mtable_open_cache = 4sort_buffer_size = 64Kread_buffer_size = 256Kread_rnd_buffer_size = 256Knet_buffer_length = 2Kthread_stack = 128Kserver-id = 2[mysqldump]quickmax_allowed_packet = 2M[mysql]no-auto-rehash[mysql_safe]log-error=/data/3307/mysql_lufeng3307.errpid-file=/data/3307/mysqlf.pid##############################################创建启动文件[root@Mysql-server_2 ~]# cat /data/3306/mysql#!/bin/sh#################################mysql 3306 by lufeng @2017.4.3#################################intport=3306mysql_user="root"mysql_pwd="199429"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 functionfunction_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 functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql}case $1 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /data/${port}/mysql {start|stop|restart}\n"esac##########################################################[root@Mysql-server_2 ~]# cat /data/3307/mysql#!/bin/sh#################################mysql 3307 by lufeng @2017.4.3#################################intport=3307mysql_user="root"mysql_pwd="199429"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 functionfunction_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 functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql}case $1 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /data/${port}/mysql {start|stop|restart}\n"esac#######################################################配置多实例文件权限[root@Mysql-server_2 ~]# chown -R mysql.mysql /data[root@Mysql-server_2 ~]# find /data -name mysql|xargs chmod 700[root@Mysql-server_2 ~]# find /data -name mysql -exec ls -l {} \; -rwx------ 1 mysql mysql 999 4月 3 13:33 /data/3306/mysql-rwx------ 1 mysql mysql 1210 4月 3 13:35 /data/3307/mysql全局变量:[root@Mysql-server_2 ~]# ls /application/mysql/bin/mysql/application/mysql/bin/mysql[root@Mysql-server_2 ~]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile[root@Mysql-server_2 ~]# tail -1 /etc/profileexport PATH=/application/mysql/bin:$PATH[root@Mysql-server_2 ~]# source /etc/profile[root@Mysql-server_2 ~]# echo $PATH/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin初始化数据库文件[root@Mysql-server_2 scripts]# cd /application/mysql/scripts/[root@Mysql-server_2 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql[root@Mysql-server_2 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysqlInstalling MySQL system tables...170403 13:47:58 [Note] /application/mysql/bin/mysqld (mysqld 5.5.49) starting as process 2971 ...OKFilling help tables...170403 13:47:59 [Note] /application/mysql/bin/mysqld (mysqld 5.5.49) starting as process 2980 ...OK============成功标志启动命令:[root@Mysql-server_2 ~]# /data/3306/mysql startStarting MySQL... [root@Mysql-server_2 ~]# /data/3307/mysql startStarting MySQL...[root@Mysql-server_2 ~]# netstat -lntup|grep mysqltcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3638/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3962/mysqld 配置开机自启动:[root@Mysql-server_2 ~]# echo "mysql multi instances" >>/etc/rc.local[root@Mysql-server_2 ~]# echo "/data/3306/mysql start" >>/etc/rc.local [root@Mysql-server_2 ~]# echo "/data/3307/mysql start" >>/etc/rc.local [root@Mysql-server_2 ~]# tail -3 /etc/rc.local mysql multi instances/data/3306/mysql start/data/3307/mysql start登录:[root@lufengcentos ~]# mysql -S /data/3306/mysql.sock 安全配置:[root@lufengcentos ~]# mysqladmin -u root -S /data/3307/mysql.sock password '199429'[root@lufengcentos ~]# mysql -S /data/3306/mysql.sock -uroot -pEnter password:
转载于:https://blog.51cto.com/bestlufeng/1912675