Mysql多实例安装+主从复制+读写分离 -学习笔记

时间:2022-12-20 08:07:20
<h1>
<span class="link_title"><a href="/wmj2004/article/details/53393528">
Mysql多实例安装+主从复制+读写分离 -学习笔记 </a>
</span> </h1>
<style type="text/css">
.embody{
padding:10px 10px 10px;
margin:0 -20px;
border-bottom:solid 1px #ededed;
}
.embody_b{
margin:0 ;
padding:10px 0;
}
.embody .embody_t,.embody .embody_c{
display: inline-block;
margin-right:10px;
}
.embody_t{
font-size: 12px;
color:#999;
}
.embody_c{
font-size: 12px;
}
.embody_c img,.embody_c em{
display: inline-block;
vertical-align: middle;
}
.embody_c img{
width:30px;
height:30px;
}
.embody_c em{
margin: 0 20px 0 10px;
color:#333;
font-style: normal;
}
</style>
<script type="text/javascript">
$(function () {
try
{
var lib = eval("("+$("#lib").attr("value")+")");
var html = "";
if (lib.err == 0) {
$.each(lib.data, function (i) {
var obj = lib.data[i];
//html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
html += ' <a href="' + obj.url + '" target="_blank">';
html += ' <img src="' + obj.logo + '">';
html += ' <em><b>' + obj.name + '</b></em>';
html += ' </a>';
});
if (html != "") {
setTimeout(function () {
$("#lib").html(html);
$("#embody").show();
}, 100);
}
}
} catch (err)
{ } });
</script>

一. Mysql多实例编译安装

-------------------------------------

1.多实例定义:

同一套mysql程序,不同的“my.cnf,启动程序,端口”。属于逻辑上面的多实例。

2.下载mysql-5.5.32.tar.gz

3.安装相关依赖:

yum install ncurses-devel libaio-devel cmake gcc-c++

4.创建用户:

useradd mysql -s /sbin/nologin -M

5.解压缩开始编译:

  1. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.32/ -DMYSQL_DATADIR=/data/mysql \
  2. -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.5.32/tmp/mysql.sock  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci  \
  3. -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii  -DENABLED_LOCAL_INFILE=ON  -DWITH_INNOBASE_STORAGE_ENGINE=1  \
  4. -DWITH_FEDERATED_STORAGE_ENGINE=1  -DWITH_BLACKHOLE_STORAGE_ENGINE=1  -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1  \
  5. -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1  -DWITH_ZLIB=bundled  -DENABLED_LOCAL_INFILE=1  \
  6. -DWITH_READLINE=1   -DWITH_EMBEDDED_SERVER=1  -DWITH_DEBUG=0
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.32/ -DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.5.32/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

6.创建实例目录:

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

7.创建配置文件:

PS:server-id 每个实例要不一样。

