本文共 20994 字,大约阅读时间需要 69 分钟。
1.前言
上一篇讲述了如何在CentOS7下编译安装Nginx-1.12.0并如何配置反向代理,本篇将讲述如何编译安装MySQL5.7.18并配置多实例。2.准备2.1下载MySQL5.7.18源码注意最新版本的MySQL需要Boost才能编译安装,在MySQL提供的下载中有不带boost的源码,还有带boost的源码,如果下载不带boost的源码还需要再去下载boost源码,为省事起见,建议下载带boost的源码,下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz (可直接使用wget下载)注:可以打开网页来下载,网页地址为:https://dev.mysql.com/downloads/mysql/,当前最新版本为5.7.18,在“Select Operating System:”处选择“Source Code”,然后在图中选择“Generic Linux (Architecture Independent), Compressed TAR Archive Includes Boost Headers 5.7.18 58.8M (mysql-boost-5.7.18.tar.gz)”,如下图所示: 2.2安装epelEPEL的全称叫 Extra Packages for Enterprise linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。装上了 EPEL之后,就相当于添加了一个第三方源。在编译MySQL过程中需要一些依赖插件,安装epel后可直接通过yum install 来安装。执行命令:yum install epel-release –y
2.3安装cmake
不同于nginx使用make来编译和安装,MySQL使用CMake来编译和安装,安装命令:yum install cmake –y为将来方便还可以把相关的也一并安装了:
yum install autoconf automake libtool2.4安装可能依赖库
yum install -y krb5 krb5-devel libidn libidn-devel openssl openssl-devel -yyum install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devdel -yyum install zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel -yyum install ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel -yyum install perl-Data_dumper python-devel -y
3.编译及安装
3.1解压文件假定mysql-boost-5.7.18.tar.gz文件位于/root/目录下(不管是通过wget直接下载的还是通过下载工具上传的),首先解压文件:tar zxvf /root/mysql-boost-5.7.18.tar.gzcd /root/mysql-5.7.18
3.2配置、编译及安装
首先进行参数配置,命令如下:cmake \-DWITH_BOOST=boost \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.18 \-DMYSQL_DATADIR=/usr/local/mysql-5.7.18/data \-DWITH_MYISAM_STORAGE_ENGINE=1 \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_FEDERATED_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITH_EXAMPLE_STORAGE_ENGINE=1 \-DWITH_PARTITION_STORAGE_ENGINE=1 \-DWITH_MEMORY_STORAGE_ENGINE=1 \-DWITH_READLINE=1 \-DENABLED_LOCAL_INFILE=1 \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \-DMYSQL_USER=mysql \-DWITH_ZLIB=bundled \-DWITH_READLINE=1 \-DWITH_FAST_MUTEXES=1 \-DWITH_EMBEDDED_SERVER=1 \-DWITH_DEBUG=0 \-DWITH_SSL=system
这里要说明几点:
(1)当命令或参数过长时,为了便于直观显示,可以在其后加上”\”,当然”\”与正常命令之间需要有空格;(2) -DWITH_BOOST=boost 指定boost类库的位置;(3) -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.18 指定安装位置;在上述配置过程中可能会有出错信息,根据出错信息调整响应参数或者安装缺失的类库,直到提示可以安装为止。然后执行make进行编译,编译过程中没有错误的再执行make install安装,由于源代码体积有点大,编译耗费的时间比较长,在本人2G内存、4核的虚拟机上编译时间超过半小时以上。安装成功之后,在/usr/local/mysql-5.7.18下就是安装后的文件目录。4.数据库初始化4.1创建my.cnf文件my.cnf文件是MySQL的配置文件,在MySQL启动过程中会读取里面的配置信息。在本篇中不满足于单实例的配置,本篇将利用3306和3307两个端口创建两个实例。首先创建文件夹:mkdir –p /usr/local/mysql-5.7.18/data/3306/datamkdir –p /usr/local/mysql-5.7.18/data/3307/data
/usr/local/mysql-5.7.18/data/3306文件夹用于存放监听3306端口实例的相关配置信息、执行脚本和数据,/usr/local/mysql-5.7.18/data/3307文件夹用于存放监听3307端口实例的相关配置信息、执行脚本和数据。
因为mysql数据库将以nginx这个用户来运行,因此首先需要创建这个账户:useradd mysql -s /sbin/nologin –M分别在/usr/local/mysql-5.7.18/data/3306和/usr/local/mysql-5.7.18/data/3307创建my.cnf文件,可以使用vim创建并粘贴内容。/usr/local/mysql-5.7.18/data/3306/my.cnf的内容:
[client]port = 3306socket = /usr/local/mysql-5.7.18/data/3306/mysql.sock[mysqld_safe]log-error = /usr/local/mysql-5.7.18/data/3306/mysql.errpid-file = /usr/local/mysql-5.7.18/data/3306/mysql.pid[mysqld]## * Basic Settings#server-id = 1log-bin= /usr/local/mysql-5.7.18/data/3306/mysql-binuser = mysqlpid-file = /usr/local/mysql-5.7.18/data/3306/mysql.pidsocket = /usr/local/mysql-5.7.18/data/3306/mysql.sockport = 3306basedir = /usr/local/mysql-5.7.18datadir = /usr/local/mysql-5.7.18/data/3306/datatmpdir = /tmpopen_files_limit=1024external-locking = falsecharacter-set-server=utf8default-storage-engine=MyISAMbind-address = 0.0.0.0max_allowed_packet = 8Mthread_stack = 192Kthread_cache_size = 8max_connections = 800max_connect_errors = 300#table_cache = 64#thread_concurrency = 10query_cache_limit = 1Mquery_cache_size = 2Mjoin_buffer_size=1Msort_buffer_size=1Mlong_query_time = 1relay-log = /usr/local/mysql-5.7.18/data/3306/relay-binrelay-log-info-file =/usr/local/mysql-5.7.18/data/3306/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mkey_buffer_size=16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors =1032,1062replicate-ignore-db = mysql#innodb_additional_mem_pool_size = 4Minnodb_buffer_pool_size = 32Minnodb_data_file_path = ibdata1:12M:autoextend#innodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 4M#innodb_log_files_in_groups = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0[mysqldump]quickquote-namesmax_allowed_packet = 16M
/usr/local/mysql-5.7.18/data/3307/my.cnf的内容:
[client]port = 3307socket = /usr/local/mysql-5.7.18/data/3307/mysql.sock[mysqld_safe]log-error = /usr/local/mysql-5.7.18/data/3307/mysql.errpid-file = /usr/local/mysql-5.7.18/data/3307/mysql.pid[mysqld]## * Basic Settings#server-id = 2user = mysqlpid-file = /usr/local/mysql-5.7.18/data/3307/mysql.pidsocket = /usr/local/mysql-5.7.18/data/3307/mysql.sockport = 3307basedir = /usr/local/mysql-5.7.18datadir = /usr/local/mysql-5.7.18/data/3307/datatmpdir = /tmpopen_files_limit=1024external-locking = falsecharacter-set-server=utf8default-storage-engine=MyISAMbind-address = 0.0.0.0max_allowed_packet = 8Mthread_stack = 192Kthread_cache_size = 8max_connections = 800max_connect_errors = 300#table_cache = 64#thread_concurrency = 10query_cache_limit = 1Mquery_cache_size = 2Mjoin_buffer_size=1Msort_buffer_size=1Mlong_query_time = 1relay-log = /usr/local/mysql-5.7.18/data/3307/relay-binrelay-log-info-file =/usr/local/mysql-5.7.18/data/3307/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mkey_buffer_size=16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors =1032,1062replicate-ignore-db = mysql#innodb_additional_mem_pool_size = 4Minnodb_buffer_pool_size = 32Minnodb_data_file_path = ibdata1:12M:autoextend#innodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 4M#innodb_log_files_in_groups = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0[mysqldump]quickquote-namesmax_allowed_packet = 16M
注:在3306及3307配置中都提到了mysql.err这个文件,这个文件是用来记录MySQL启动过程的错误信息的,不过悲催的是如果这个文件不存在,那么是会启动出错的,可又没有地方可以查看错误,因此需要提前创建好文件并设置权限。
touch /usr/local/mysql-5.7.18/data/3306/mysql.errtouch /usr/local/mysql-5.7.18/data/3307/mysql.errchmod 766 /usr/local/mysql-5.7.18/data/3306/mysql.errchmod 766 /usr/local/mysql-5.7.18/data/3307/mysql.err
注:如果嫌每次输入完整的文件路径麻烦可以切换到程序的安装目录,甚至还可以创建软链接,所谓的软链接有点类似于Windows下的快捷方式,如ln –s /usr/local/mysql-5.7.18 /usr/local/mysql就是创建了mysql-5.7.18的软链接,甚至如果你喜欢,还可以在用户根目录下创建软链接,如:ln –s /usr/local/mysql-5.7.18 /root /mysql。
通过命令查看,如下图所示是在SSH客户端中的效果: 上面mysql,nginx及tomcat都是创建的软链接(文件属性的第一个字符是”l”表明是软链接),其对应的真实路径分别为当前路径下的mysql-5.7.18、nginx-1.12.0及apache-tomcat-8.5.15目录。4.2创建MySQL的启动文件分别在/usr/local/mysql-5.7.18/data/3306和/usr/local/mysql-5.7.18/data/3307创建mysql文件。可用vim来创建文件,这个文件是用来启动MySQL实例的,所以在创建完成不要忘记chmod 755设置。/usr/local/mysql-5.7.18/data/3306/mysql#!/bin/shport=3306mysql_user="root"mysql_pwd="mypassword"cmd_path="/usr/local/mysql-5.7.18/bin"mysql_sock="/usr/local/mysql-5.7.18/data/${port}/mysql.sock"#startup functionfunction_start_mysql(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${cmd_path}/mysqld_safe --defaults-file=/usr/local/mysql-5.7.18/data/${port}/my.cnf 2>&1 > /dev/null & else printf "MySQL is running...\n" exit fi}#stop functionfunction_stop_mysql(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /usr/local/mysql-5.7.18/data/${port}/mysql.sock shutdown fi}#restart functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql}case $1 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /usr/local/mysql-5.7.18/data/${port}/mysql {start|stop|restart}\n"esac
/usr/local/mysql-5.7.18/data/3307/mysql文件内容:
#!/bin/shport=3307mysql_user="root"mysql_pwd="mypassword"cmd_path="/usr/local/mysql-5.7.18/bin"mysql_sock="/usr/local/mysql-5.7.18/data/${port}/mysql.sock"#startup functionfunction_start_mysql(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${cmd_path}/mysqld_safe --defaults-file=/usr/local/mysql-5.7.18/data/${port}/my.cnf 2>&1 > /dev/null & else printf "MySQL is running...\n" exit fi}#stop functionfunction_stop_mysql(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /usr/local/mysql-5.7.18/data/${port}/mysql.sock shutdown fi}#restart functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql}case $1 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /usr/local/mysql-5.7.18/data/${port}/mysql {start|stop|restart}\n"esac
注意:
上述启动文件中关闭实例没有采用kill进程的办法,而是使用mysqladmin shutdown的方法,这个方法需要root级别用户的账号和密码,因此需要控制这个文件的查看和编辑权限。 需要给两个文件设置执行权限。chmod 755 /usr/local/mysql-5.7.18/data/3306/mysqlchmod 755 /usr/local/mysql-5.7.18/data/3307/mysql
此外,因为MySQL的两个实例均以MySQL用户运行,为保证有足够的权限,设置/usr/local/mysql-5.7.18/文件夹的用户和用户组属性:
chown –R mysql:mysql /usr/local/mysql-5.7.184.3执行初始化网上有很多例子讲解MySQL初始化都是利用MySQL安装目录下的bin目录中的mysql_install_db来执行,但是这个官方不建议的,官方建议使用mysqld来初始化。下面的脚本可以执行但不建议使用(且本人发现执行后不知道root用户默认密码):
/usr/local/mysql-5.7.18/bin/mysql_install_db \--basedir=/usr/local/mysql-5.7.18 \--datadir=/usr/local/mysql-5.7.18/data/3306/data \--user=mysql/usr/local/mysql-5.7.18/bin/mysql_install_db \--basedir=/usr/local/mysql-5.7.18 \--datadir=/usr/local/mysql-5.7.18/data/3307/data \--user=mysql
下面的脚本是官方建议的:
/usr/local/mysql-5.7.18/bin/mysqld --initialize \--basedir=/usr/local/mysql-5.7.18 \--datadir=/usr/local/mysql-5.7.18/data/3306/data \--user=mysql/usr/local/mysql-5.7.18/bin/mysqld --initialize \--basedir=/usr/local/mysql-5.7.18 \--datadir=/usr/local/mysql-5.7.18/data/3307/data \--user=mysql
上面的脚本如果执行成功,最后一句里有root用户的初始密码,在本人执行上述两条命令时得到结果如下:
其中关键的两句(每个实例初始化都有类似一句):2017-06-09T13:45:09.590690Z 1 [Note] A temporary password is generated for root@localhost: GdlR7&+w2017-06-09T13:46:48.721250Z 1 [Note] A temporary password is generated for root@localhost: Z>B#5IfIFAfj
上述密码是随机的,请记住一会登录系统后更改临时密码。
4.4启动MySQL实例和登录4.4.1启动实例启动3306端口实例:/usr/local/mysql-5.7.18/data/3306/mysql start启动3307端口实例;
/usr/local/mysql-5.7.18/data/3307/mysql start这是可以查看实例是否启动成功,如果启动成功相应的端口就会处于监听状态。
[root@localhost ~]# /usr/local/mysql-5.7.18/data/3306/mysql startStarting MySQL...[root@localhost ~]# /usr/local/mysql-5.7.18/data/3307/mysql startStarting MySQL...[root@localhost ~]# netstat -lntp | grep 330tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 4480/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3817/mysqld
从执行结果来看,3306和3307两个端口对应的MySQL实例均已启动。如果启动不成功,可以通过相应的错误日志来排除错误,如下就是用tail来查看错误日志:
[root@localhost ~]# tail /usr/local/mysql-5.7.18/data/3306/mysql.err2017-06-10T14:41:51.366238Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.2017-06-10T14:41:51.366625Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.2017-06-10T14:41:51.371787Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.2017-06-10T14:41:51.416687Z 0 [Note] Event Scheduler: Loaded 0 events2017-06-10T14:41:51.416855Z 0 [Note] /usr/local/mysql-5.7.18/bin/mysqld: ready for connections.Version: '5.7.18-log' socket: '/usr/local/mysql-5.7.18/data/3306/mysql.sock' port: 3306 Source distribution2017-06-10T14:41:51.416862Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 2017-06-10T14:41:51.416864Z 0 [Note] Beginning of list of non-natively partitioned tables2017-06-10T14:41:51.473660Z 0 [Note] End of list of non-natively partitioned tables2017-06-10T14:42:07.539386Z 3 [Note] Start binlog_dump to master_thread_id(3) slave_server(2), pos(mysql-bin.000001, 2654)
4.4.2登录实例
如果是单实例可以通过mysql –u user –p来登录,但这里有了两个实例,所以登录方式有些区别。登录到3306端口对应的实例:cd /usr/local/mysql-5.7.18./bin/mysql -u root -p -S ./data/3306/mysql.sock
登录到3307端口对应的实例:
cd /usr/local/mysql-5.7.18./bin/mysql -u root -p -S ./data/3307/mysql.sock
以下是登录3306实例的过程:
[root@localhost ~]# cd /usr/local/mysql-5.7.18[root@localhost mysql-5.7.18]# ./bin/mysql -u root -p -S ./data/3306/mysql.sockEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.18
在此过程中需要输入root的密码,root的密码在初始化阶段时系统随机生成了,可见“执行初始化”一节中描述:
2017-06-09T13:45:09.590690Z 1 [Note] A temporary password is generated for root@localhost: GdlR7&+w2017-06-09T13:46:48.721250Z 1 [Note] A temporary password is generated for root@localhost: Z>B#5IfIFAfj
其中Gdl<nv>R7&+w和Z>B#5IfIFAfj分别是3306和3307实例对应的随机密码,初次登录过程中需要输入。
登录成功后,如果再执行其它语句时会得到如下提示:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.因为我们没有改系统为root账户生成的随机密码,在此之前是不能进行任何操作的,可以通过如下命令更改:
mysql> SET PASSWORD = PASSWORD('your_password'); Query OK, 0 rows affected (0.03 sec)
在实际执行过程中请将your_password更改为自己的密码。改完之后记得改3306和3307两个启动文件中的用户密码哦,否则stop命令不会生效的。
至此,MySQL数据库已完成初始化和初步配置。在实际过程中由于root用户的权限过大、且默认情况下只能在服务器上登录,为了安全一般会创建其它较小权限的用户,在这里为了演示方便省却了这个操作。5.设置MySQL主从同步主从同步,也称master-slave,是开发过程中常见的提高程序性能的办法。通常情况下在主数据库服务器写入数据,查询数据的时候是在从服务器,主从服务器之间通过同步机制来保持一致,虽然主从数据库的一致性是存在一定时间差的,但因为这个时间差很小所以在一些对时间不是非常敏感的业务场景下被认为是实时一致的。在大多数主流数据库中都是可以通过配置来实现主从同步、读写分离的。这里就以刚刚配置起来的两个数据实例来演示如何实现主从同步的,步骤有如下:(1)主数据库实例设置server-id和开启bin-log;(2)主数据库实例创建用于同步的账号;(3)从数据库实例设置server-id;(4)从数据库实例配置同步参数;(5)从数据库实例启动同步开关。5.1主数据库实例设置server-id和开启bin-log其实在上面的3306的配置文件里已经配置了server-id和bin-log,就在/usr/local/mysql-5.7.18/data/3306/my.cnf文件的[mysqld]节点处:[mysqld]## * Basic Settings#server-id = 1log-bin= /usr/local/mysql-5.7.18/data/3306/mysql-bin
另外还需要注意的是在这个my.cnf配置文件中还有一句:
replicate-ignore-db = mysql它的意思是在进行主从同步时忽略mysql这个库,因为mysql库主要存放账号及授权信息的,不同数据库实例账号和授权信息不同的可能性极大,因此没有必要同步这个库。通过如下命令可以快速查看是否已经正确配置:
[root@localhost ~]# egrep "server-id|log-bin" /usr/local/mysql-5.7.18/data/3306/my.cnfserver-id = 1log-bin = /usr/local/mysql-5.7.18/data/3306/mysql-bin
在数据库中也可以查看:
mysql> show variables like "server_id";+---------------+-------+| Variable_name | Value |+---------------+-------+| server_id | 1 |+---------------+-------+1 row in set (0.00 sec)mysql> show variables like "log_bin";+---------------+-------+| Variable_name | Value |+---------------+-------+| log_bin | ON |+---------------+-------+1 row in set (0.00 sec)
同时,这里也查看一下主数据库的状态:
mysql> show master status \G;*************************** 1. row *************************** File: mysql-bin.000003 Position: 154 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)ERROR: No query specified
这里:File和Position的值是两个很重要的参数,这是从数据库下次同步时的起始位置。
5.2主数据库实例创建用于同步的账号在主数据库创建一个replication账号用于从主库同步数据到从库,创建replication账号的语句如下:grant replication slave on *.* to 'replication'@'%' identified by 'your_password';flush privileges;在实际执行时请将your_password改为自己的密码,执行这个SQL语句后就会在mysql库中的user表中创建一个名为replication的用户,可以通过SQL语句查看:mysql> show grants for "replication"@"%";+-----------------------------------------------------+| Grants for replication@% |+-----------------------------------------------------+| GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' |+-----------------------------------------------------+1 row in set (0.00 sec)
5.3从数据库实例设置server-id
同样,其实在上面的3307的配置文件里已经配置了server-id和bin-log,就在/usr/local/mysql-5.7.18/data/3307/my.cnf文件的[mysqld]节点处:[mysqld]## * Basic Settings#server-id = 2
注意:在一个主从关系群中server-id是唯一的,另外在由于从数据库不向其它库同步数据,因此没有开启bin-log。这里也通过egrep查看一下:
[root@localhost ~]# egrep "server-id|log-bin" /usr/local/mysql-5.7.18/data/3307/my.cnfserver-id = 1
5.4从数据库实例配置同步参数
登录3307端口对应的实例:cd /usr/local/mysql-5.7.18./bin/mysql -S ./data/3307/mysql.sock
然后执行以下语句:
CHANGE MASTER TO \MASTER_HOST='127.0.0.1', \MASTER_PORT=3306, \MASTER_USER='replication', \MASTER_PASSWORD='your_password', \MASTER_LOG_FILE='mysql-bin.000003', \MASTER_LOG_POS=154;
说明:
MASTER_HOST为主服务器IP或主机名;MASTER_PORT为主服务器端口;MASTER_USER为主服务器上用于同步的数据库账户名;MASTER_PASSWORD为主服务器上用于同步的数据库账户对应的密码;MASTER_LOG_FILE为当前bin-log日志文件名;MASTER_LOG_POS为当前偏移量;其中MASTER_LOG_FILE和MASTER_LOG_POS可以在主数据库上执行” show master status \G;”SQL语句来获得,见本文“主数据库实例设置server-id和开启bin-log”一节。5.5从数据库实例启动同步开关登录从数据库执行”start slave”即可。在本篇中由于连个数据库都是刚刚初始化的,所以数据都是一致的。在实际情况中,需要先将两个数据库实例中的除mysql库之外的数据一致后才可启动同步,否则两个库中的就会不一致。这里我们来检查一下效果:在主库中创建一个数据库,如下:mysql> create database zhoufoxcn;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || zhoufoxcn |+--------------------+5 rows in set (0.01 sec)
然后在从库中查看数据库的情况:
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || zhoufoxcn |+--------------------+5 rows in set (0.00 sec)
可见主库的数据被自动同步到从库了,这时再在主库中删除zhoufoxcn这个库,可以看到从库中也自动删掉了。
这时在主库查看状态:mysql> show master status \G;*************************** 1. row *************************** File: mysql-bin.000003 Position: 488 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)ERROR: No query specified
在从库查看状态:
mysql> show slave status \G;*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 488 Relay_Log_File: relay-bin.000008 Relay_Log_Pos: 701 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 488 Relay_Log_Space: 1115 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: da584607-4d19-11e7-8903-080027f376f5 Master_Info_File: /usr/local/mysql-5.7.18/data/3307/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)ERROR: No query specified
可以看出通过配置,两台数据库能够自动同步数据了。
6.总结本篇主要讲述了如何从MySQL官方网站下载源代码通过编译和安装,然后配置成多个实例运行,在最后还讲解了如何配置MySQL数据库主从同步。在实际情况中,可能有人在用MariaDB了,这是原MySQL开发人员在Oracle收购了Sun之后(Sun收购了MySQL),担心Oracle不再继续开源MySQL而开发的一套开源数据系统,它们有很多相似性,在本篇没有涉及。另外,本篇没有涉及的还有通过mysqld_multi.server来设置将MySQL随系统启动。声明:本文首发于本人个人微信订阅号:zhoujinqiaoIT,其后会同时在本人的CSDN、51CTO及oschina三处博客发布,本人会负责在此四处答疑。
转载地址:http://zlsix.baihongyu.com/