432423423 发表于 2017-10-31 15:42:56

mysq之配置多实例

1.准备工作
sudo su -                使用sudo提权登陆
pkill mysqld         杀掉启动项
ps -ef |grep mysqld查看
rm-f/etc/init.d/mysqld   删除启动项

2.建立文件夹,为多实例配置做准备
mkdir -p /data/{3306,3307}/data       创建两个 3306,3307文件目录。前一个/data ,不要写成这样,会变成/data3306,/data3307.连个文件目录
yum isntalltree -y                安装tree
tree       /data/                         查看目录层结构
/data/
├── 3306
│   └── data
└── 3307
    └── data


添加mysql5.5多实例my.cnf文件
vi /data/3306/my.cnf             按 ‘i’进入编辑模式       保存 按esc 后输入   :wq进行保存


port            = 3306
socket          = /data/3306/mysql.sock

no-auto-rehash

#skip-grant-tables
user      = mysql
port            = 3306
socket          = /data/3306/mysql.sock
basedir   = /application/mysql-5.5.32
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

quick
max_allowed_packet                      = 2M

log-error                                       = /data/3306/mysql_zexi3306.err
pid_file                                        = /data/3306/mysqld.pid
vi /data/3307/my.cnf         按 ‘i’进入编辑模式   保存 按esc 后输入   :wq进行保存
添加配置如下:

port            = 3307
socket          = /data/3307/mysql.sock

no-auto-rehash

user      = mysql
port            = 3307
socket          = /data/3307/mysql.sock
basedir   = /application/mysql-5.5.32               :这个安装位置一定要弄清楚
datadir         = /data/3307/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/3307/error.log
#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
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                                       = 2
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

quick
max_allowed_packet                      = 2M

log-error                                       = /data/3307/mysql_zexi3307.err
pid_file                                        = /data/3307/mysqld.pid


多实例启动文件启动mysql
vi /data/3306/mysqld   ,脚本内容如下:
############2017y,
###mysql start script
##### ......oldboy vido
#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
vi /data/3307/mysqld   ,脚本内容如下:
############2017y,
###mysql start script
##### ......oldboy vido
#init
port=3307
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

# tree /data/
/data/
├── 3306
│   ├── data
│   ├── my.cnf
│   └── mysqld
└── 3307
    ├── data
    ├── my.cnf
    └── mysqld




3.添加权限
chown -Rmysql.mysql    /data/      修改用户合组为mysql
find /data -type f -name "mysql"|xargs   ls-l      查看权限
find /data -type f -name "mysql"|xargschmod +x       为多实例启动项添加执行权限
find /data -type f -name "mysql"|xargs   ls-l      查看权限


4.实例

# tail -1 /etc/profile                      查看文件的变量
export PATH=/application/mysql/bin:$PATH
cd /application/mysql/scripts/                                    进入到目录下配置实例
./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql                                                          配置3306的实例
./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql                                                       配置3307的实例
实例怎么看才算是正确的?有这两个ok。
Installing MySQL system tables...
OK
Filling help tables
OK

为什么要初始化?
a.初始化的主要目的是创建基础 的数据库文件,例如:生成mysql库表等
b.初始化后可以查看对应实例数据目录,例如   tree /data/ ,可以看到生成了很多库表。
tree /data/3306/data/                                        初始化完成后查看,多出很多东西


5. 启动mysql,并进入数据库

/data/3306/mysql start                                  启动3306实例
/data/3307/mysql start                                  启动3307实例
netstat -lntup|grep 330             查看启动项
tcp      0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      19078/mysqld      
tcp      0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      19821/mysqld

mysql-S /data/3306/mysql.sock       登陆数据库

mysql> create database d3306;               添加3306数据库
mysql-S /data/3307/mysql.sock         登陆数据库
mysql> create database d3307;               添加3306数据库
mysql> show databases ;               查看数据库


system mysql -S /data/3306/mysql.sock      使用system命令,进入到3306里去。
mysql> show databases;                是在3306数据库里查询的命令

6.修改多实例数据库密码


/data/3306/mysql stop                  停止数据库
/data/3307/mysql stop                  停止数据库
停止数据库时,发现停止不了???
# /data/3306/mysql stop
Stoping Mysql...
/application/mysql/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'


vi /data/3306/mysql                            编辑 mysql启动文件
mysql_pwd=""                                    把密码置为空
/data/3306/mysql stop                        停止3306
vi /data/3307/mysql                              编辑 mysql启动文件
mysql_pwd=""                                    把密码置为空
/data/3307/mysql stop                      停止3306
netstat -lntup |grep 330                  检查



/data/3306/mysql start                         启动3306MySQL数据库
mysqladmin -u root -S /data/3306/mysql.sock passwod 'oldboy123'         修改密码
/data/3307/mysql start                         启动3306MySQL数据库
mysqladmin -u root -S /data/3307/mysql.sock passwod 'oldboy123'         修改密码
# netstat -lntup|grep 330                                             检查启动项
tcp      0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      20679/mysqld      
tcp      0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      21428/mysqld      
vi /data/3306/mysql             mysql_pwd="oldboy23"                修改启动文件密码
vi /data/3307/mysql             mysql_pwd="oldboy23"                修改启动文件密码
find /data/ -type f -name "mysql" -exec chmod 700 {} \;         授权 启动项权限
find /data/ -type f -name "mysql" -exec chown root.root {} \;   授权 启动项用户和组
find /data/ -type f -name "mysql" -exec ls -l {} \;                         查看
-rwx------. 1 root root 1148 10月 29 16:50 /data/3307/mysql
-rwx------. 1 root root 1147 10月 29 16:49 /data/3306/mysql
# pkill mysqld                                              杀掉mysqld
# /data/3306/mysql start                           启动   
# /data/3307/mysql start                            启动
# ss -lntup|grep 330                                    检查
tcp    LISTEN   0      128                  *:3306                  *:*      users:(("mysqld",22216,12))
tcp    LISTEN   0      128                  *:3307                  *:*      users:(("mysqld",22957,12))
# /data/3307/mysql stop                           停止
# /data/3306/mysql stop                              停止
# ss -lntup|grep 330                                     检查
# /data/3306/mysql start                                  重新启动
# /data/3307/mysql start                               重新启动
# ss -lntup|grep 330                                    检查
tcp    LISTEN   0      128                  *:3306                  *:*      users:(("mysqld",23721,12))
tcp    LISTEN   0      128                  *:3307                  *:*      users:(("mysqld",24462,12))


页: [1]
查看完整版本: mysq之配置多实例