vim /data/3306/my.cnf

  1. [client]
  2. port            = 3306
  3. socket          = /data/3306/mysql.sock
  4. [mysql]
  5. no-auto-rehash
  6. [mysqld]
  7. user    = mysql
  8. port    = 3306
  9. socket  = /data/3306/mysql.sock
  10. basedir = /usr/local/mysql-5.5.32
  11. datadir = /data/3306/data
  12. open_files_limit    = 1024
  13. character-set-server = utf8
  14. back_log = 600
  15. max_connections = 800
  16. max_connect_errors = 3000
  17. table_cache = 614
  18. external-locking = FALSE
  19. max_allowed_packet =8M
  20. sort_buffer_size = 1M
  21. join_buffer_size = 1M
  22. thread_cache_size = 100
  23. thread_concurrency = 2
  24. query_cache_size = 2M
  25. query_cache_limit = 1M
  26. query_cache_min_res_unit = 2k
  27. #default_table_type = InnoDB
  28. thread_stack = 192K
  29. #transaction_isolation = READ-COMMITTED
  30. tmp_table_size = 2M
  31. max_heap_table_size = 2M
  32. long_query_time = 1
  33. #log_long_format
  34. #log-error = /data/3306/error.log
  35. #log-slow-queries = /data/3306/slow.log
  36. pid-file = /data/3306/mysql.pid
  37. log-bin = /data/3306/mysql-bin
  38. relay-log = /data/3306/relay-bin
  39. relay-log-info-file = /data/3306/relay-log.info
  40. binlog_cache_size = 1M
  41. max_binlog_cache_size = 1M
  42. max_binlog_size = 2M
  43. expire_logs_days = 7
  44. key_buffer_size = 16M
  45. read_buffer_size = 1M
  46. read_rnd_buffer_size = 1M
  47. bulk_insert_buffer_size = 1M
  48. #myisam_sort_buffer_size = 1M
  49. #myisam_max_sort_file_size = 10G
  50. #myisam_max_extra_sort_file_size = 10G
  51. #myisam_repair_threads = 1
  52. #myisam_recover
  53. lower_case_table_names = 1
  54. skip-name-resolve
  55. slave-skip-errors = 1032,1062
  56. #replicate-ignore-db = mysql
  57. server-id = 1
  58. innodb_additional_mem_pool_size = 4M
  59. innodb_buffer_pool_size = 16G                    #设置成内存的一半最好
  60. innodb_data_file_path = ibdata1:128M:autoextend
  61. innodb_file_io_threads = 4
  62. innodb_thread_concurrency = 8
  63. innodb_flush_log_at_trx_commit = 2
  64. innodb_log_buffer_size = 2M
  65. innodb_log_file_size = 4M
  66. innodb_log_files_in_group = 3
  67. innodb_max_dirty_pages_pct = 90
  68. innodb_lock_wait_timeout = 120
  69. innodb_file_per_table = 0
  70. [mysqldump]
  71. quick
  72. max_allowed_packet = 2M
  73. [mysqld_safe]
  74. log-error=/data/3306/mysql_3306.err
  75. pid-file=/data/3306/mysqld.pid
