linux下mysql多实例安装,并实现主主同步

记录一下linux下mysql安装方法。

需要的组建如下,使用yum方式安装:yum install gcc gcc-c++ kernel-devel ncurses-devel bison cmake

拷贝mysql linux安装包至linux系统中 例如放在:/usr/local/src/mysql/Iddbs_DB.tar.gz

解压:tar zxvf /usr/local/src/mysql/Iddbs_DB.tar.gz

进入目录:cd /usr/local/src/mysql/Iddbs_DB

1.有些版本mysql需要先 ./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysql

然后:make && make install

2.这个版本直接:./install  一路y


添加用户和用户组

groupadd mysql #添加mysql用户组  
useradd -g mysql mysql #创建mysql用户并把它放到mysql组下  
chown -R root:mysql /usr/local/mysql #修改mysql文件属性 


初始化数据目录

#把用到的工具添加到/usr/bin目录  
ln -s /usr/local/mysql/bin/mysqld_multi /usr/bin/mysqld_multi  
ln -s /usr/local/mysql/bin/mysql_install_db /usr/bin/mysql_install_db 


创建文件夹

/usr/local/var/mysql1

/usr/local/var/mysql2

/usr/local/var/mysql3

/usr/local/var/mysql4


#初始化四个数据目录
/usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/var/mysql1 --user=mysql
/usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/var/mysql2 --user=mysql
/usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/var/mysql3 --user=mysql
/usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/var/mysql4 --user=mysql

?这样:/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &


#修改属性
chown -R mysql /usr/local/var/mysql1
chown -R mysql /usr/local/var/mysql2
chown -R mysql /usr/local/var/mysql3
chown -R mysql /usr/local/var/mysql4


配置多实例启动脚本
从MySQL的源码中把复制到/etc/init.d/目录下
cp /usr/local/src/mysql-5.1.60/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server

#修改basedir和bindir为安装路径(my.cnf中)
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin

添加mysql/bin到path中


配置多实例数据库配置文件
     用mysqld_multi工具查看该配置文件的模板方法,命令为:mysqld_multi --example
     在/etc/目录下创建创建文件/etc/mysqld_multi.cnf,把mysqld_multi --example产生的文件粘进去,修改相应属性,如:mysqld,mysqladmin,socket,port,pid-file,datadir,user等。


[mysqld_multi]
mysqld     = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
#user       = mysql
#password   = my_password


[mysqld1]
socket     = /usr/local/var/mysql1/mysql1.sock
port       = 3306
pid-file   = /usr/local/var/mysql1/mysql1.pid
datadir    = /usr/local/var/mysql1
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

character-set-server=utf8


[mysqld2]
socket     = /usr/local/var/mysql2/mysql2.sock
port       = 3307
pid-file   = /usr/local/var/mysql2/mysql2.pid
datadir    = /usr/local/var/mysql2
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

character-set-server=utf8


[mysqld3]
socket     = /usr/local/var/mysql3/mysql3.sock
port       = 3308
pid-file   = /usr/local/var/mysql3/mysql3.pid
datadir    = /usr/local/var/mysql3
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

character-set-server=utf8


[mysqld4]
socket     = /usr/local/var/mysql4/mysql4.sock
port       = 3309
pid-file   = /usr/local/var/mysql4/mysql4.pid
datadir    = /usr/local/var/mysql4
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

character-set-server=utf8


单实例启动方法:

启动服务:
[root@localhost init.d]# ./mysql.server start
关闭服务:
[root@localhost init.d]# ./mysql.server stop


mysqld: 未被识别的服务 解决方法:
/etc/init.d/mysql.server 拷贝一份放到原文件中,改名 mysqld
启动方法:
service mysqld start
service mysqld stop
服务可用

mysql.server 原始位置在mysql安装目录support-files下

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld


启动多实例数据库

将/usr/local/mysql/bin加到$PATH环境变量里
export PATH=/usr/local/mysql/bin:$PATH

永久加入PATH:

ls -lf

su - root

vi .bash_profile

添加 export  mysqlhome=/usr/local/mysql

在PATH=最后面添加 :$mysqlhome/bin

保存


#启动
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start

#关闭
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf stop

#查看数据库状态
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report

注:可在最后添加 1,2,3,4等,或1-4


其他:

#查看相应端口是否已经被监听
ps -ef | grep mysql
netstat -tunlp
  
#查看是否有活动进程
ps -aux|grep mysql


关闭防火墙:service iptables stop
开机不启动防火墙:chkconfig iptables off


关闭进程:
kill -TERM 5438

#进入端口为3306的数据库  
mysql -uroot -p -h127.0.0.1 -P3306  
  
#通过sock文件登录  
mysql -uroot -p -S /usr/local/var/mysql1/mysql1.sock  
  
 #查看socket文件  
mysql> SHOW VARIABLES LIKE 'socket';  
  
#查看pid文件  
mysql> SHOW VARIABLES LIKE '%pid%';


参考:http://chenzehe.iteye.com/blog/1266260

主主同步地址:


你可能感兴趣的:(linux,mysql)