本文共 5667 字,大约阅读时间需要 18 分钟。
当前我认为数据库主从有两大应用价值:
1.从库相当于主库的备份。虽然数据库的主从并不能代替/取代备份,例如错误的数据可能毁掉所有的数据库,但主从也是在一种可读的状态下保持备份的一种实现方式。
2.从库可以缓解主库的压力,能提高性能。由于从库是只读的,因此在读取查询方面,从库可以代替主库,承担一定的压力,无论是这些压力是来自用户(应用程序)还是开发、运维自己。
MySQL的主从其实并不困难,而且需要做的事情也并不多,mysql官方给出的文档中对主从也做出了详细的指导、说明和解释,但总共也多少篇幅。鉴于很多网友已经详细叙写了相关的文档或文章,此处就能不赘述了(也可以略微参考本文的最后一部分“数据库主从的配置过程中使用的一些命令和步骤”)。
简单说几句关于添加新的从库的小技巧。
因为添加从库时,需要知道MASTER_LOG_FILE和MASTER_LOG_POS,虽然通过在master主库上执行“show master status”能看到当前的MASTER_LOG_FILE和MASTER_LOG_POS,但实际上这种方法只能用于master主库刚开始用的情况下,即数据库刚建好,没有任何数据写入的情况下,或者能将这些数据先手动同步到从库中去。
一种较为方便的方法就是,通过mysqldump导出主库的数据库,同时导出事件、触发器、函数和视图,以及master-data,这样通过查看mysqldump导出的文件的前几行就能找到MASTER_LOG_FILE和MASTER_LOG_POS,这时在将从主库导出的sql语句在从库执行以后,就可以通过这一个MASTER_LOG_FILE和MASTER_LOG_POS,配置好从库,并开始同步。
关于mysqldump的详细说明以及MySQL数据库的的备份脚本可以参考文章《》以及文章中的评论。
通过mysqldump导出主库的数据库,同时导出事件、触发器、函数和视图,以及master-data:
/usr/local/mysql/bin/mysqldump -uusername -hipaddress -ppassword -Pport --routines --events --triggers --single-transaction --flush-logs --master-data=1 –databases databasename
如果想单独导出事件、触发器、函数和视图等而不导出数据,则可以执行:
/usr/local/mysql/bin/mysqldump -uusername -hipaddress -ppassword -Pport --routines --events --triggers --no-create-info --no-data --no-create-db
此外将一个数据库主从的配置过程中使用的一些命令和步骤陈列如下,以作参考或备忘:
# Sat Aug 22 11:23:03 CST 2015# Get mysql 5.5.38 for production use# https://downloads.mariadb.com/archive/index/p/mysql/v/5.5.38# https://downloads.mariadb.com/archives/mysql-5.5/mysql-5.5.38.tar.gzwget -c https://downloads.mariadb.com/archives/mysql-5.5/mysql-5.5.38-linux2.6-x86_64.tar.gz# Remove some conflictsrpm -e mysql-server mysqluserdel mysqlrm -rf /usr/local/mysql/rm -f /etc/my.cnf# Set user and groupgroupadd -r -g 27 mysql useradd -r -u 27 -g mysql mysql -c "MySQL Server" -d /dev/null -s /sbin/nologingroupadd -r mysql useradd -r -g mysql mysql -c "MySQL Server" -d /dev/null -s /sbin/nologin# Install MySQL database tar zxf mysql-5.5.38-linux2.6-x86_64.tar.gzcp -r mysql-5.5.38-linux2.6-x86_64 /usr/local/mysql# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --explicit_defaults_for_timestamp --skip-name-resolve/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --skip-name-resolve# Run it temporarily # /usr/local/mysql/bin/mysqld_safe &/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &# Set mysql commands ln -s /usr/local/mysql/bin/mysql /usr/bin/mysqlln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladminln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldumpln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlogln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config# set mysql root password, etc/usr/local/mysql/bin/mysql_secure_installation# Set mysql library ln -s /usr/local/mysql/lib/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.so.18.0.0ln -s /usr/local/mysql/lib/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.so.18ln -s /usr/local/mysql/lib/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.sols -l /usr/lib64/libmysqlclient.so.18.0.0ls -l /usr/lib64/libmysqlclient.so.18ls -l /usr/lib64/libmysqlclient.sovim /etc/ld.so.conf.d/mysql-x86_64.conf /usr/lib64/mysql /usr/local/mysql/libldconfig# Set mysql replication # master dbcp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnfsed -i "s@server-id\t= 1@server-id = 101@g" /etc/my.cnfsed -i "s@#log-bin=mysql-bin@log-bin=mysql-bin@g" /etc/my.cnfsed -i "s@#binlog_format=mixed@binlog_format=mixed@g" /etc/my.cnf# slave dbcp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnfsed -i "s@server-id\t= 1@server-id = 103@g" /etc/my.cnf# read errors from logscat /usr/local/mysql/data/`hostname`.err# Some reference# server-id = 101# log-bin=/usr/local/mysql/data/bin-log# max_binlog_size = 1500M# binlog_cache_size = 128K# binlog-do-db = devdbops# binlog-ignore-db = mysql# log-slave-updates# expire_logs_day=2# binlog_format=mixed# end mysql temporarilykill -TERM `ps -ef | awk '/mysqld_safe/ && ! /awk/ {print $2}'` || kill -KILL `ps -ef | awk '/mysqld_safe/ && ! /awk/ {print $2}'`ps -ef | awk '/mysqld_safe/ && ! /awk/ {print $2}'kill -TERM `ps -ef | awk '/mysqld/ && ! /awk/ {print $2}'` || kill -KILL `ps -ef | awk '/mysqld/ && ! /awk/ {print $2}'`ps -ef | awk '/mysqld/ && ! /awk/ {print $2}'# Setting sysvinit cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqlchmod +x /etc/init.d/mysql# Start mysql databaseservice mysql startservice mysql status# some operation about replicaion# master dbGRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";FLUSH PRIVILEGES;CREATE USER 'dev'@'%' IDENTIFIED BY 'dev';CREATE DATABASE IF NOT EXISTS devdbops;GRANT ALTER,ALTER ROUTINE,CREATE,CREATE ROUTINE,CREATE TEMPORARY TABLES,CREATE VIEW,DELETE,DROP,EXECUTE,INDEX,INSERT,LOCK TABLES,SELECT,UPDATE,SHOW VIEW ON devdbops.* TO 'dev'@"%";USE devdbops;CREATE TABLE `testtable` (`id` int NOT NULL ,`name` varchar(255) NULL ,`value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,PRIMARY KEY (`id`));INSERT INTO `testtable` (`id`, `name`, `value`) VALUES ('0', 'a', 'b');# CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';# GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';# CREATE USER 'repl'@'%' IDENTIFIED BY 'slavepass';# GRANT REPLICATION SLAVE ON devdbops.* to 'replication'@'%';SHOW MASTER STATUS;SHOW PROCESSLIST \G;SHOW SLAVE HOSTS;QUIT;# slave dbSTOP SLAVE;CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='root',MASTER_PASSWORD='root',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=107;START SLAVE;SHOW SLAVE STATUS \G;# Read for test
tag:mysql主从配置,mysql添加从库,mysql备份视图函数,mysql备份脚本,Linux安装mysql
--end--
转载地址:http://wqfox.baihongyu.com/