一台服务器启动多个mysql数据库
安装完mysql后
复制mysql数据库到当前路径并且重命名为mysql2
cp -r /usr/local/mysql /usr/local/mysql2
切换路径到mysql2
cd /usr/local/mysql2
复制my.cnf文件到mysql2下
cp /etc/my.cnf /usr/local/mysql2
编辑mysql2的my.cnf
vim /usr/local/mysql2/my.cnf
更改内容:
[mysqld]
port = 3307
socket = /tmp/mysql2.sock
datadir =/data/mysql2
初始化数据库mysql2
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql2
为mysql2创建启动脚本
cp /etc/init.d/mysqld /etc/init.d/mysql2
vim /etc/init.d/mysql2
更改内容:
basedir=/usr/local/mysql2
datadir=/data/mysql2
conf=$basedir/my.cnf
启动数据库mysql2
/etc/init.d/mysql2 start
主从配置
A主,B从
登录方式:
mysql -S /tmp/mysql.sock 或者 mysql -h127.0.0.1 -p3306
mysql -S /tmp/mysql2.sock 或者 mysql -h127.0.0.1 -p3307
mysql
创建库:create database db1;
quit
复制一些数据到数据库:
mysqldump -S /tmp/mysql.sock mysql > 123.sql
mysql -S /tmp/mysql.sock db1 < 123.sql 把数据导入到数据库
查看数据
mysql
use db1;
show tables;
quit
编辑主mysql的my.cnf
vim /etc/my.cnf
server-id = 1
log-bin=yanglog
binlog-do-db=db1,db2 (选项)
binlog-ignore-db=mysql (选项)
:wq
修改完毕配置文件重启数据库
/etc/init.d/mysqld restart
创建一个传输binlog的用户
mysql
grant replication slave on *.* to ‘repl‘@‘127.0.0.1‘ identified by ‘123123‘; 创建用户
flush privileges; 刷新权限
flush tables with read lock; 表的读权限锁死
show master status; 读一个数据
quit
从上操作:
修改配置文件:
vim /usr/local/mysql2/my.cnf
serverid = 11
replicate-do-db=db1(选项)
replicate-ignore-db=db1(选项)
:wq
mysql -S /tmp/mysql2.sock -e "create database db1" 创建数据库
mysql -S /tmp/mysql2.sock db1 < 123.sql 导入数据
mysql -S /tmp/mysql2.sock 登录数据库
slave stop; 停止库
change master to master_host=‘127.0.0.1‘, master_port=3306, master_user=‘repl‘, master_password=‘123123‘, master_log_file=‘yanglog.000001‘, master_log_pos=106;(指定主的信息)
slave start; 启动库
show slave status\G; 查看主从配置是否成功
slave stop 停止库
quit 退出
/etc/init.d/mysqld2 restart 重启数据库
mysql -S /tmp/mysql2.sock
show slave status\G; 查看主从配置是否成功
本文出自 “百闻世界” 博客,请务必保留此出处http://anson12315.blog.51cto.com/2441584/1914617
原文地址:http://anson12315.blog.51cto.com/2441584/1914617