centos下mysql安装以及双向同步配置

文章引用了:http://www.2cto.com/database/201501/371451.html

大概流程借鉴了上面连接,担心好的文章可能缺少维护。所以复制粘贴一下并且修改一下。

(1)CentOS版本:CentOS-7

查看方法:

[root@bogon 桌面]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)

安装rpm包:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-embedded-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-test-advanced-5.6.22-1.el7.x86_64.rpm

2. 卸载MariaDB

如果直接点击rpm包安装会得到错误提示。因为CentOS的默认数据库已经不再是MySQL了,而是MariaDB,为什么呢?

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

查看当前安装的mariadb包:

[root@bogon 桌面]# rpm -qa | grep mariadb

[root@bogon 桌面]# rpm -qa | grep maria*

将它们统统强制性卸载掉:

[root@bogon 桌面]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64(可能你电脑上不是这个版本)输入你的版本

[root@bogon 桌面]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64

[root@bogon 桌面]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64

3. 安装MYSQL

双击下面三个包进行自动安装:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

提示:其实第二个包devel我也不知道是干什么的,也不知道是不是必须的(上网搜了一下应该不是必须的),没有测试是否必须就已经点来装上了,也不想花时间去测试是否必须了,有测试过的朋友麻烦留言告知。

4. 启动MYSQL

[root@bogon 桌面]#service mysql start

得到错误:ERROR!The server quit without updating PID file

我们这里主要是因为:selinux惹的祸,如果是centos系统,默认会开启selinux。解决方法是关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。

然后再启动mysql就没问题了:

[root@bogon 桌面]#service mysql start

查看MySQL运行状态:

[root@bogon 桌面]# service mysql status

SUCCESS! MySQL running (2377)

5. 默认root用户登录MYSQL

[root@bogon 桌面]# mysql -u root -p

Enter password:

ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password: YES)

(1) 停止MySQL服务

[root@bogon 桌面]# service mysql stop

Shutting down MySQL.. SUCCESS!

(2) 输入绕过密码认证命令

[root@bogon 桌面]# mysqld_safe --user=mysql --skip-grant-tables--skip-networking &

150117 22:23:31 mysqld_safe Logging to '/var/lib/mysql/bogon.err'.

150117 22:23:31 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql

(3) 输入登录用户命令:(步骤三应该另起一个终端进行操作->)

[root@bogon 桌面]# mysql -u root mysql

Reading table information for completion oftable and column names

You can turn off this feature to get aquicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version:5.6.22-enterprise-commercial-advanced MySQL Enterprise Server - AdvancedEdition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.

(4) 输入修改root密码SQL语句

mysql> UPDATE user SET Password=PASSWORD('root123') where USER='root';

Query OK, 4 rows affected (0.04 sec)

Rows matched: 4 Changed: 4 Warnings: 0

(5) 输入数据刷新命令

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

(6) 退出

mysql> quit

Bye

(7) 启动MYSQL

[root@bogon 桌面]# service mysql start

Starting MySQL SUCCESS!

登录mysql,查看所有数据库:

[root@bogon 桌面]# mysql -u root -p

mysql> show databases;

ERROR 1820 (HY000):You must SET PASSWORD before executing this statement

提示要再设置一下密码:

mysql> SET PASSWORD = PASSWORD('root123');

Query OK, 0 rows affected (0.00 sec)

显示数据库:

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

4 rows in set (0.00 sec)

进入数据库创建表、显示表:

mysql> use test;

Database changed

mysql> show tables;

Empty set (0.02 sec)

mysql>create table testTable(name char(15) not null,passwd char(15) not null);

Query OK, 0 rows affected (0.87 sec)

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| testTable |

+----------------+

1 row in set (0.00 sec)

备注:我的设置的密码是“root123”。大家在执行showdatabases收到错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement后也可以试试以下面方式登录mysql来执行showdatabases是否就不会有这个错误,我没有条件测试了:

登录MySQL界面:mysql –uroot -p(修改的新密码)

例如:mysql -u root -p123456

mysql安装后三个主要的目录及其功能:查找的话用where is mysql

/var/lib/mysql 数据库文件

/usr/share/mysql 命令及配置文件

/usr/bin mysqladmin、mysqldump等命令

查看mysql配置文件

1.vi/etc/init.d/mysql

conf=/etc/my.cnf

print_defaults=

conf就是查找配置文件的路径

2.mysql --verbose --help | grep -A 1 'Default options'

Default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

上面两种方法都可以,然后查看所在目录是否有my.cnf,如果没有,拷贝一份过去


主从同步配置:(linux主库和window从库)

主(linux):配置linux下/etc/my.cnf,添加如下字段

Server-id = 1  #这是数据库ID,此ID是唯一的,主库默认为1,其他从库以此ID进行递增,ID值不能重复,必须为232–1之间的一个正整数值

log-bin = mysql-bin  #这个一定得设置,否则没有日志的话,从数据库上会报错

replicate-wild-do-table = test.% #需要同步的数据库,如果需要同步多个数据库;(binlog-do-db = test,这样同步有问题,具体自己百度

启动数据库:

service mysql start

mysql>start master;

mysql>show master status\G;


从(window):mysql文件下my.ini,添加如下字段

Server-id = 2 #从库id,可以为别的,不能与主库相同

启动数据库:

net start mysql 

mysql>change master to

>master_host='192.168.124.51',#主库ip地址

>master_user='AffairLog',   #主库用户名

>master_password='password';#主库密码

mysql>start slave;


mysql>show slave status\G;


下面两个为yes就为成功

Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件

Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。


 双向同步:

 从库加入如下参数,跟主库配置相同

log-bin = mysql-bin  #

log-bin = mysql-bin  #这个一定得设置,否则没有日志的话,从数据库上会报错

replicate-wild-do-table = test.name#如果需要同步表的话,添加这句,需要同步的数据库,将表名改为%;

添加完毕后,启动从库:

net start mysql,同时输入下面两句话

mysql>start slave;

主库需要配置从库ip等

mysql>change master to

>master_host='192.168.124.51', #从库ip

>master_user='AffairLog',    #从库用户名

>master_password='password';   #从库密码

> start slave

>start master

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值