mysql真实环境搭建主从
MySQL真实环境搭建主从
创新互联-专业网站定制、快速模板网站建设、高性价比禹城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式禹城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖禹城地区。费用合理售后完善,10年实体公司更值得信赖。
防伪码:人之所以能,是相信能。
前言:当今数据库有oracle mysql SQL Server ACCESS 等等很多种,今天我们来真实环境搭建mysql主从。先说一下四种数据库的区别:
1.四种数据库的区别:
ACCESS:功能相对不是那么强大,主要是开发单机版软件中经常用到。
SQL Server:是目前应用比较广泛和普遍的一款数据库,是数据库发展的一个里程碑。
MySQL:是一个开源的关系数据库管理系统,有快速、可靠和易于使用的特点;MySQL服务器工作在客户/服务器或嵌入系统中。
Oracle:Oracle的功能比较强大,一般用于超大型管理系统软件的建立,Oracle良好的兼容性、可移植性、可连接性和高生产率使Oracle RDBMS具有良好的开放性。现在的应用范围也已经比较广泛。
下面让我们来搭建主从:
操作系统centos6.5_64
数据库版本mysql5.6.29
主数据库 192.168.226.133
从数据库192.168.226.134
mysql同步帐户root/root
一.安装mysql准备
1、下载并上传mysql源码包mysql-5.6.29.tar.gz
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.29.tar.gz
2、检查并卸载系统中低版本的mysql
rpm -qa |grep mysql
yum remove mysql mysql-server mysql-libs
3、安装必要的系统插件
yum install ncurses-deve cmake
或者
rpm -ivhncurses-devel-5.7-3.20090208.el6.x86_64.rpm
rpm -ivhcmake-2.6.4-5.el6.x86_64.rpm
二、添加用户和组:
添加用户和组:
groupadd mysql
useradd -g mysql mysql -s /usr/sbin/nologin
三、创建目录及授权
mkdir -p /data/mysql/data
mkdir -p /data/mysql/log/iblog
mkdir -p /data/mysql/log/binlog
mkdir -p /data/mysql/log/relaylog
mkdir -p /data/mysql/run
mkdir -p /data/mysql/tmp
chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql
四、开始编译
1、解压
tar zxfmysql-5.6.29.tar.gz
cd mysql-5.6.29
编译安装mysql:
cmake \
-DCMAKE_INSTALL_PREFIX=/data/mysql \
-DINSTALL_DATADIR=/data/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_SSL=yes \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DSYSCONFDIR=/etc \
-DWITH_READLINE=on
make
make install
五、修改配置文件my.cnf参数
cp /data/mysql/support-files/my-default.cnf /data/mysql/my.cnf
修改主数据库配置文件my.cnf
vi /data/mysql/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
port = 3306
socket =/data/mysql/run/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
##enable autocommit
autocommit=1
general_log=off
explicit_defaults_for_timestamp=true
# system
basedir=/data/mysql
datadir=/data/mysql/data/
max_allowed_packet=134217728
max_connections=8192
max_user_connections=8000
open_files_limit=65535
pid_file=/data/mysql/run/mysqld.pid
port=3306
server_id=128
skip_name_resolve=ON
socket=/data/mysql/run/mysql.sock
tmpdir=/data/mysql/tmp
# binlog
binlog_cache_size=32768
binlog_format=row
expire_logs_days=15
log-bin=/data/mysql/log/binlog/master-bin
log-bin-index=/data/mysql/log/binlog/master-bin.index
log_slave_updates=ON
max_binlog_cache_size=2147483648
max_binlog_size=524288000
sync_binlog=100
#relay
# LOGGING #
log_error = /data/mysql/log/alert.log
log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /data/mysql/log/slow.log
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1
#slave#
slave_skip_errors=OFF
log_slave_updates=ON
# innodb #
innodb_log_group_home_dir=/data/mysql/log/iblog
innodb_data_home_dir=/data/mysql/log/iblog
innodb_adaptive_flushing=1
innodb_additional_mem_pool_size=20M
innodb_buffer_pool_instances=8
innodb_change_buffering=inserts
innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 4
innodb_log_file_size = 100M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 128M
innodb_file_format=Barracuda
innodb_file_io_threads=4
innodb_flush_neighbors=0
innodb_io_capacity=200
innodb_lock_wait_timeout=5
innodb_log_buffer_size=64M
innodb_lru_scan_depth=2048
innodb_max_dirty_pages_pct=60
innodb_old_blocks_time=1000
innodb_online_alter_log_max_size=200M
innodb_open_files=200
innodb_print_all_deadlocks=1
innodb_purge_threads=4
innodb_read_ahead_threshold=0
innodb_read_io_threads=8
innodb_rollback_on_timeout=0
innodb_sort_buffer_size=2M
innodb_spin_wait_delay=6
innodb_stats_on_metadata=0
innodb_strict_mode=1
innodb_sync_array_size=256
innodb_sync_spin_loops=30
innodb_thread_concurrency=64
innodb_use_native_aio=0
innodb_write_io_threads=8
innodb_support_xa=1
[mysqld_safe]
datadir=/data/mysql/data/
修改从数据库my.cnf配置文件:
vi /data/mysql/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
port = 3306
socket =/data/mysql/run/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
##enable autocommit
autocommit=1
general_log=off
explicit_defaults_for_timestamp=true
# system
basedir=/data/mysql
datadir=/data/mysql/data/
max_allowed_packet=134217728
max_connections=8192
max_user_connections=8000
open_files_limit=65535
pid_file=/data/mysql/run/mysqld.pid
port=3306
server_id=240
skip_name_resolve=ON
socket=/data/mysql/run/mysql.sock
tmpdir=/data/mysql/tmp
# binlog
binlog_cache_size=32768
binlog_format=row
expire_logs_days=15
log-bin=/data/mysql/log/binlog/slave-bin
log-bin-index=/data/mysql/log/binlog/slave-bin.index
log_slave_updates=ON
max_binlog_cache_size=2147483648
max_binlog_size=524288000
sync_binlog=100
#relay
relay-log=/data/mysql/log/relaylog/slave-relay-bin
relay-log-index=/data/mysql/log/relaylog/slave-relay-bin.index
# LOGGING #
log_error = /data/mysql/log/alert.log
log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /data/mysql/log/slow.log
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1
#slave#
slave_skip_errors=OFF
log_slave_updates=ON
# innodb #
innodb_log_group_home_dir=/data/mysql/log/iblog
innodb_data_home_dir=/data/mysql/log/iblog
innodb_adaptive_flushing=1
innodb_additional_mem_pool_size=20M
innodb_buffer_pool_instances=8
innodb_change_buffering=inserts
innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 4
innodb_log_file_size = 100M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 128M
innodb_file_format=Barracuda
innodb_file_io_threads=4
innodb_flush_neighbors=0
innodb_io_capacity=200
innodb_lock_wait_timeout=5
innodb_log_buffer_size=64M
innodb_lru_scan_depth=2048
innodb_max_dirty_pages_pct=60
innodb_old_blocks_time=1000
innodb_online_alter_log_max_size=200M
innodb_open_files=200
innodb_print_all_deadlocks=1
innodb_purge_threads=4
innodb_read_ahead_threshold=0
innodb_read_io_threads=8
innodb_rollback_on_timeout=0
innodb_sort_buffer_size=2M
innodb_spin_wait_delay=6
innodb_stats_on_metadata=0
innodb_strict_mode=1
innodb_sync_array_size=256
innodb_sync_spin_loops=30
innodb_thread_concurrency=64
innodb_use_native_aio=0
innodb_write_io_threads=8
innodb_support_xa=1
[mysqld_safe]
datadir=/data/mysql/data/
六、执行MySQL安装脚本
cd /data/mysql/
./scripts/mysql_install_db --defaults-file=/data/mysql/my.cnf --user=mysql --datadir=/data/mysql/data
七、修改系统环境变量
vi /etc/profile
PATH=/data/mysql/bin:$PATH:/sbin
八、自启动脚本
cp /data/mysql/my.cnf /etc/my.cnf
cp /data/mysql/support-files/mysql.server /etc/init.d/mysql
vi /etc/init.d/mysql
修改mysqld_pid_file_path=/data/mysql/run/mysqld.pid
chkconfig --add mysql
/etc/init.d/mysql start
/etc/init.d/mysql stop
主上做相应的配置从上也做如上相应的配置。
九、配置主从
在主服务器上登陆数据库,并配置数据库
mysql -h227.0.0.1 -uroot -P3306
注意:如果此报错。
原因: 这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令:
ln -s /data/mysql/bin/mysql /usr/bin
即可解决问题。
mysql>set old_passwords=0;
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
|master-bin.000003 | 333| | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
需要用到上面标注红色的两个参数
在从服务器上登陆数据库,并配置数据库
mysql -h227.0.0.1 -uroot -P3306
mysql>set old_passwords=0;
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql>stop slave;
mysql> CHANGE MASTER TO MASTER_HOST='10.140.19.189',MASTER_USER='root',MASTER_PASSWORD='root' ,MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=333;
mysql>start slave;
mysql>show slave status \G
在打印出来的状态下,查看以下两个参数的值:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果这两个参数的值都是yes,则运行正常,可以在master数据库上添加一个数据库或者添加一张表,检查slave数据库上是否存在。
如果 这两个参数有任何一个不是 Yes,则说明存在问题。可以查看slave上的数据库错误日志文件查看错误原因。
十、常见的错误原因
1、server-id一致。
2、用户权限不够。需要的权限包括:REPLICATION SLAVE,RELOAD,CREATE USER,SUPER。
GRANT REPLICATION SLAVE,RELOAD,CREATE USER,SUPER ON *.* TO {USER}@{IP_ADDR} WITH GRANT OPTION;
3、数据库UUID一致,如果mysql安装时是通过批量复制安装的,则有可能数据库的UUID一致,进入数据库的datadir目录,修改auto.cnf。随意修改下uuid中的值,重启mysqld服务即可。
4、Master数据库端口被防火墙阻挡。
希望能帮到大家!谢谢!
分享标题:mysql真实环境搭建主从
路径分享:http://myzitong.com/article/jjepep.html