在原来LNMP 的环境下
[root@LNMP ~]# vim /etc/init.d/mysqld #查找数据库的安装路径
[root@LNMP ~]# cd /data/mysql/
[root@LNMP mysql]# ls
[root@LNMP mysql]# tar czvf discuz.tar /data/mysql/discuz/
#备份之前的discuz 论坛数据库表。
-----------------------------------------------------------------------
安装mysql:
[root@LNMP mysql]# rm -rf /usr/local/mysql #删除之前的安装
[root@LNMP mysql]# cd /usr/local/src/
[root@LNMP src]# tar zxvf mysql-5.1.73-linux-x86_64-glibc23.tar.gz
[root@LNMP src]# mv mysql-5.1.73-linux-x86_64-glibc23 /usr/local/mysql
[root@LNMP src]# ls /usr/local/mysql
bin COPYING data docs include INSTALL-BINARY lib man mysql-test README scripts share sql-bench support-files
[root@LNMP src]# grep 'mysql' /etc/passwd #检查有没有 mysql 这个用户,如果没有就创建
mysql:x:500:500::/home/mysql:/sbin/nologin
[root@LNMP mysql]# cp support-files/my-small.cnf /etc/my.cnf #拷贝配置文件
cp: overwrite `/etc/my.cnf'? y
[root@LNMP mysql]# cp support-files/mysql.server /etc/init.d/mysqld
cp: overwrite `/etc/init.d/mysqld'? y
[root@LNMP ~]# vim /etc/init.d/mysqld #编译mysql配置文件
basedir=/usr/local/mysql #安装路径
datadir=/data/mysql #数据库路径
[root@LNMP mysql]# rm -rf /data/mysql #删除原来的/datadir
[root@LNMP mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
#初始化 ,看到下面两个OK,则安装完成
[root@LNMP mysql]# echo $?
0
[root@LNMP mysql]# ls /data/mysql #安装无误会生成这两个目录
mysql test
[root@LNMP ~]# /etc/init.d/mysqld start #启动mysql
[root@LNMP ~]# ps aux |grep mysql #查看
[root@LNMP ~]# netstat -lnp |grep 3306
----------------------------------------分割线----------------------------------------
[root@LNMP ~]# cd /usr/local/
[root@LNMP local]# ls
bin etc games include jdk1.8.0_112 lib lib64 libexec man mysql nginx php pureftpd resin sbin share src tomcat
[root@LNMP local]# cp -r mysql mysql_slave #创建第二个mysql (复制)
[root@LNMP mysql_slave]# chown -R mysql:mysql /data/mysql_slave #
[root@LNMP mysql_slave]# cp /etc/my.cnf . #拷贝配置文件到mysql_slave 下
[root@LNMP mysql_slave]# vim my.cnf #编译配置文件
# The MySQL server
[mysqld]
port = 3307 #3306 改为3307
socket = /tmp/mysql_slave.sock #/tmp/mysql.sock改为/tmp/mysql_slave.sock
datadir = /data/mysql_slave #定义数据库的目录
[root@LNMP mysql_slave]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql_slave #初始化数据库
[root@LNMP mysql_slave]# cd /etc/init.d/
[root@LNMP init.d]# cp mysqld mysqldslave #复制启动文件
root@LNMP init.d]# vim mysqldslave #编译启动文件
basedir=/usr/local/mysql_slave #mysql 更改为mysql_slave
datadir=/data/mysql_slave #mysql 更改为mysql_slave
conf=/$basedir/my.cnf #(这一条也可以写在这里)
# Try to find basedir in /etc/my.cnf
conf=/$basedir/my.cnf #更改conf=/etc/my.cnf 为 conf=/$basedir/my.cnf
-----------------------------------------------------------------------
[root@LNMP init.d]# /etc/init.d/mysqldslave start
Starting MySQL. SUCCESS!
[root@LNMP init.d]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@LNMP init.d]# netstat -lnp |grep mysql #两个mysql 都启来了
#扩展,如查需求还要启动一个mysql怎么解决
?按照开始的配置重新再配置即可。
(因为楼主是LNMP 的环境下来搭建的,在这过程中出现了的些差错,由于楼主是个初学小白,排错的经验少了点,这之前出个两个问题,一是mysql帐户登不了,二是第二个mysql 初始化的时候不成功。
mysql登不了,是没有赋权,赋权就好了[root@LNMP mysql_slave]# chown -R mysql:mysql /data/mysql_slave 。初始化不成功)
---------------------------------------------------------------------------------
Mysql 主从配置:主是3306 从是3307
登录mysql 的几种方工
[root@LNMP ~]# which mysql
/usr/bin/mysql #楼主预装了mysql 的命令
[root@LNMP ~]# /usr/local/mysql/bin/mysql #如果没有预装,就用绝对路径来登mysql
(把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:
# cd /usr/local/bin
# ln -fs /MYSQLPATH/bin/mysql mysql)
[root@LNMP ~]# mysql -S /tmp/mysql.sock #通过端口登录
[root@LNMP ~]# mysql #直接命令登录
[root@LNMP ~]# mysql -S /tmp/mysql_slave.sock #3307 的登录
[root@LNMP ~]# mysql -h127.0.0.1 -P3307 #通过端口登,(大P)
------------------------------------------------------------
[root@LNMP ~]# mysql -S /tmp/mysql.sock
mysql> create database db1; #创建一个测试数据库
Query OK, 1 row affected (0.05 sec)
[root@LNMP ~]# mysqldump -S /tmp/mysql.sock mysql > 123.sql #拷贝一些测试数据
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@LNMP ~]# mysql -S /tmp/mysql.sock db1 < 123.sql #恢复数据到db1
mysql> use db1;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_db1 |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
23 rows in set (0.00 sec)
[root@LNMP ~]# vim /etc/my.cnf #配置主的配置文件
server-id = 1 #打开server-id,这个数值主和从不能一样
og-bin=mysql-bin #打开log-bin,文件名是可以自定义的,楼主改为"rizhi"。
binlog-do-db=db1 #只针对db1做主从,后面可以跟多个库
#扩展(binlog-ignore-db=mysql,如果有上百个库,可以反向思考,做个黑名单,指定不同步的库mysql)
[root@LNMP ~]# /etc/init.d/mysqld restart #配置完成后,重启Mysql
Shutting down MySQL... SUCCESS!
Starting MySQL. SUCCESS!
[root@LNMP ~]# ls /data/mysql #系统会生成一个mysql-bin 为前缀的文件
db1 ibdata1 ib_logfile0 ib_logfile1 LNMP.err LNMP.pid mysql mysql-bin.000001 mysql-bin.index test
[root@LNMP ~]# mysql -S /tmp/mysql.sock #登录主,创建用户 replication:权限 ,repl:用户名
mysql> grant replication slave on *.* to 'repl'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock; #锁死读的权限
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; #查看mrizhi.0000001 的相关数据,在配置从同步的时候会用到
-----------------------------------------
[root@LNMP ~]# vim /usr/local/mysql_slave/my.cnf #从的配置
server-id = 1111 #打开server-id,且自定义更改1111
#replicate-do-db=db1(扩展)
[root@LNMP ~]# ls
123.sql anaconda-ks.cfg install.log install.log.syslog opt www.blog-andy.com.conf
[root@LNMP ~]# mysql -S /tmp/mysql_slave.sock -e "create database db1" #在从上创建db1
[root@LNMP ~]# mysql -S /tmp/mysql_slave.sock db1<123.sql
# 要实现同步,就要保证这两台机上库是一样的
[root@LNMP ~]# mysql -S /tmp/mysql_slave.sock #登录从
mysql> slave stop; #关掉数据库
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='127.0.0.1', master_port=3306, master_user='repl', master_password='123456',master_log_file='rizhi.000001', master_log_pos=106;
Query OK, 0 rows affected (0.04 sec)
#这是个关键核心配置,语法拼写,一定要严谨。
mysql> slave start; #启动从
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G; #查看状态 #没有配置成功
查看下面的报错:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
[root@LNMP ~]# /etc/init.d/mysqldslave restart #重启从,刚才修改了server-id 没有重启
[root@LNMP ~]# mysql -S /tmp/mysql_slave.sock
mysql> show slave status\G;
------------------------------------------------------
主从测试:
在主上的操作:
(mysql> flush tables with read lock; #因为之前有锁死的操作)
mysql>unlock tables; #解锁读的权限
mysql> use db1;
Database changed
mysql> show tables;
mysql> drop table help_category;
在从上的操作:
mysql> use db1;
Database changed
mysql> show tables;#详情见下图,同步成功。
在主上的操作: 创建表
mysql> show create table user\G: #用user表做范例
Create Table: CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
。。。。。。下略
CREATE TABLE `linuxt`
(
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
。。。。。。。。。。。
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges';
#在结尾去加分号“;”在命名表名的时候不要有数字,楼主开始的表名内有数字,总是报错。
mysql> use db1;
mysql> show tables;
删除数据库操作:在从上同步成功。
----------------------------------------------------------------------
mysql 的主从机制相当的脆弱,很容易就中断了,楼主在实验的过程中,不小心在从上进行了数据表的创建,最后发现不对。回到主上现来操作,发现创建不了,最后只能重新恢复数据表,再重新操作。
在实际生产环境中,做监控。
Slave_IO_Running: Yes #监控这两项是不是YES
Slave_SQL_Running: Yes
Last_IO_Error: (查看此处有没有报错)
一定不要在从上去写数据,这样操作会导致数据库紊乱。