[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-5.5.32

datadir = /data/3306/data

open_files_limit    = 1024

character-set-server = utf8

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 = 16G #设置成内存的一半最好

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_3306.err

pid-file=/data/3306/mysqld.pid

vim /data/3307/my.cnf

  1. [client]
  2. port            = 3307
  3. socket          = /data/3307/mysql.sock
  4. [mysql]
  5. no-auto-rehash
  6. [mysqld]
  7. user    = mysql
  8. port    = 3307
  9. socket  = /data/3307/mysql.sock
  10. basedir = /usr/local/mysql-5.5.32
  11. datadir = /data/3307/data
  12. open_files_limit    = 1024
  13. character-set-server = utf8
  14. back_log = 600
  15. max_connections = 800
  16. max_connect_errors = 3000
  17. table_cache = 614
  18. external-locking = FALSE
  19. max_allowed_packet =8M
  20. sort_buffer_size = 1M
  21. join_buffer_size = 1M
  22. thread_cache_size = 100
  23. thread_concurrency = 2
  24. query_cache_size = 2M
  25. query_cache_limit = 1M
  26. query_cache_min_res_unit = 2k
  27. #default_table_type = InnoDB
  28. thread_stack = 192K
  29. #transaction_isolation = READ-COMMITTED
  30. tmp_table_size = 2M
  31. max_heap_table_size = 2M
  32. #long_query_time = 1
  33. #log_long_format
  34. #log-error = /data/3307/error.log
  35. #log-slow-queries = /data/3307/slow.log
  36. pid-file = /data/3307/mysql.pid
  37. #log-bin = /data/3307/mysql-bin
  38. relay-log = /data/3307/relay-bin
  39. relay-log-info-file = /data/3307/relay-log.info
  40. binlog_cache_size = 1M
  41. max_binlog_cache_size = 1M
  42. max_binlog_size = 2M
  43. expire_logs_days = 7
  44. key_buffer_size = 16M
  45. read_buffer_size = 1M
  46. read_rnd_buffer_size = 1M
  47. bulk_insert_buffer_size = 1M
  48. #myisam_sort_buffer_size = 1M
  49. #myisam_max_sort_file_size = 10G
  50. #myisam_max_extra_sort_file_size = 10G
  51. #myisam_repair_threads = 1
  52. #myisam_recover
  53. lower_case_table_names = 1
  54. skip-name-resolve
  55. slave-skip-errors = 1032,1062
  56. #replicate-ignore-db = mysql
  57. server-id = 3
  58. innodb_additional_mem_pool_size = 4M
  59. innodb_buffer_pool_size = 16G                    #设置成内存的一半
  60. innodb_data_file_path = ibdata1:128M:autoextend
  61. innodb_file_io_threads = 4
  62. innodb_thread_concurrency = 8
  63. innodb_flush_log_at_trx_commit = 2
  64. innodb_log_buffer_size = 2M
  65. innodb_log_file_size = 4M
  66. innodb_log_files_in_group = 3
  67. innodb_max_dirty_pages_pct = 90
  68. innodb_lock_wait_timeout = 120
  69. innodb_file_per_table = 0
  70. [mysqldump]
  71. quick
  72. max_allowed_packet = 2M
  73. [mysqld_safe]
  74. log-error=/data/3307/mysql_3307.err
  75. pid-file=/data/3307/mysqld.pid
[client]
port            = 3307
socket          = /data/3307/mysql.sock [mysql]

no-auto-rehash [mysqld]

user    = mysql

port    = 3307

socket  = /data/3307/mysql.sock

basedir = /usr/local/mysql-5.5.32

datadir = /data/3307/data

open_files_limit    = 1024

character-set-server = utf8

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 = 3 innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 16G #设置成内存的一半

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_3307.err

pid-file=/data/3307/mysqld.pid

8.创建启动脚本:

vim /data/3306/mysql

#3307只要改动port参数就可以

  1. #!/bin/sh
  2. #init
  3. port=3306
  4. mysql_user="root"
  5. mysql_pwd="Root123"
  6. cmdpath="/usr/local/mysql-5.5.32/bin"
  7. mysql_sock="/data/${port}/mysql.sock"
  8. #startup function
  9. function_start_mysql()
  10. {
  11. if [ ! -e "$mysql_sock" ];then
  12. echo "starting mysql…"
  13. /bin/sh ${cmdpath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /devull &
  14. else
  15. echo "mysql is running…"
  16. exit
  17. fi
  18. }
  19. #stop function
  20. function_stop_mysql()
  21. {
  22. if [ ! -e "$mysql_sock" ];then
  23. echo "mysql is stopped…"
  24. exit
  25. else
  26. echo "stoping mysql…"
  27. ${cmdpath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
  28. fi
  29. }
  30. #restart function
  31. function_restart_mysql()
  32. {
  33. echo "restarting mysql…"
  34. function_stop_mysql
  35. sleep 2
  36. function_start_mysql
  37. }
  38. case $1 in
  39. start)
  40. function_start_mysql
  41. ;;
  42. stop)
  43. function_stop_mysql
  44. ;;
  45. restart)
  46. function_restart_mysql
  47. ;;
  48. *)
  49. echo "usage: /data/${port}/mysql {start|stop|restart}"
  50. esac
#!/bin/sh

init

port=3306

mysql_user="root"

mysql_pwd="Root123"

cmdpath="/usr/local/mysql-5.5.32/bin"

mysql_sock="/data/${port}/mysql.sock"

startup function

function_start_mysql()

{

    if [ ! -e "$mysql_sock" ];then

      echo "starting mysql…"       /bin/sh ${cmdpath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /devull &

    else

      echo "mysql is running…"

      exit

    fi

}

stop function

function_stop_mysql()

{

    if [ ! -e "$mysql_sock" ];then

       echo "mysql is stopped…"

       exit

    else

       echo "stoping mysql…"

       ${cmdpath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

   fi }

restart function

function_restart_mysql()

{

    echo "restarting mysql…"

    function_stop_mysql

    sleep 2

    function_start_mysql

} case $1 in start)     function_start_mysql

;; stop)     function_stop_mysql

;; restart)     function_restart_mysql

;; *)

    echo "usage: /data/${port}/mysql {start|stop|restart}"

esac

9.修改目录权限:

chown -R mysql:mysql /data/

find /data -type f -name mysql -exec chmod 700 {} \;

10.添加环境变量:

echo 'export PATH=/usr/local/mysql-5.5.32/bin/:$PATH' >> /etc/profile

11.初始化数据库:

cd /usr/local/mysql-5.5.32/scripts/

./mysql_install_db --basedir=/usr/local/mysql-5.5.32/ --datadir=/data/3306/data/ --user=mysql

