<!DOCTYPE html><html><head><title>Mysql主从配置</title><meta charset='utf-8'><link href='https://dn-maxiang.qbox.me/res-min/themes/marxico.css' rel='stylesheet'></head><body><div id='preview-contents' class='note-content'>
<div id="wmd-preview" class="preview-content"></div>
<div id="wmd-preview-section-13" class="wmd-preview-section preview-content">
</div><div id="wmd-preview-section-14" class="wmd-preview-section preview-content">
<h1 id="mysql主从配置">Mysql主从配置</h1>
<p></p>
</div><div id="wmd-preview-section-15" class="wmd-preview-section preview-content">
<h3 id="安装主mysql">安装主Mysql</h3>
</div><div id="wmd-preview-section-16" class="wmd-preview-section preview-content">
<h4 id="安装前的准备">安装前的准备</h4>
<p>检查系统是否存在mysql用户</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@test</span> mysql]<span class="hljs-comment"># less /etc/passwd | grep mysql</span><br><span class="hljs-symbol">mysql:</span><span class="hljs-symbol">x:</span><span class="hljs-number">501</span><span class="hljs-symbol">:</span><span class="hljs-number">501</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/home/mysql</span><span class="hljs-symbol">:/sbin/nologin</span> <br><br></code></pre>
<p>若没有则创建</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">[root@<span class="hljs-built_in">test</span> mysql]<span class="hljs-comment"># useradd -M -s /sbin/nologin mysql</span><br><br></code></pre>
<p>检查系统是否安装RPM格式的mysql,已安装则删除</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">[root@<span class="hljs-built_in">test</span> LAMP]<span class="hljs-comment"># rpm -qa | grep mysql</span><br>mysql-libs-<span class="hljs-number">5.1</span>.<span class="hljs-number">73</span>-<span class="hljs-number">5</span>.el6_6.x86_64<br>mysql-devel-<span class="hljs-number">5.1</span>.<span class="hljs-number">73</span>-<span class="hljs-number">5</span>.el6_6.x86_64<br>mysql-<span class="hljs-number">5.1</span>.<span class="hljs-number">73</span>-<span class="hljs-number">5</span>.el6_6.x86_64<br>[root@<span class="hljs-built_in">test</span> LAMP]<span class="hljs-comment"># yum remove mysql</span><br><br></code></pre>
<p>新建数据库数据存放目录,即datadir,并更改属主、属组为mysql</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> LAMP]# chown <span class="hljs-string">mysql:</span>mysql <span class="hljs-regexp">/data/</span>mysql/<br>[root<span class="hljs-annotation">@test</span> LAMP]# ll <span class="hljs-regexp">/data/</span>mysql/ -d<br>drwxr-xr-x <span class="hljs-number">2</span> mysql mysql <span class="hljs-number">4096</span> <span class="hljs-number">11</span>月 <span class="hljs-number">29</span> <span class="hljs-number">01</span>:<span class="hljs-number">21</span> <span class="hljs-regexp">/data/</span>mysql/<br><br></code></pre>
</div><div id="wmd-preview-section-17" class="wmd-preview-section preview-content">
<h4 id="安装配置第一台mysql">安装配置第一台Mysql</h4>
<p>解压Mysql、移动并重命名</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">[root@<span class="hljs-built_in">test</span> LAMP]<span class="hljs-comment"># tar zxvf mysql-5.1.72-linux-x86_64-glibc23.tar.gz</span><br>[root@<span class="hljs-built_in">test</span> LAMP]<span class="hljs-comment"># mv mysql-5.1.72-linux-x86_64-glibc23 /usr/local/mysql</span><br><br></code></pre>
<p>进入mysql目录</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> LAMP]# cd <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/mysql/</span><br><br></code></pre>
<p>拷备配置文件</p>
<pre class="prettyprint hljs-dark"><code class="hljs stylus">cp support-files/my-large<span class="hljs-class">.cnf</span> /etc/my<span class="hljs-class">.cnf</span> <br><br></code></pre>
<p>初始化数据库,�Cuser=定义数据库属主,�Cdatadir=定义数据库数据存放目录</p>
<pre class="prettyprint hljs-dark"><code class="hljs brainfuck"><span class="hljs-string">.</span><span class="hljs-comment">/scripts/mysql_install_db</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">user=mysql</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">datadir=/data/mysql/</span><br><br></code></pre>
<p>拷备启动脚本并修改权限</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> mysql]# cp support-files<span class="hljs-regexp">/mysql.server /</span>etc<span class="hljs-regexp">/init.d/</span>mysqld<br>[root<span class="hljs-annotation">@test</span> mysql]# chmod <span class="hljs-number">755</span> <span class="hljs-regexp">/etc/</span>init.d/mysqld<br><br></code></pre>
<p>修改启动脚本,指定数据目录datadir和程序目录basedir</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> mysql]# vim <span class="hljs-regexp">/etc/</span>init.d/mysqld <br><br>basedir=<span class="hljs-regexp">/usr/</span>local/mysql<br>datadir=<span class="hljs-regexp">/data/</span>mysql<br><br></code></pre>
<p>加入系统启动服务项,设定开机启动</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">[root@<span class="hljs-built_in">test</span> mysql]<span class="hljs-comment"># chkconfig --add mysqld</span><br>[root@<span class="hljs-built_in">test</span> mysql]<span class="hljs-comment"># chkconfig mysqld on</span><br><br></code></pre>
<p>启动Mysql,并查看监听端口</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@test</span> mysql]<span class="hljs-comment"># service mysqld start</span><br><span class="hljs-constant">Starting MySQL.</span> <span class="hljs-constant">SUCCESS!</span> <br>[root<span class="hljs-variable">@test</span> mysql]<span class="hljs-comment"># netstat -nlp | grep mysql</span><br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">3306</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:*</span> <span class="hljs-constant">LISTEN </span> <span class="hljs-number">2109</span>/mysqld <br>unix <span class="hljs-number">2</span> [ <span class="hljs-constant">ACC </span>] <span class="hljs-constant">STREAM </span> <span class="hljs-constant">LISTENING </span> <span class="hljs-number">17799</span> <span class="hljs-number">2109</span>/mysqld /tmp/mysql.sock<br><br></code></pre>
</div><div id="wmd-preview-section-18" class="wmd-preview-section preview-content">
<h3 id="安装第二台mysql-slave">安装第二台Mysql-slave</h3>
<p>拷备第一台Mysql程序目录并重命为mysql-slave,做为第二台mysql的程序目录</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">[root@<span class="hljs-built_in">test</span> <span class="hljs-built_in">local</span>]<span class="hljs-comment"># cp -r mysql mysql-slave</span><br><br></code></pre>
<p>拷备第一台mysql配置文件到mysql-slave下,做为第二台mysql的配置文件</p>
<pre class="prettyprint hljs-dark"><code class="hljs vim">[root@test mysql2]# <span class="hljs-keyword">cp</span> /etc/my.<span class="hljs-keyword">cnf</span> ./my.<span class="hljs-keyword">cnf</span><br><br></code></pre>
<p>编辑mysql-slave的配置文件,更改从mysql的port、socket,避免与第一台mysql发生冲突,添加datadir</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@test</span> mysql2]<span class="hljs-comment"># vim my.cnf </span><br><br> [mysqld]<br> port = <span class="hljs-number">3307</span><br> socket = <span class="hljs-regexp">/tmp/mysql</span>-slave.sock<br> datadir = <span class="hljs-regexp">/data/mysql</span>-slave<br><br></code></pre>
<p>初始化mysql2</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> mysql2]# .<span class="hljs-regexp">/scripts/</span>mysql_install_db --user=mysql --datadir=<span class="hljs-regexp">/data/</span>mysql-slave<br><br></code></pre>
</div><div id="wmd-preview-section-19" class="wmd-preview-section preview-content">
<h4 id="使用启动脚本启动mysql2">使用启动脚本启动Mysql2</h4>
<p>拷备启动脚本</p>
<pre class="prettyprint hljs-dark"><code class="hljs stata">[root@<span class="hljs-keyword">test</span> mysql2]# <span class="hljs-keyword">cd</span> /etc/init.<span class="hljs-keyword">d</span>/<br>[root@<span class="hljs-keyword">test</span> init.<span class="hljs-keyword">d</span>]# cp mysqld mysqld-slave<br><br></code></pre>
<p>编辑mysql-slave启动脚本,指定mysql-slave的basedir和datadir,并更改conf=$basedir/my.cnf,指定mysql-slave的配置文件所在位置</p>
<pre class="prettyprint hljs-dark"><code class="hljs stata">[root@<span class="hljs-keyword">test</span> init.<span class="hljs-keyword">d</span>]# vim mysqld-slave<br><br> basedir=/usr/<span class="hljs-keyword">local</span>/mysql-slave<br> datadir=/data/mysql-slave<br> <span class="hljs-keyword">conf</span>=<span class="hljs-label">$basedir</span>/my.cnf<br><br></code></pre>
<p>使用启动脚本启动mysql-slave</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@test</span> init.d]<span class="hljs-comment"># /etc/init.d/mysqld-slave start</span><br><span class="hljs-constant">Starting MySQL.</span> <span class="hljs-constant">SUCCESS!</span> <br>[root<span class="hljs-variable">@test</span> init.d]<span class="hljs-comment"># netstat -nlp | grep mysql</span><br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">3306</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:*</span> <span class="hljs-constant">LISTEN </span> <span class="hljs-number">3077</span>/mysqld <br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">3307</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:*</span> <span class="hljs-constant">LISTEN </span> <span class="hljs-number">3330</span>/mysqld <br>unix <span class="hljs-number">2</span> [ <span class="hljs-constant">ACC </span>] <span class="hljs-constant">STREAM </span> <span class="hljs-constant">LISTENING </span> <span class="hljs-number">25204</span> <span class="hljs-number">3077</span>/mysqld /tmp/mysql.sock<br>unix <span class="hljs-number">2</span> [ <span class="hljs-constant">ACC </span>] <span class="hljs-constant">STREAM </span> <span class="hljs-constant">LISTENING </span> <span class="hljs-number">30937</span> <span class="hljs-number">3330</span>/mysqld /tmp/mysql-slave.sock<br><br></code></pre>
</div><div id="wmd-preview-section-20" class="wmd-preview-section preview-content">
<h4 id="使用启动命令起动mysql-slave">使用启动命令起动Mysql-slave</h4>
<p>使用脚本停止mysql2</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@test</span> init.d]<span class="hljs-comment"># /etc/init.d/mysqld-slave stop</span><br><span class="hljs-constant">Shutting </span>down <span class="hljs-constant">MySQL.</span>.... <span class="hljs-constant">SUCCESS!</span> <br>[root<span class="hljs-variable">@test</span> init.d]<span class="hljs-comment"># netstat -nlp | grep mysql</span><br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">3306</span> <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span><span class="hljs-symbol">:*</span> <span class="hljs-constant">LISTEN </span> <span class="hljs-number">3077</span>/mysqld <br>unix <span class="hljs-number">2</span> [ <span class="hljs-constant">ACC </span>] <span class="hljs-constant">STREAM </span> <span class="hljs-constant">LISTENING </span> <span class="hljs-number">25204</span> <span class="hljs-number">3077</span>/mysqld /tmp/mysql-slave.sock<br><br></code></pre>
<p>使用启动命令启动Mysql-slave</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@test</span> init.d]# <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/mysql-slave/</span>bin<span class="hljs-regexp">/mysqld_safe --defaults-file=/</span>usr<span class="hljs-regexp">/local/</span>mysql-slave/my.cnf --user=mysql &<br>[<span class="hljs-number">1</span>] <span class="hljs-number">3373</span><br>[root<span class="hljs-annotation">@test</span> init.d]# <span class="hljs-number">151129</span> <span class="hljs-number">02</span>:<span class="hljs-number">53</span>:<span class="hljs-number">58</span> mysqld_safe Logging to <span class="hljs-string">'/data/mysql-slave/test.err'</span>.<br><span class="hljs-number">151129</span> <span class="hljs-number">02</span>:<span class="hljs-number">53</span>:<span class="hljs-number">58</span> mysqld_safe Starting mysqld daemon with databases from <span class="hljs-regexp">/data/</span>mysql-slave<br>[root<span class="hljs-annotation">@test</span> init.d]# !ne<br>netstat -nlp | grep mysql<br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">3306</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:* LISTEN <span class="hljs-number">3077</span>/mysqld <br>tcp <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">3307</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:* LISTEN <span class="hljs-number">3494</span>/mysqld <br>unix <span class="hljs-number">2</span> [ ACC ] STREAM LISTENING <span class="hljs-number">25204</span> <span class="hljs-number">3077</span><span class="hljs-regexp">/mysqld /</span>tmp/mysql.sock<br>unix <span class="hljs-number">2</span> [ ACC ] STREAM LISTENING <span class="hljs-number">31582</span> <span class="hljs-number">3494</span><span class="hljs-regexp">/mysqld /</span>tmp/mysql-slave.sock<br><br></code></pre>
</div><div id="wmd-preview-section-21" class="wmd-preview-section preview-content">
<h4 id="配置mysql主从的准备工作">配置Mysql主从的准备工作</h4>
<p>主mysql,端口:3306,socket:/tmp/mysql.sock <br>
从mysql,端口:3307,socket:/tmp/mysql-slave.sock</p>
<p>在主上创建测试库:db1</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# mysql -u root -S <span class="hljs-regexp">/tmp/</span>mysql.sock -e <span class="hljs-string">"create database db1"</span>;<br><br></code></pre>
<p>导出主的mysql库数据,导入到db1库</p>
<pre class="prettyprint hljs-dark"><code class="hljs coffeescript">[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># mysqldump -u root -S /tmp/mysql.sock mysql > 123.sql</span><br>-- <span class="hljs-attribute">Warning</span>: Skipping the data <span class="hljs-keyword">of</span> table mysql.event. Specify the --events option explicitly.<br>[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># mysql -u root -S /tmp/mysql.sock db1 < 123.sql</span><br><br></code></pre>
<p>查看db1库</p>
<pre class="prettyprint hljs-dark"><code class="hljs smalltalk">[root@stady ~]# mysql -u root -<span class="hljs-class">S</span> /tmp/mysql.sock <br><br>mysql> use db1<br><span class="hljs-class">Database</span> changed<br>mysql> show tables;<br><br>+---------------------------+-<br>| <span class="hljs-class">Tables_in_db1</span> |<br>+---------------------------+-<br><span class="hljs-localvars">| columns_priv |</span><br><span class="hljs-localvars">| db |</span><br><span class="hljs-localvars">| event |</span><br><span class="hljs-localvars">| func |</span><br><span class="hljs-localvars">| general_log |</span><br><span class="hljs-localvars">| help_category |</span><br><span class="hljs-localvars">| help_keyword |</span><br><span class="hljs-localvars">| help_relation |</span><br><span class="hljs-localvars">| help_topic |</span><br><span class="hljs-localvars">| host |</span><br><span class="hljs-localvars">| ndb_binlog_index |</span><br><span class="hljs-localvars">| plugin |</span><br><span class="hljs-localvars">| proc |</span><br><span class="hljs-localvars">| procs_priv |</span><br><span class="hljs-localvars">| servers |</span><br><span class="hljs-localvars">| slow_log |</span><br><span class="hljs-localvars">| tables_priv |</span><br><span class="hljs-localvars">| time_zone |</span><br><span class="hljs-localvars">| time_zone_leap_second |</span><br><span class="hljs-localvars">| time_zone_name |</span><br><span class="hljs-localvars">| time_zone_transition |</span><br><span class="hljs-localvars">| time_zone_transition_type |</span><br><span class="hljs-localvars">| user |</span><br>+---------------------------+<br><span class="hljs-number">23</span> rows in set (<span class="hljs-number">0.00</span> sec)<br><br>mysql> exit<br><br></code></pre>
</div><div id="wmd-preview-section-22" class="wmd-preview-section preview-content">
<h4 id="配置主mysqlmaster">配置主mysql(master)</h4>
<p>编辑主mysql的配置文件</p>
<pre class="prettyprint hljs-dark"><code class="hljs coffeescript">[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># vim /etc/my.cnf</span><br><br></code></pre>
<p>更改或添加</p>
</div><div id="wmd-preview-section-23" class="wmd-preview-section preview-content">
<pre class="prettyprint hljs-dark"><code class="hljs applescript">server-<span class="hljs-property">id</span> = <span class="hljs-number">1</span> <span class="hljs-comment">#设置主mysql的server-id,可为任意但必须唯一</span><br><span class="hljs-command">log</span>-bin=mysql-master <span class="hljs-comment">#开启主mysql的二进制binlog日志</span><br><span class="hljs-comment">#binlog-do-db=db1,db2,...dbn #可选项,用来指定需要同步的库</span><br><span class="hljs-comment">#binlog-ignore-db=db1,db2...dbn #可选项,用来指定同步需要忽略的库</span><br></code></pre>
<p>重启主mysql</p>
<pre class="prettyprint hljs-dark"><code class="hljs coffeescript">[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># service mysqld restart</span><br>Shutting down MySQL. SUCCESS! <br>Starting MySQL. SUCCESS! <br><br></code></pre>
<p>设置主Mysql的root密码</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# mysqladmin -u root -S <span class="hljs-regexp">/tmp/</span>mysql.sock password <span class="hljs-string">'qwe123'</span><br><br></code></pre>
<p>密码登陆主mysql</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# mysql -u root -S <span class="hljs-regexp">/tmp/</span>mysql.sock -p<br>Enter <span class="hljs-string">password:</span> <br><br></code></pre>
<blockquote>
<p><strong><em>授权一个用来同步数据的用户repl</em></strong> <br>
<strong><em>mysql> grant replication slave on </em>.* to ‘repl’@’127.0.0.1’ identified by ‘123123’;*</strong> <br>
Query OK, 0 rows affected (0.00 sec)</p>
<p><strong><em>锁死表的读</em></strong> <br>
<strong><em>mysql> flush tables with read lock;</em></strong> <br>
Query OK, 0 rows affected (0.00 sec)</p>
<p><strong><em>查看主mysql的状态</em></strong> <br>
<strong><em>mysql> show master status;</em></strong></p>
</blockquote>
<pre class="prettyprint hljs-dark"><code class="hljs asciidoc"><span class="hljs-code">+---------------------+</span>----------<span class="hljs-code">+--------------+</span>------------------+<br><span class="hljs-header">| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |<br>+---------------------+----------+--------------+------------------+</span><br><span class="hljs-header">| mysql-master.000003 | 394 | | |<br>+---------------------+----------+--------------+-------------------</span><br>File:<br>position:<br><br></code></pre>
</div><div id="wmd-preview-section-24" class="wmd-preview-section preview-content">
<h4 id="配置从mysqlslave">配置从mysql(slave)</h4>
<p>编辑从mysql的配置文件</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# vim <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/mysql-slave/</span>my.cnf <br><br></code></pre>
<p>修改或添加</p>
<pre class="prettyprint hljs-dark"><code class="hljs nix"><span class="hljs-variable">server-id =</span> <span class="hljs-number">2</span> <span class="hljs-comment">#设置从mysql的server-id,不能与主Mysql一样</span><br><span class="hljs-variable">replicate-do-db=</span>db1,db2,...dbn <span class="hljs-comment">#可选项,用来指定需要同步的库</span><br><span class="hljs-variable">replicate-ignore-db=</span>db1,db2...dbn <span class="hljs-comment">#可选项,用来指定同步需要忽略的库</span><br><span class="hljs-comment">#若主mysql已定义,则从mysql就不用再次定义</span><br><br></code></pre>
<p>在从mysql创建db1库,拷备主mysql的db1库,导入到从mysql的db1库 <br>
确保主从mysql上的数据完全一致</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# mysql -u root -S <span class="hljs-regexp">/tmp/</span>mysql-slave.sock -e <span class="hljs-string">"create database db1"</span>;<br>[root<span class="hljs-annotation">@stady</span> ~]# mysqldump -u root -S <span class="hljs-regexp">/tmp/</span>mysql.sock -pqwe123 db1 > db1.sql<br>[root<span class="hljs-annotation">@stady</span> ~]# mysql -u root -S <span class="hljs-regexp">/tmp/</span>mysql-slave.sock db1 < db1.sql <br><br></code></pre>
<p>查看从mysql的db1库</p>
<pre class="prettyprint hljs-dark"><code class="hljs elixir">[root<span class="hljs-variable">@stady</span> ~]<span class="hljs-comment"># mysql -u root -S /tmp/mysql-slave.sock </span><br><br>mysql> <span class="hljs-keyword">use</span> db1<br><span class="hljs-constant">Database </span>changed<br>mysql> show tables;<br><br></code></pre>
<p>停上主从复制</p>
<pre class="prettyprint hljs-dark"><code class="hljs r">mysql> slave <span class="hljs-keyword">stop</span>;<br>Query OK, <span class="hljs-number">0</span> rows affected, <span class="hljs-number">1</span> <span class="hljs-keyword">warning</span> (<span class="hljs-number">0.00</span> sec)<br><br></code></pre>
<p>指定change master</p>
<pre class="prettyprint hljs-dark"><code class="hljs bash">mysql> change master to master_host=<span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-comment">#指定主mysql的ip</span><br>master_port=<span class="hljs-number">3306</span>, <span class="hljs-comment">#指定主mysql的端口</span><br>master_user=<span class="hljs-string">'repl'</span>, <span class="hljs-comment">#指定同步用户</span><br>master_password=<span class="hljs-string">'123123'</span>, <span class="hljs-comment">#同步用户密码</span><br>master_<span class="hljs-built_in">log</span>_file=<span class="hljs-string">'mysql-master.000003'</span>, <span class="hljs-comment">#指定二进制的binlog文件</span><br>master_<span class="hljs-built_in">log</span>_pos=<span class="hljs-number">394</span>; <span class="hljs-comment">#指定binlog的偏移量</span><br><br>Query OK, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0.04</span> sec)<br><br></code></pre>
<p>开启主从复制 </p>
<pre class="prettyprint hljs-dark"><code class="hljs haml"> mysql> slave start<br> -<span class="ruby">> ;<br></span> Query OK, 0 rows affected (0.03 sec)<br><br></code></pre>
<p>退出从mysql</p>
<pre class="prettyprint hljs-dark"><code class="hljs mel"> mysql> <span class="hljs-keyword">quit</span><br> Bye<br><br></code></pre>
<p>进入<strong><em>主mysql</em></strong>,解锁表</p>
<pre class="prettyprint hljs-dark"><code class="hljs groovy">[root<span class="hljs-annotation">@stady</span> ~]# mysql -u root -S <span class="hljs-regexp">/tmp/</span>mysql.sock -p qwe123 -e <span class="hljs-string">"unlock tables"</span>;<br><br></code></pre>
<p>进入<strong><em>从mysql</em></strong>,查看slave状态</p>
<pre class="prettyprint hljs-dark"><code class="hljs gherkin">mysql> show slave status\G;<br><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span> 1. row <span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><span class="hljs-keyword">*</span><br> Slave_IO_State: Waiting for master to send event<br> Master_Host: 127.0.0.1<br> Master_User: repl<br> Master_Port: 3306<br> Connect_Retry: 60<br> Master_Log_File: mysql-master.000005<br> Read_Master_Log_Pos: 106<br> Relay_Log_File: stady-relay-bin.000007<br> Relay_Log_Pos: 254<br> Relay_Master_Log_File: mysql-master.000005<br> Slave_IO_Running: Yes<br> Slave_SQL_Running: Yes<br> Replicate_Do_DB: <br> Replicate_Ignore_DB: <br> Replicate_Do_Table: <br> Replicate_Ignore_Table: <br> Replicate_Wild_Do_Table: <br> Replicate_Wild_Ignore_Table: <br> Last_Errno: 0<br> Last_Error: <br> Skip_Counter: 0<br> Exec_Master_Log_Pos: 106<br> Relay_Log_Space: 557<br> Until_Condition: None<br> Until_Log_File: <br> Until_Log_Pos: 0<br> Master_SSL_Allowed: No<br> Master_SSL_CA_File: <br> Master_SSL_CA_Path: <br> Master_SSL_Cert: <br> Master_SSL_Cipher: <br> Master_SSL_Key: <br> Seconds_Behind_Master: 0<br>Master_SSL_Verify_Server_Cert: No<br> Last_IO_Errno: 0<br> Last_IO_Error: <br> Last_SQL_Errno: 0<br> Last_SQL_Error: <br>1 row in set (0.00 sec)<br><br></code></pre>
<p>只有当</p>
<pre class="prettyprint hljs-dark"><code class="hljs avrasm"><span class="hljs-label">Slave_IO_Running:</span> Yes <span class="hljs-preprocessor">#</span><br><span class="hljs-label">Slave_SQL_Running:</span> Yes <span class="hljs-preprocessor">#</span><br><br></code></pre>
<p>则说明主从mysql运行正常</p>
</div><div id="wmd-preview-section-25" class="wmd-preview-section preview-content">
<h4 id="测试主从">测试主从</h4>
<p>登陆主mysql,删除db1库中的host表</p>
<pre class="prettyprint hljs-dark"><code class="hljs smalltalk">[root@stady ~]# mysql -u root -<span class="hljs-class">S</span> /tmp/mysql.sock -p <br> mysql> use db1<br> <span class="hljs-class">Database</span> changed<br> mysql> show tables;<br> +---------------------------+<br> | <span class="hljs-class">Tables_in_db1</span> |<br> +---------------------------+<br> <span class="hljs-localvars">| columns_priv |</span><br> <span class="hljs-localvars">| event |</span><br> <span class="hljs-localvars">| func |</span><br> <span class="hljs-localvars">| general_log |</span><br> <span class="hljs-localvars">| help_category |</span><br> <span class="hljs-localvars">| help_keyword |</span><br> <span class="hljs-localvars">| help_relation |</span><br> <span class="hljs-localvars">| help_topic |</span><br> <span class="hljs-localvars">| host |</span><br> <span class="hljs-localvars">| ndb_binlog_index |</span><br> <span class="hljs-localvars">| plugin |</span><br> <span class="hljs-localvars">| proc |</span><br> <span class="hljs-localvars">| procs_priv |</span><br> <span class="hljs-localvars">| servers |</span><br> <span class="hljs-localvars">| slow_log |</span><br> <span class="hljs-localvars">| tables_priv |</span><br> <span class="hljs-localvars">| time_zone |</span><br> <span class="hljs-localvars">| time_zone_leap_second |</span><br> <span class="hljs-localvars">| time_zone_name |</span><br> <span class="hljs-localvars">| time_zone_transition |</span><br> <span class="hljs-localvars">| time_zone_transition_type |</span><br> <span class="hljs-localvars">| user |</span><br> +---------------------------+<br> <span class="hljs-number">22</span> rows in set (<span class="hljs-number">0.01</span> sec)<br><br> mysql> drop table host;<br> <span class="hljs-class">Query</span> <span class="hljs-class">OK</span>, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0.00</span> sec)<br><br></code></pre>
<p>登入从mysql,查看db1库中是否存在host表</p>
<pre class="prettyprint hljs-dark"><code class="hljs smalltalk">[root@stady ~]# mysql -u root -<span class="hljs-class">S</span> /tmp/mysql-slave.sock <br> mysql> use db1<br> <span class="hljs-class">Database</span> changed<br> mysql> show tables;<br> +---------------------------+<br> | <span class="hljs-class">Tables_in_db1</span> |<br> +---------------------------+<br> <span class="hljs-localvars">| columns_priv |</span><br> <span class="hljs-localvars">| event |</span><br> <span class="hljs-localvars">| func |</span><br> <span class="hljs-localvars">| general_log |</span><br> <span class="hljs-localvars">| help_category |</span><br> <span class="hljs-localvars">| help_keyword |</span><br> <span class="hljs-localvars">| help_relation |</span><br> <span class="hljs-localvars">| help_topic |</span><br> <span class="hljs-localvars">| ndb_binlog_index |</span><br> <span class="hljs-localvars">| plugin |</span><br> <span class="hljs-localvars">| proc |</span><br> <span class="hljs-localvars">| procs_priv |</span><br> <span class="hljs-localvars">| servers |</span><br> <span class="hljs-localvars">| slow_log |</span><br> <span class="hljs-localvars">| tables_priv |</span><br> <span class="hljs-localvars">| time_zone |</span><br> <span class="hljs-localvars">| time_zone_leap_second |</span><br> <span class="hljs-localvars">| time_zone_name |</span><br> <span class="hljs-localvars">| time_zone_transition |</span><br> <span class="hljs-localvars">| time_zone_transition_type |</span><br> <span class="hljs-localvars">| user |</span><br> +---------------------------+<br> <span class="hljs-number">21</span> rows in set (<span class="hljs-number">0.00</span> sec)<br><br></code></pre>
<p>在从myslq的db1库中没有host表,说明主从复制配置成功</p>
</div><div id="wmd-preview-section-26" class="wmd-preview-section preview-content">
<h3 id="注意">注意:</h3>
<p><strong><em>如果需重启主mysql,务必要先把从mysql的slave停掉,即在从mysql上执行:</em></strong></p>
<pre class="prettyprint hljs-dark"><code class="hljs vim"> mysql> slave <span class="hljs-keyword">stop</span>;<br> Query OK, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0.00</span> sec)<br><br> mysql> <span class="hljs-keyword">quit</span><br> Bye<br><br></code></pre>
<p>然后再重启主、从mysql</p>
<pre class="prettyprint hljs-dark"><code class="hljs coffeescript">[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># service mysqld stop</span><br>Shutting down MySQL....... SUCCESS! <br>[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># /etc/init.d/mysqld-slave stop</span><br>Shutting down MySQL..... SUCCESS! <br>[root<span class="hljs-property">@stady</span> ~]<span class="hljs-comment"># </span><br><br></code></pre></div><div id="wmd-preview-section-footnotes" class="preview-content"></div></div></body></html>