码迷,mamicode.com
首页 > 数据库 > 详细

MySQL-5.5.49安装、多实例、主从复制

时间:2017-09-13 00:24:45      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:关闭自动   type   server   commit   gbk   pts   basedir   restart   lock   

源码安装mysql

yum install ncurses-devel libaio-devel -y
mkdir /server/tools -p
cd /server/tools
wget https://cmake.org/files/v2.8/cmake-2.8.8.tar.gz
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
gmake 
gmake install
which cmake
/usr/local/bin/cmake
cd ..
useradd -s /sbin/nologin -M mysql
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49.tar.gz
tar xf mysql-5.5.49.tar.gz
cd mysql-5.5.49
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.49 -DMYSQL_DATADIR=/application/mysql-5.5.49/data -DMYSQL_UNIX_ADDR=/application/mysql-5.5.49/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0

make 
make install
ln -s /application/mysql-5.5.49/ /application/mysql
ls /application/mysql

 二进制包安装mysql

useradd -s /sbin/nologin -M mysql
mkdir /application 
mkdir /server/tools -p
cd /server/tools
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49-linux2.6-x86_64.tar.gz
tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
ln -s /application/mysql-5.5.49/ /application/mysql
sed -i ‘s#/usr/local/#/application/#g‘ /application/mysql/bin/mysqld_safe
cd /application/mysql

 创建多实例配置文件和启动脚本

mkdir -p /data/{3306,3307}/data

vim /data/3306/my.cnf       #3306配置文件

[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
pid-file = /data/3306/mysql.pid
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
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
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/mysql_peter3306.err
pid-file=/data/3306/mysqld.pid

vim /data/3307/my.cnf       #3307配置文件

[client]
port            = 3307
socket          = /data/3307/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user            = mysql
port            = 3307
socket          = /data/3307/mysql.sock
basedir         = /application/mysql
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
pid-file = /data/3307/mysql.pid
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
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db = mysql

server-id = 3

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/3307/mysql_peter3307.err
pid-file=/data/3307/mysqld.pid

vim /data/3306/mysql       #3306启动脚本

#!/bin/bash
####################################
#this scripts is created by peter at 2017-08-31
#mail:asdftttt@163.com
####################################
#init
port=3306
mysql_user="root"
mysql_pwd="123456"
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 "Stopping 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

vim /data/3307/mysql       #3307启动脚本

#!/bin/bash
####################################
#this scripts is created by peter at 2017-08-31
#mail:asdftttt@163.com
####################################
#init
port=3307
mysql_user="root"
mysql_pwd="123456"
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 "Stopping 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
│   └── mysql
└── 3307
    ├── data
    ├── my.cnf
└── mysql

 配置文件权限,将mysql客户端命令加入路径

chown -R mysql.mysql /data
find /data -type f -name mysql | xargs chmod 700       // 启动文件中有数据库管理员密码
find /data -type f -name mysql -exec ls -l {} \;
-rwx------ 1 mysql mysql 1203 Sep  2 21:12 /data/3307/mysql
-rwx------ 1 mysql mysql 1204 Sep  2 21:12 /data/3306/mysql
ln -s /application/mysql/bin/* /usr/local/sbin

 初始化数据库,启动多实例,加入开机启动

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
chown -R mysql.mysql /data
/data/3306/mysql start
/data/3307/mysql start
netstat -nutlp | grep 330
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      27934/mysqld       
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28653/mysqld 
echo "#mysql multi instances" >> /etc/rc.local
echo "/data/3306/mysql start" >> /etc/rc.local
echo "/data/3307/mysql start" >> /etc/rc.local
tail -3 /etc/rc.local
#mysql multi instances
/data/3306/mysql start
/data/3307/mysql start

 登录不同实例,设置管理员密码

mysql -S /data/3306/mysql.sock
mysql -S /data/3307/mysql.sock
mysqladmin -u root -S /data/3306/mysql.sock password ‘123456‘
mysqladmin -u root -S /data/3307/mysql.sock password ‘123456‘
mysql -uroot -p123456 -S /data/3306/mysql.sock
mysql -uroot -p123456 -S /data/3306/mysql.sock

 如何再增加一个实例?

#多实例本质是多端口,多配置文件。添加的流程是改配置,改启动脚本,初始化,给权限,启动
mkdir -p /data/3308/data \cp /data/3306/my.cnf /data/3308/ \cp /data/3306/mysql /data/3308/ sed -i ‘s#3306#3308#g‘ /data/3308/my.cnf sed -i ‘s#server-id = 1#server-id = 8#g‘ /data/3308/my.cnf sed -i ‘s#3306#3308#g‘ /data/3308/mysql chmod 700 /data/3308/mysql /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3308/data --user=mysql chown -R mysql.mysql /data/3308 /data/3308/mysql start echo "/data/3308/mysql start" >> /etc/rc.local netstat -nutlp | grep 3308 tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 29518/mysqld

 单机多实例主从复制

主库:Master 3306
从库:Slave1 3307
从库:Slave2 3308

#Master
vim /data/3306/my.cnf

[mysqld]
...
server-id = 1
log-bin = /data/3306/mysql-bin

/data/3306/mysql restart

mysql -uroot -p123456 -S /data/3306/mysql.sock
mysql>show variables like ‘server_id‘;
mysql>show variables like ‘log_bin‘;
mysql>grant replication slave on *.* to ‘rep‘@‘172.16.1.%‘ identified by ‘123456‘;
mysql>flush privileges;
mysql>select user,host from mysql.user;
mysql>show grants for ‘rep‘@‘172.16.1.%‘;
mysql>flush table with read lock;  // 窗口不能关闭,关闭自动解锁
mysql>show master status;

#新开窗口
mkdir /server/backup/ -p
mysqldump -uroot -p123456 -S /data/3306/mysql.sock --events -A -B | gzip > /server/backup/mysql_bak.$(date +%F).sql.gz
ls -l /server/backup

mysql>show master status;
mysql>unlock tables;

#Slave1
vim /data/3307/my.cnf

[mysqld]
...
server-id = 3  // id必须唯一

/data/3307/mysql restart

cd /server/backup
ls -l
gzip -d mysql_bak.2017-09-03.sql.gz   
mysql -uroot -p123456 -S /data/3307/mysql.sock < mysql_bak.2017-09-03.sql
mysql -uroot -p123456 -S /data/3307/mysql.sock << EOF
CHANGE MASTER TO
MASTER_HOST=‘172.16.1.34‘,
MASTER_PORT=3306,
MASTER_USER=‘rep‘,
MASTER_PASSWORD=‘123456‘,
MASTER_LOG_FILE=‘mysql-bin.000001‘,
MASTER_LOG_POS=332;
EOF
cat /data/3307/data/master.info
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "start slave;"
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show slave status\G;" | egrep "IO_Running|SQL_Running|_Behind_Master"

#Master
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "create database peter;"

#Slave1
mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show databases like ‘peter‘;"

 

MySQL-5.5.49安装、多实例、主从复制

标签:关闭自动   type   server   commit   gbk   pts   basedir   restart   lock   

原文地址:http://www.cnblogs.com/Peter2014/p/7512568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!