./mysql_install_db --basedir=/usr/local/mysql-5.5.32/ --datadir=/data/3307/data/ --user=mysql

12.启动数据库:

/data/3306/mysql start

/data/3307/mysql start

13.设置数据库root密码:

mysqladmin -uroot password Root123 -S /data/3306/mysql.sock

mysqladmin -uroot password Root456 -S /data/3307/mysql.sock

14.登陆数据库:

mysql -uroot -pRoot123 -S /data/3306/mysql.sock

mysql -uroot -pRoot456 -S /data/3307/mysql.sock

15.远程登陆数据库:

mysql -uroot -pRoot123 -h 172.16.1.214 -P 3306

二. Mysql主从复制基础

-------------------------------------

1.主动复制架构图:

Mysql多实例安装+主从复制+读写分离 -学习笔记

Mysql多实例安装+主从复制+读写分离 -学习笔记

2.主从复制流程图:

Mysql多实例安装+主从复制+读写分离 -学习笔记

3.主从复制条件:

#我们下面的操作是多实例主从复制,3306为主库,3307为从库。

主库要开启log-bin,主库和从库的server-id要不一样。

4.在主库上面创建同步用户:

mysql> grant replication slave on *.* to 'rep'@'172.16.1.%' identified by 'Root1234';    #后面的密码要设置复杂些。

mysql> flush privileges;

5.导出主库数据文件:

  1. 一。常规方法:
  2. mysql> flush table with read lock;     #先锁表,锁表后不能退出mysql窗口,否则失效
  3. mysql> show master status;   #记录bin-log的位置信息
  4. +------------------+----------+--------------+------------------+
  5. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  6. +------------------+----------+--------------+------------------+
  7. | mysql-bin.000004 |      560 |              |                  |
  8. +------------------+----------+--------------+------------------+
  9. mysqldump -uroot -pRoot123 -S /data/3306/mysql.sock --events -A -B|gzip >/tmp/bak_$(date +%F).sql.gz
  10. mysql> unlock tables;   #解锁数据库
  11. 二。快捷方法:
  12. mysqldump -uroot -pRoot123 -S /data/3306/mysql.sock --events -A -B -F --master-data=2 -x|gzip >/tmp/bak_f_$(date +%F).sql.gz
  13. -x 自动锁表
  14. -F --master-data=2    以备注形式记录bin-log的位置。(可以打开文件看头部备注信息)
一。常规方法:
mysql> flush table with read lock; #先锁表,锁表后不能退出mysql窗口,否则失效
mysql> show master status; #记录bin-log的位置信息
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 560 | | |
+------------------+----------+--------------+------------------+
mysqldump -uroot -pRoot123 -S /data/3306/mysql.sock --events -A -B|gzip >/tmp/bak_$(date +%F).sql.gz
mysql> unlock tables; #解锁数据库 二。快捷方法:

mysqldump -uroot -pRoot123 -S /data/3306/mysql.sock --events -A -B -F --master-data=2 -x|gzip >/tmp/bak_f_$(date +%F).sql.gz

-x 自动锁表

-F --master-data=2 以备注形式记录bin-log的位置。(可以打开文件看头部备注信息)

6.还原备份内容到从库:

gzip -d bak_2016-12-02.sql.gz

mysql -uroot -pRoot456 -S /data/3307/mysql.sock < bak_2016-12-02.sql

7.在从库创建master_info文件,并启动同步:

  1. mysql -uroot -pRoot456 -S /data/3307/mysql.sock  <<EOF
  2. CHANGE MASTER TO
  3. MASTER_HOST='172.16.1.214',
  4. MASTER_PORT=3306,
  5. MASTER_USER='rep',
  6. MASTER_PASSWORD='Root1234',
  7. MASTER_LOG_FILE='mysql-bin.000004',
  8. MASTER_LOG_POS=560;
  9. EOF
  10. mysql> start slave;        #开启同步
  11. mysql> show slave status \G ;    #查看同步状态信息
mysql -uroot -pRoot456 -S /data/3307/mysql.sock  <<EOF
CHANGE MASTER TO
MASTER_HOST='172.16.1.214',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='Root1234',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=560;
EOF mysql> start slave; #开启同步

