MySQL多实例,主从同步
由于背景原因,所做的主从同步还是要基于MySQL 5.1的版本,主从同步主要是一个数据库读写访问原来的数据库热度过大,需要做到使用从库对读分压。
MySQL主从同步介绍
1.建立MySQL 账户
#groupadd mysql #useradd -s /sbin/nologin -g mysql -M mysql
#tail -l /etc/passwd
建立 MySQL 软件目录
#mkdir -p /home/tools
#cd /home/tools/
2.编译安装MySQL 软件(http://down1.chinaunix.net/distfiles/mysql-5.1.62.tar.gz)
#tar zxf mysql-5.1.62.tar.gz #cd mysql-5.1.62
配置
./configure \
--prefix=/usr/local/mysql \
--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \
--localstatedir=/usr/local/mysql/data \
--enable-assembler \
--enable-thread-safe-client \
--with-mysqld-user=mysql \
--with-big-tables \
--without-debug \
--with-pthread \
--enable-assembler \
--with-extra-charsets=complex \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=partition,innobase \
--with-plugin-PLUGIN \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static
3.静态编译生成mysqld的执行文件
#make
4.安装MySQL
#make install
5.获取MySQL 配置文件
#ls -l support-files/*.cnf #cp support-files/my-small.cnf /etc/my.cnf
6.创建数据库文件
#mkdir -p /usr/local/mysql/data #chown -R mysql.mysql /usr/local/mysql
#/usr/local/mysql/bin/mysql_install_db --user=mysql
#
7.启动MySQL 数据库
#cp support-files/mysql.server /usr/local/mysql/bin #netstat -lnt|grep 3306
#/user/local/bin/mysql_safe --user=mysql &
8.配置MySQL 命令的全局使用路径
#echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile #source /etc/profile
9.配置/etc/init.d/mysqld start 方式启动数据库
#cp support-files/mysql.server /etc/init.d/mysqld #chmod 700 /etc/init.d/mysqld
#/etc/init.d/mysqld restart
多实例安装
1.采用不同的端口来作为二级目录
mkdir -p /data/{3306,3307}/data |
ls -l support-files/*.cnf /bin/cp support-files/my-small.cnf /etc/my.cnf |
vi /data/3306/my.cnf vi /data/3307/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 = /usr/local/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/mysql_barry3306.err pid-file=/data/3306/mysqld.pid |
#!/bin/sh
#/data/3306/mysql 脚本
#init port=3306 mysql_user="root" mysql_pwd="" CmdPath="/usr/local/mysql/bin" #startup function function_start_mysql() { printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & } #stop function function_stop_mysql() { printf "Stoping MySQL...\n" ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown } #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 |--my.cnf |--mysql |--data
--3307
|--my.cnf
|--mysql |--data
#授权
chown -R mysql.mysql /data
find /data -name mysql -exec chmod 700 {} \
|
echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile source /etc/profile |
mysql_install_db --datadir=/data/3306/data --user=mysql mysql_install_db --datadir=/data/3307/data --user=mysql |
/data/3306/mysql start /data/3307/mysql start #检查MySQL数据是否启动 netstat -lnt|grep 330[6,7] |
echo "/data/3306/mysql start" >>/etc/rc.local
echo "/data/3307/mysql start" >>/etc/rc.local
|
mysql -S /data/3306/mysql.sock
mysql -S /data/3307/mysql.sock
|
mysqladmin -u root -S /data/3306/mysql.sock password 'barry123' #<- 更改默认密码。 mysql -S /data/3306/mysql.sock #<- 无法直接登录 mysql -uroot -p -S /data/3306/mysql.sock #<-新的登录方式 |
select user,host form mysql.user |
主从复制配置
[mysqld] server-id =1 log-bin=/data/3306/mysql-bin |
grep -E "server-id|log-bin" /data/3306/my.cnf log-bin=/data/3306/mysql-bin server-id=1 |
mysql -uroot -p'' -S /data/3306/mysql.sock grant replication slave on *.* to 'rep'@'10.0.0.%' identified by 'password'; |
flush tables with read lock; interactive_timeout=60 wait_timeout=60 |
mkdir /server/backup/ -p mysqldump -uroot -p'password' -S /data/3306/mysql.sock -A -B |gzip >/server/backup/mysql_bak.${date +%F}.sql.gz ls -l /server/backup/mysql_bak.${date +%F}.sql.gz |
mysql -u root -p'password' -S /data/3306/mysql.sock -e "show master status" |
unlock tables; |
[mysqld] server-id=2 #log-bin=/data/3307/mysql-bin |
gzip -d mysql_bak.2014-04-17.sql.gz mysql -uroot -p'password' -S /data/3307/mysql.sock < mysql_bak.2014-04-17.sql |
mysql -uroot -p'password' -S /data/3307/mysql.sock CHANGE MASTER TO MASTER_HOST='10.0.0.x', <==这里是主库的IP MASTER_PORT=3306, <==这里是主库的端口,从库端口可以和主库不同。 MASTER_USER='rep',<==这里是主库上建立的用于复制的用户rep MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.0000008',<==这里是show master status时看到的查到二进制文件名称 MASTER_POS=342;<==这里是show master status时看到的查看二进制日志偏移量,注意不能多空格。 |
cat |mysql -uroot -p'password' -S /data/3307/mysql.sock<< EOF
CHANGE MASTER TO
MASTER_HOST='10.0.0.x',
MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.0000008', MASTER_LOG_POS=342; EOF
|
CHANGE MASTER TO
MASTER_HOST='192.168.1.234',
MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=261; |
mysql -uroot -p'password' -S /data/3307/mysql.sock -e "start slave;" mysql -uroot -p'password' -S /data/3307/mysql.sock -e "show slave status\G;" |