mysql的安装以及主从复制部署

一、Mysql安装:

查看是否已经安装MySQL:rpm -qa|grep -i mysql,如果已经安装可以先卸载rpm -e --nodeps  mysql-libs     --nodeps代表:可能其他地方有依赖mysql,这里强制卸载

两种方式:

1.rpm方式(未安装wget,执行命令: yum install wget):

①wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

②yum -y install mysql57-community-release-el7-10.noarch.rpm

③开始安装MySQL服务器  代码:yum -y install mysql-community-server

④启动mysql:systemctl start  mysqld.service,查看MySQL的运行状态:systemctl status mysqld.service

⑤找出MySQL的密码:grep "password" /var/log/mysqld.log

⑥命令修改数据库密码:/usr/bin/mysqladmin -u root -p password ' Wy_123456‘

⑦进入MySQL:MySQL -u root -p   密码是:Wy_123456

2.压缩包安装:

①首先下载压缩安装包(选择你想要的版本):

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.6.38-linux-glibc2.12-i686.tar.gz

②解压到/usr/local/下,重命名为mysql:tar -zxvf mysql-5.6.38-linux-glibc2.12-i686.tar.gz -C /usr/local/ && cd /usr/local/&&mv mysql-5.6.38-linux-glibc2.12-i686/ mysql 

③添加系统mysql组和mysql用户:groupadd mysql&&useradd -r -g mysql mysql

④创建mysql数据目录,新目录不存在则创建

       在MySQL中常见目录   mkdir -p data

       ⑤修改/etc/my.cnf文件(MySQL的配置文件):

datadir=/usr/local/mysql/data

 

       ⑥修改目录权限:chown -R mysql:mysql  /usr/local/mysql

       ⑦初始化数据库,进入mysql目录:

./bin/mysqld --user=mysql --basedir=/usr/local/mysql  --datadir=/usr/local/mysql/data/  --initialize

./bin/mysql --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --initialize ;

       记得要记住此时生成的临时密码,如果忘了可以去日志中查找 grep "password"

/位置/mysqld.log

⑧启动mysql服务和添加开机启动mysql服务(在mysql目录下):cp -a ./support-files/mysql.server /etc/init.d/mysql

chmod +x /etc/init.d/mysql

chkconfig --add mysql

检查服务是否生效:chkconfig --list mysql

⑨这是启动mysql就可以用:

# 启动

service mysql start;

# 查看启动状态

service mysql status;

⑩进入mysql:

mysql -uroot -p

进入后可能不能执行任何sql语句,因为需要修改密码,执行语句:

alter user 'root'@'localhost' identified with mysql_native_password by ’你的密码’;

如果想要所有的服务器都可以访问那么把上面的localhost改为%

刷新

flush privileges;

二、安装中可能出现的问题

1、无法启动:是不是日志等文件无法生成的问题,那就是mysql权限的问题:执行下面命令:chmod 777 日志文件生成位置文件夹   chown  -R mysql:mysql 文件夹

2.忘记密码:

①vim /etc/my.cnf,添加 skip-grant-table(跳过权限验证)

②重启服务

③重置密码:

use mysql;

update user set authentication_string='密码' where user='root';

alter user 'root'@'localhost' identified with mysql_native_password by '密码';

flush privileges;

④vim /etc/my.cnf,删除 skip-grant-table

⑤再次重启 mysql 服务,使用新密码登录 mysql

三、mysql的主从配置

1、主库配置:

①先在/etc/my.cnf中添加:

server-id=1

innodb_flush_log_at_trx_commit=1

sync_binlog=1

sync_relay_log=1

binlog_format=ROW

relay-log-info-repository=TABLE

master-info-repository=TABLE

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=4

relay_log_recovery=ON

log-bin=binlog

# binlog_do_db = test1,test2    #日志记录那些数据库

# binlog_ignore_db = mysql,performance_schema,information_schema #日志记录忽略那些数据库的

lower_case_table_names = 1

default-storage-engine=INNODB

transaction-isolation=READ-COMMITTED

innodb_buffer_pool_size = 2G

skip-name-resolve

back_log = 500

key_buffer_size = 400M

max_allowed_packet = 8M