mysql> show slave status \G ; #查看同步状态信息

8.监控mysql主从复制的状态指标:

mysql -uroot -pRoot456 -S /data/3307/mysql.sock -e "show slave status \G ;" | egrep -i "_Running|_Behind"

Slave_IO_Running: Yes                             #同步进程是否启动

            Slave_SQL_Running: Yes                           #sql进程是否启动

        Seconds_Behind_Master: 0                                   #从库同步完成需要的时间

三. Mysql主主复制+读写分离进阶

--------------------------------------------------------------------

1.如果出现同步错误,需要忽略错误,进行下条语句继续同步。

  1. mysql> stop slave;
  2. mysql> set global sql_slave_skip_counter =1;
  3. mysql> start slave
mysql> stop slave;
mysql> set global sql_slave_skip_counter =1;
mysql> start slave

2.通过配置文件自动忽略同步错误“1007”,生产环境主从分离要用:

vim /data/3307/my.cnf

slave-skip-errors = 1032,1062,1007,1008

3.让从库开启bin_log的办法:

vim /data/3307/my.cnf

  1. log-slave-updates
  2. log-bin = /data/3307/mysql-bin
  3. expire_logs_days = 7                #日志只保留7天
log-slave-updates
log-bin = /data/3307/mysql-bin
expire_logs_days = 7 #日志只保留7天

4,.双主库同步方案:

a.让表的ID自增,让主1写1,3,5     主2  写2,4,6。

  1. A增加:
  2. auto_increment_increment = 2
  3. auto_increment_offset=1
  4. B增加:
  5. <p> b.不让表的ID自增,通过WEB程序去seq服务器取ID,写入双主。</p>
A增加:
auto_increment_increment = 2
auto_increment_offset=1
B增加: b.不让表的ID自增,通过WEB程序去seq服务器取ID,写入双主。

5.读写分离的方案:

a.通过开发的程序控制读写的位置。 (推荐)

b.通过开源软件实现。(Amoeba,Mysql_proxy)

c.自己开发DAL层。(大公司用)

6.增量备份步骤:

一。选择一台从库,不对外提供业务,专门用了做备份。此从库开启bin_log功能,做增量备份。

二。备份的时候需要停止sql线程,在用mysqldump备份:

stop slave sql_thread;                 #停止sql线程,不会停止同步IO进程

三。然后将备份文件和bin_log文件远程保存起来。

下面命令可以用来远程备份bin_log:

mysqlbinlog --read-from-remote-server --raw --host=172.16.1.214 --port=3306 --user=rep --password=Root1234 --stop-never mysql-bin.000001

7.主从复制延时原因:

a.从库太多,一般要3-5个

b.从库配置太低。

c.慢sql过多。

d.高版本mysql支持多线程复制

8.同一账号在主从库拥有不同权限方案:

a.在主库上创建拥有读写权限账号,然后再从库上面收回写入权限。

mysql> grant insert,update,delete,select on *.* to web@'172.16.1.%' identified by '123';             #创建web用户对所有表拥有读写权限,主库执行。

mysql> show grants form  web@'172.16.1.%';             #查看用户权限

mysql> REVOKE insert,update,delete ON *.* FROM 'web'@'172.16.1.%';                #回收web用户写入权限,从库执行

b.从库不同步mysql授权表:(推荐方案)

my.cnf 里面加:

replicate-ignore-db = mysql

binlog-ignore-db = mysql

c.在从库设置read-only:

mysql> set global read_only=1;

或者 my.cnf 加 read-only

四. Mysql 灾难恢复演练

--------------------------------------------------------------------

1.主库当机从库接管方案:

a.半同步从库(谷歌半同步插件)。

b.选一台从库什么都不干,等着接管。

c.当机后选举一台从库来当主库。

2.查看从库的同步状态:

mysql> show processlist\G;

3.手动从库提升成主库的步骤:

a.停止同步,重置master。

mysql> stop slave ;

mysql> reset master;

b.删除目录文件里面的master.info 和 relay-bin。

c.取消read-only设置,确认bin-log是否开启。

d.重启数据库。

PS: 有一个软件“MHA”可以自动实现上面操作。

4.mysql的高可用方案:

MYSQL+HA+DRBD

MYSQL+MHA