Linux6.5安装mysql5.7和mysql主从搭建

centos 系统自带了mysql组件
[root@localhost mysql]# rpm -qa |grep mysql
mysql-libs-5.1.71-1.el6.x86_64
卸载组件
[root@localhost mysql]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
1 解压 安装包
tar –xvf /soft/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz –C /usr/local/
2 修改解压包的名字
mv mysql-5.7.20-linux-glibc2.12 mysql
3 安装 Libaio,numactl依赖
yum install libaio numactl –y
4 创建用户和组,创建目录,修改文件用户和组
groupadd mysql
useradd –r –g mysql mysql
mkdir /usr/local/data
chmod 770 /usr/local/mysql/data
chown -R mysql mysql
chgrp -R mysql mysql

5 创建mysql运行时的日志
mkdir /var/log/mariadb
6 初始化mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

生成的初始密码在首次以root用户登入时使用。
[root@dt01 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2017-12-06T12:54:25.429110Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-06T12:54:25.751425Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-12-06T12:54:25.835091Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-06T12:54:25.909643Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96d3e0b8-da84-11e7-b484-000c29caf9c5.
2017-12-06T12:54:25.914464Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2017-12-06T12:54:25.919081Z 1 [Note] A temporary password is generated for root@localhost: Nh>g9(Vl/;Jn

其原因是从 5.6开始,timestamp 的默认行为已经是 deprecated 了。
在MySQL 5.6.6之前,TIMESTAMP的默认行为:
•TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL。(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值。)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp。
•表中的第一个TIMESTAMP列,如果没有声明NULL属性、DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 属性。
•表中第二个TIMESTAMP列,如果没有声明为NULL或者DEFAULT子句,默认自动分配’0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配’0000-00-00 00:00:00′,且没有警告。
要关闭警告,需要加入下面的参数: [mysqld]
explicit_defaults_for_timestamp=true
重启MySQL后错误消失,这时TIMESTAMP的行为如下:
•TIMESTAMP如果没有显示声明NOT NULL,是允许NULL值的,可以直接设置改列为NULL,而没有默认填充行为。
•TIMESTAMP不会默认分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP属性。
•声明为NOT NULL且没有默认子句的TIMESTAMP列是没有默认值的。往数据表中插入列,又没有给TIMESTAMP列赋值时,如果是严格SQL模式,会抛出一 个错误,如果严格SQL模式没有启用,该列会赋值为’0000-00-00 00:00:00′,同时出现一个警告。(这和MySQL处理其他时间类型数据一样,如DATETIME)
也就是 explicit_defaults_for_timestamp 关闭了 timestamp 类型字段锁拥有的一些会让人感到奇怪的默认行为,加入了该参数之后,如果还需要为 timestamp类型的字段指定默认行为,那么就需要显示的在创建表时显示的指定。explicit_defaults_for_timestamp 也就是这个意思:显示指定默认值为timestamp类型的字段

bin/mysql_ssl_rsa_setup – basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

bin/mysqld_safe --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

7 将mysql加入到可控制启动服务的文件夹内,并命名mysql,即service可控制的服务名,届时可用service mysql start|stop控制启动及关闭mysql

cp support-files/mysql.server /etc/init.d/mysql

切换到到/etc目录下配置my.cnf

cd /etc/

vi my.cnf

在[mysqld]中添加或者修改一下内容:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

vi /etc/profile #配置环境变量 添加如下内容

export MYSQL_HOME="/usr/local/mysql"
export PATH=“ P A T H : PATH: PATH:MYSQL_HOME/bin”

source /etc/profile #使配置及时生效

配置开机启动mysql服务

chkconfig --add mysql

chkconfig --level 2345 mysql on

8 启动 mysql
service mysql start
注意: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’, 首先查看mysql 是否启动 ,没有则启动在连接 ,然后在看mysql.sock配置路径是否正确
五、首次登入mysql

mysql -u root -p

Enter password:输入之前生成的临时密码
mysql> SET PASSWORD = PASSWORD(‘123456’);
六、防火墙
CentOS升级到7之后,使用firewalld代替了原来的iptables。下面记录如何使用firewalld开放Linux端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload #重启防火墙

七、常见问题
1、Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
处理:修改vi my.cnf中的socket=/tmp/mysql.sock"。

安装成功 :
[root@dt01 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2017-12-06T12:54:25.429110Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-06T12:54:25.751425Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-12-06T12:54:25.835091Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-06T12:54:25.909643Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96d3e0b8-da84-11e7-b484-000c29caf9c5.
2017-12-06T12:54:25.914464Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2017-12-06T12:54:25.919081Z 1 [Note] A temporary password is generated for root@localhost: Nh>g9(Vl/;Jn

方法1: 用SET PASSWORD命令
  mysql -u root
  mysql> SET PASSWORD FOR ‘root’@‘localhost’ = PASSWORD(‘newpass’);
方法2:用mysqladmin
  mysqladmin -u root password “newpass”
  如果root已经设置过密码,采用如下方法
  mysqladmin -u root password oldpass “newpass”
方法3: 用UPDATE直接编辑user表
  mysql -u root
  mysql> use mysql;
  mysql> UPDATE user SET Password = PASSWORD(‘newpass’) WHERE user = ‘root’;
  mysql> FLUSH PRIVILEGES;
在丢失root密码的时候,可以这样
  mysqld_safe --skip-grant-tables&
  mysql -u root mysql
  mysql> UPDATE user SET password=PASSWORD(“new password”) WHERE user=‘root’;
  mysql> FLUSH PRIVILEGES;

远程服务器不允许你的java程序访问它的数据库。所以,我们要对远程服务器进行设置,使它允许你进行连接。
步骤:
一、打开mysql控制台,输入:use mysql;
二、输入:show tables;
三、输入:select host from user;
四、输入:update user set host =’%’ where user =‘root’;
五、打开计算机的服务界面,重新启动mysql服务,远程服务器就允许程序连接数据库了。

解决mysql 只能通过localhost 不能通过 ip访问的问题

1 选择mysql数据库
2 授权
grant all privileges on . to ’root’@’%’ identified by ’123456’;
3 刷新权限
flush privileges;
4 重启mysql
检查防火墙是否过滤掉端口

mysql 主从

主数据库配置

  1. 修改 /etc/my.cnf 添加如下内容
    server-id=200 #设置主服务器的ID
    innodb_flush_log_at_trx_commit=2
    sync_binlog=1 #开启binlog日志同步功能
    log-bin=mysql-bin-200 #binlog日志文件名
    binlog-do-db=xxxx # 这个表示只同步某个库 (如果没有此项,表示同步所有的库)
  2. 重启mysql ,然后登录mysql
  3. 授权
    mysql> grant replication slave on . to ‘root’@‘192.168.2.222’ identified by ‘123456’;
    ##授权给从数据库服务器192.168.2.222,用户名root,密码123456
  4. mysql>show master status ;

##查看主库的状态 file,position这两个值很有用。要放到slave配置中(分别对应的值master_log_file和master_log_pos的值)

从数据库配置
1) 修改 /etc/my.cnf 配置文件 ,加入一下内容
server-id=201
innodb_flush_log_at_trx_commit=2
sync_binlog=1
log-bin=mysql-bin-201

  1. 重启mysql ,然后登录mysql

  2. 授权
    mysql> change master to master_host=‘192.168.2.118’,master_user=‘root’,master_password=‘123456’,master_log_file=‘mysql-bin-200.000001’,master_log_pos=441;

  3. 开启和关闭从库(start slave/stop slave)
    mysql> start slave;
    Query OK, 0 rows affected (0.01 sec)

  4. 查看状态
    mysql> show slave status \G;

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.118 #主库地址
Master_User: root #主库用户
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-200.000001
Read_Master_Log_Pos: 441
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin-200.000001
Slave_IO_Running: Yes # slave_io_running:yes 和slave_sql_
Slave_SQL_Running: Yes # running:yes 表示 配置成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 441
Relay_Log_Space: 535
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 200
Master_UUID: eefd984e-10aa-11e8-aeb7-000c29c9cbb7
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

验证

  1. 在主库建一个数据库,在从库查看是否有
  2. 在主库创建一个表,在从库中查看
  3. 在主库插入一条数据,在从库查看
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值