说明:此处用mysql5.7.29分别在两台机器实例化安装部署。
106.13.145.174/3306(主)和39.101.213.45/3306(从)
1.分别在两台机器上安装, mysql-5.7.29解压版安装
1.1上传安装包到/usr/lcoal下
tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
1.2重命名并创建服务进程的日志文件
mv mysql-5.7.29-linux-glibc2.12-x86_64 mysql
cd mysql
mkdir log
mkdir data
echo "" > /usr/local/mysql/log/mysqld.log
chown -R root:root /usr/local/mysql/log/mysqld.log
1.3执行安装脚本(5.6版本的)
/usr/local/mysql/scripts/mysql_install_db --user=root --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
``
### 1.4执行安装脚本(5.7版本的)
/usr/local/mysql/bin/mysql_install_db --user=root --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
### 1.5 创建配置文件且修改配置文件
注:mysql5.7.18之后的版本没有默认的配置文件需要自己创建
直接上传找好的my.cnf文件 上传到 /etc/下面即可(此处若需要可以联系我获取)
启动前先修改
vim /usr/local/mysql/support-files/mysql.server
找到start模块,添加--user=root到mysqld_safe 即可
增加mysql环境变量
vim /etc/profile
增加
/usr/local/mysql/bin
保存退出Esc,:wq!
配置生效
source /etc/profile
创建自己的 mysql 服务启动脚本mysqlserver.sh,脚本内容如下:
!/bin/sh
if [ -z $1 ]; then
echo "ERROR::you must input a name(start/restart/stop/status)"
exit 0
fi
/usr/local/mysql/support-files/mysql.server $1
赋予脚本可执行权限(/www/shell此路径为我自己的脚本存放路径)
chmod +x /www/shell/mysqlserver.sh
启动mysql
/www/shell/mysqlserver.sh start
cd /usr/local/mysql/bin
./mysql -uroot -p
若启动有如下报错:
**ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/data/mysql.sock' (2)**
先查找mysql.sock 路径
find / -name mysql.sock
发现路径在
/tmp/mysql.sock
修改配置文件中mysql.sock路径为/tmp/mysql.sock
vim /etc/my.cnf
再重启mysql服务
cd /usr/local/mysql/bin
客户端连接mysql(此处可设置环境变量使得任意处可执行mysql)
./mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
查看初始密码
cat /root/.mysql_secret
mysql -uroot -p
再次登录后,设置远程登录并修改root初始密码
alter user 'root'@'localhost' identified by '123abc';
GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '123abc' WITH GRANT OPTION;
flush privileges;
## 2.mysql主从配置
### 2.1设置主库(106.13.145.174)配置文件:
vim /etc/my.cnf找到[mysqld]模块增加下面内容
server_id=3306
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/log/slow.log
binlog_format = mixed
log-bin = /usr/local/mysql/log/mysql3306_bin
重启数据库
/www/shell/mysqlserver.sh restart
mysql -uroot -p
登录成功后
SHOW MASTER STATUS;
记住File和Position的值,后面配置从库时需要的参数
mysql3306_bin.000001和154
### 2.2设置从库(39.101.213.45)配置文件:
vim /etc/my.cnf找到[mysqld]模块增加下面内容
server_id=3307
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/log/slow.log
binlog_format = mixed
log-bin = /usr/local/mysql/log/mysql3307_bin
和设置主库类似,wq!保存重启mysql并登录mysql -uroot -p
设置从库链接主库
change master to master_host='106.13.145.174',master_port=3306,master_user='root',master_password='123abc',master_log_file='mysql3306_bin.000001',master_log_pos=154;
运行完会有如下信息:
start slave;
show slave status \G
到此主从设置完毕。
## 3.测试主从
### 3.1登录master(106.13.145.174)主库创建test01数据库,并创建表slave_test
mysql -uroot -p
mysql> create database test01;
mysql> use test01;
mysql> create table slave_test(id int(6),name varchar(10));
mysql> insert into slave_test values(000001,'zhaoyanfei');
mysql> show tables;