在一台机上搭建多个MYSQL并设置主从
安装
cd /usr/local/src/ tar zxvf /usr/local/src/mysql-5.5.53-linux2.6-x86_64.tar.gz mv mysql-5.5.53-linux2.6-x86_64 /usr/local/mysql grep mysql /etc/passwd #没有则添加用户 useradd -s /sbin/nologin mysql #添加启动路径 vim /etc/profile.d/path.sh #!/bin/bash/ export PATH=$PATH:/usr/local/mysql/bin cd /usr/local/mysql/ cp support-files/my-small.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld
修改配置文件
vim !$ basedir=/usr/local/mysql datadir=/data/mysql
添加开机服务
chkconfig –add mysqld mkdir -p /data/mysql ; chown -R mysql:mysql /data/mysql
编译并开启
./scripts/mysql_install_db –user=mysql –datadir=/data/mysql
/etc/init.d/mysqld start
复制mysql
cd .. cp -r mysql mysql_slave cd mysql_slave cp /etc/my.cnf .
更改从配置文件
vim my.cnf [mysqld] port = 3307 socket = /tmp/mysql_slave.sock datadir =/data/mysql_slave
编译复制修改开机启动服务
./scripts/mysql_install_db –user=mysql –datadir=/data/mysql_slave cp /etc/init.d/mysqld /etc/init.d/mysqldslave vim /etc/init.d/mysqldslave basedir=/usr/local/mysql_slave datadir=/data/mysql_slave cont=$basedir/my.cnf
设置开机启动并启动从
chkconfig –add mysqldslave /etc/init.d/mysqldslave start netstat -lnp |grep mysql
主增加一个测试数据库db1
mysqldump -S /tmp/mysql.sock mysql > 123.sql mysql -S /tmp/mysql.sock -e “create database db1” mysql -S /tmp/mysql.sock db1 < 123.sql
改主配置文件
vim /etc/my.cnf server-id = 1 # Uncomment the following if you want to log updates log-bin=junwei #白名单 #binlog-do-db=db1,db2 #黑名单 #binlog-ignore-db=mysql
重启mysql主登陆创建授权用户更新
service mysqld restart mysql -S /tmp/mysql.sock grant replication slave on *.* to ‘repl’@’127.0.0.1’ identified by ‘123123’; flush privileges; flush tables with read lock; show master status;
修改从配置文件
vim /usr/local/mysql_slave/my.cnf server-id = 2 #黑名单 #replicate-ignore-db=mysql #白名单 #replicate-do-db=mysql
从创建数据库导入数据
mysql -S /tmp/mysql_slave.sock -e “create database db1” mysql -S /tmp/mysql_slave.sock db1< 123.sql
从登陆设置同步
mysql -S /tmp/mysql_slave.sock slave stop; change master to master_host=’127.0.0.1′, master_port=3306,master_user=’repl’, master_password=’123123′, master_log_file=’junwei.000001′, master_log_pos=332; quit service mysqldslave restart mysql -S /tmp/mysql_slave.sock slave start; show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes #这两个选项yes说明配置成功
解锁主数据库
unlock tables;