thread_stack = 256K

sort_buffer_size = 16M

innodb_log_file_size=2G

max_connections=1000

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

lower_case_table_names=1

②重启mysql (service mysql restart)

③在主库创建一个用于同步的用户(当然也可以不创建,直接用已有的,不过为了安全建议还是创建一个)

grant replication slave on *.* to 'slave'@'172.16.36.110' identified by '123456';

flush privileges;刷新授权表

意思是:这个slave用户密码为123456,但有同步的权限,且只能在172.16.36.110上连接,要是所有的服务器都可以连接的话改成%,此时可以去172.16.36.110服务器上测试下看能连接不:mysql  -h  主库服务器  -uslave -p  输入密码123456

如果可以连接成功则配置成功

④主服务器锁表:flush tables with read lock; #锁定数据表(目的是暂时使其不能继续写,保持现有状态用于同步)

备份主库中需要同步的数据库: mysqldump -uroot -p123456 db1 > /backup/db1.sql

⑤主库执行:show master status; 记下 Position File 的值,等下进行从库配置要用到。

2.从库配置:

①现在数据库中导入需要同步的数据库:

scp root@主库服务器: /backup/db1.sql  /data/.

进入mysql   source  /data/db1.sql

②修改/etc/my.cnf,添加配置

server-id=2

innodb_flush_log_at_trx_commit=1

sync_binlog=1

sync_relay_log=1

binlog_format=ROW

relay-log-info-repository=TABLE

master-info-repository=TABLE

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=4

relay_log_recovery=ON

# replicate_do_db = test1,test2 #是在slave上配置,指定slave要复制哪个库,不指定则是整个数据库

# replicate-ignore-db=mysql,performance_schema,information_schema #是在slave上配置,指定slave要忽略哪个库

log-bin=binlog

lower_case_table_names = 1

default-storage-engine=INNODB

transaction-isolation=READ-COMMITTED

innodb_buffer_pool_size = 2G

skip-name-resolve

back_log = 500

key_buffer_size = 400M

max_allowed_packet = 8M

thread_stack = 256K

sort_buffer_size = 16M

innodb_log_file_size=2G

max_connections=1000

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

 

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

③重启mysql

④执行下面命令:

stop slave;

change master to

master_host='主库服务器',

master_user='slave',

master_password='123456',

master_port=3306,

master_log_file='刚才主库的File',

master_log_pos=刚才主库的position;

slave start;

⑤从库执行:show slave status\G; 查看是否正常

lave_IO_Running: Yes

Slave_SQL_Running: Yes

Master_Log_File: 刚才主库的File

Relay_Master_Log_File: 刚才主库的File

Read_Master_Log_Pos: 刚才主库的position

Exec_master_log_pos: 刚才主库的position

3、回到主库解锁表

进入mysql  执行: unclock tables;

另附上完整的主库my.cnf:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

bind-address=0.0.0.0
#skip-grant-tables
#datadir=/var/lib/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
#socket=/tmp/mysql/mysql.sock
port=3306
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
symbolic-links=0
innodb_file_per_table=1
pid-file = /usr/local/mysql/data/mysql.pid
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
#tmpdir = /tmp
#master-slave配置
server-id=1
innodb_flush_log_at_trx_commit=1
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

bind-address=0.0.0.0
#skip-grant-tables
#datadir=/var/lib/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
#socket=/tmp/mysql/mysql.sock
port=3306
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
symbolic-links=0
innodb_file_per_table=1
pid-file = /usr/local/mysql/data/mysql.pid
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
#tmpdir = /tmp
#master-slave配置
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
sync_relay_log=1
binlog_format=ROW
relay-log-info-repository=TABLE
master-info-repository=TABLE
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
relay_log_recovery=ON
log-bin=binlog
lower_case_table_names = 1
default-storage-engine=INNODB
transaction-isolation=READ-COMMITTED
innodb_buffer_pool_size = 2G
skip-name-resolve
back_log = 500
key_buffer_size = 400M
max_allowed_packet = 8M
thread_stack = 256K
sort_buffer_size = 16M
innodb_log_file_size=2G
max_connections=1000

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

 

 

 

 

 

 

            

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值