2、安装mysql,我这里是通过脚本来安装二进制mysql5.5
#!/bin/bash
####Author:lsy####
####Time:2017/07/25####
####一键安装Mysql-5.5版本####
####查询mysql用户是否存在####
user=mysqlgroup=mysql#create group if not exists egrep "^$group" /etc/group >& /dev/nullif [ $? -ne 0 ]then groupadd $groupfi#create user if not exists egrep "^$user" /etc/passwd >& /dev/nullif [ $? -ne 0 ]then useradd -g $group $userfi####安装依赖####yum -y install autoconf libaio*####源码包存放路径####if [ -d /tools ]then echo "Dir already exists"else echo "Dir don't exists" mkdir /toolsficd /tools####下载Mysql源码包#####if [ -e /tools/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz ]#then# echo "Mysql源码包已存在"#else# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz#fi####二进制安装mysql####m_name="mysql-5.5.22-linux2.6-x86_64"package="${m_name}.tar.gz"M_dir=`ls -l /usr/local |egrep "^d" |awk '{print $9}' |egrep "^mysql"`if [ $? -eq 0 ]then echo "Mysql already exists"else tar xf $package -C /usr/local cd /usr/local mv $m_name mysql cd mysql chown -R mysql . chgrp -R mysql . #mkdir -p /data/mydb #chown -R mysql:mysql /data/mydb scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data cp support-files/mysql.server /etc/init.d/mysqldficd /etc/if [ -e /etc/my.cnf ]then echo "File already exists" > /etc/my.cnf cat >> /etc/my.cnf << EOF[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306server_id = 1socket = /tmp/mysql.sockEOFelse echo "File don't exists" cat >> /etc/my.cnf << EOF[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306server_id = 1socket = /tmp/mysql.sockEOFfiln -s /usr/local/mysql/bin/* /usr/bin####启动mysql####/etc/init.d/mysqld start####判断mysql是否正常启动####ps -ef |grep mysql |grep -v grep >/dev/nullif [ $? -eq 0 ]then echo "Mysqld started successfully"else echo "Mysqld started failed"fi
3、安装好mysql后,我们需要给mysql设置密码
#mysql
mysql> USE mysql ;
mysql> UPDATE user SET Password = password('123456') WHERE User='root';
mysql> flush privileges ;
mysql> quit
主库操作 4、创建主从同步的账号
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'slave'@'172.10.12.249' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
REPLICATION SLAVE用于复制型从属服务器(从主服务器中读取二进制日志事件)
RELOAD允许使用FLUSH
SUPER允许使用CHANGE MASTER, KILL, PURGE MASTER LOGS和SET GLOBAL语句
172.10.12.249是从库的地址
slave是需要同步的用户名
123456为远程同步密码
5、主从同步的账号创建好了,我们就要锁住主数据库的表
mysql>flush table with read lock;
mysql>show master status;
6、这步我备份了主数据库所有库,用于导入从数据库。
#mysqldump -uroot -p123456 --all-databases > /work/all.sql
从库操作 在从数据库上有以下几步 1)修改server-id 2)启用中继日志 3)连接主服务器 4)启动复制线程 7、修改从服务器的相关配置文件信息
# vim /etc/my.cnf
server-id = 2
relay-log = /usr/local/mysql/log/relay-bin
Mysql使用的相应目录都需要授权为mysql 用户的
chown -R mysql:mysql /var/log/mysql/logs/relay-bin
重启Mysql服务
/etc/init.d/mysqld restart
连接到从服务器的Mysql上
mysql -uroot -pits123
查看从服务器的中继日志是否在启动状态
mysql>show global variables like '%relay%'
8、在从库服务器上执行
mysql> CHANGE MASTER TO MASTER_HOST='172.10.12.248', MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=330, MASTER_CONNECT_RETRY=10;
参数说明
MASTER_HOST 主服务器的IP
MASTER_USER 主服务器上授权复制的用户名
MASTER_PASSWORD 主服务器上授权用名的密码
MASTER_PORT 主服务器的Mysql端口
MASTER_LOG_FILE 主服务器上的日志文件
MASTER_LOG_POS 主服务器上日志文件的位置
MASTER_CONNECT_RETRY 重连次数
9、启动进程
mysql>start slave;10、检查主从同步状态,主要检查Slave_IO_Running和Slave_SQL_Running的值,两个都是YES,才说明主从连接正确。如果有一个是no,需要重新确定刚才在从服务器上执行的命令,停掉"stop slave;",查询主库master状态,然后重新进行配置主从连接。
mysql>CREATE DATABASE IF NOT EXISTS tongbu DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
从库查询
mysql>show databases;
tongbu库存在说明,Mysql主从同步完成了。