标签 mysql 下的文章

先停止mysql服务;

进入mysql安装目录下的bin目录,打开cmd执行以下命令以安全模式运行:

mysqld --skip-grant-tables

另打开一个新的CMD窗口,执行以下命令可直接进入:

mysql.exe -uroot -p

然后修改用户密码(123456):

update user set authentication_string=password("123456") where user="root";
flush privileges;

然后退出,再关掉前面打开的安全模式服务窗口,再启动mysql服务器,这时候用123456已经可以登录了,不过用navicat premium工具登录之后会提示重新设置密码(测试的时候用其他工具链接提示错误消息进不去,navicat premium可正常连接并修改密码),123456 这个密码不能用了

create database 指定utf-8编码

如下脚本创建数据库yourdbname,并制定默认的字符集是utf8。

CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

如果要创建默认gbk字符集的数据库可以用下面的sql:

create database yourdb DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

查询正则

select * from tablename where column REGEXP "^B[an]*s$"

字符串查找函数

LOCATE(substr,str)    substr在str中出现的位置,从1开始,没有返回0
POSITION(substr IN str)    功能一样 
INSTR(`subject`,'Welcome to')
select "Ban" REGEXP "^Ba*n"  正则匹配,符合时返回1,不符合时返回0
http://www.111cn.net/database/mysql/61898.htm  正规匹配的文章

匹配.字符本身,需要前面加两个\
SELECT 'http://sdfg7808.cn' REGEXP "http://(.+\.)*7808\.(cn|com)"; 匹配一个域名

字符串替换

replace(object,search,replace) 把object中出现search的全部替换为replace

设置自增ID

alter table users AUTO_INCREMENT=123456;

清空某个表中所有内容(数据量大不建议这样做,要分批删除数据免被卡死数据库)

delete from 表名;
truncate table 表名; 
不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以清空mysql表中所有内容。效率上truncate比delete快,但truncate删除后不记录mysql日志,不可以恢复数据。
delete的效果有点像将mysql表中所有记录一条一条删除到删完,而truncate相当于保留mysql表的结构,重新创建了这个表,所有的状态都相当于新表。

时间转换工具
http://tool.chinaz.com/Tools/unixtime.aspx

MySQL时间戳
SELECT unix_timestamp(time)
时间格式: YYYY-MM-DD HH:MM:SS 或 YYMMDD 或 YYYYMMDD

=====MYSQL 替换操作
将cdb_pms表subject字段中的Welcom to替换成 欢迎光临 
复制代码代码如下:

UPDATE `cdb_pms` 
SET `subject` = REPLACE(`subject`, 'Welcome to', '欢迎光临') 
WHERE INSTR(`subject`,'Welcome to') > 0 

替换cdb_posts表的message字段,将“viewthread.php?tid=3989”替换成“viewthread.php?tid=16546” 
复制代码代码如下:

UPDATE `cdb_posts` 
SET `message`= REPLACE(`message`, 'viewthread.php?tid=3989', 'viewthread.php?tid=16546') 
WHERE INSTR(`message`,'viewthread.php?tid=3989') > 0 ; 

删除所有的空格 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = TRIM(`pro_pub_time`) 

删除所有饱含'['或者']'或者'.'的字符 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, '[','') WHERE INSTR(`pro_pub_time`,'[') > 0 
UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, ']','') WHERE INSTR(`pro_pub_time`,']') > 0 
UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, '.','-') WHERE INSTR(`pro_pub_time`,'.') > 0 

替换所有含中文'-'的为英文'-' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, '-','-') WHERE INSTR(`pro_pub_time`,'-') > 0 

将所有的年月都替换成'-' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, '年','-') WHERE INSTR(`pro_pub_time`,'年') > 0 
UPDATE `es_product` SET `pro_pub_time` = REPLACE(`pro_pub_time`, '月','-') WHERE INSTR(`pro_pub_time`,'月') > 0 

将所有'2005-04-'这种类型的替换成'2005-04-01' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = CONCAT( `pro_pub_time`, '01') WHERE SUBSTRING_INDEX( `pro_pub_time`, '-', -1) = '' AND LENGTH(`pro_pub_time`) > 0 AND LENGTH(`pro_pub_time`) > 5 

将所有'2005-'这种类型替换成'2005-01-01' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = CONCAT( `pro_pub_time`, '01-01') WHERE INSTR(`pro_pub_time`,'-') > 0 AND LENGTH(`pro_pub_time`) = 5 

将所有 饱含'-',但是位数小于8的改成追加'-01' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = CONCAT( `pro_pub_time`, '-01') WHERE INSTR(`pro_pub_time`,'-') > 0 AND LENGTH(`pro_pub_time`) < 8 

将所有'2005'这样的改成'2005-01-01' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = CONCAT(`pro_pub_time`,'-01-01') WHERE INSTR(`pro_pub_time`,'-') = 0 AND LENGTH(`pro_pub_time`) = 4 

最后将所有'2005-01-01'格式化成'2005年01月' 
复制代码代码如下:

UPDATE `es_product` SET `pro_pub_time` = DATE_FORMAT(`pro_pub_time`,'%Y年%m月') WHERE INSTR(`pro_pub_time`,'-') > 0 

一, 创建用户: 

命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 

说明:username - 你将创建的用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器. 

例子: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456'; 
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456'; 
CREATE USER 'pig'@'%' IDENTIFIED BY '123456'; 
CREATE USER 'pig'@'%' IDENTIFIED BY ''; 
CREATE USER 'pig'@'%'; 

二,授权: 

命令:GRANT privileges ON databasename.tablename TO 'username'@'host' 

说明: privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename - 数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*. 

例子: GRANT SELECT, INSERT ON test.user TO 'pig'@'%'; 
GRANT ALL ON *.* TO 'pig'@'%'; 

注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令: 
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION; 

三.设置与更改用户密码 

命令:SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');如果是当前登陆用户用SET PASSWORD = PASSWORD("newpassword"); 

例子: SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456"); 

四.撤销用户权限 

命令: REVOKE privilege ON databasename.tablename FROM 'username'@'host'; 

说明: privilege, databasename, tablename - 同授权部分. 

例子: REVOKE SELECT ON *.* FROM 'pig'@'%'; 

注意: 假如你在给用户'pig'@'%'授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'pig'@'%', 则在使用REVOKE SELECT ON *.* FROM 'pig'@'%';命令并不能撤销该用户对test数据库中user表的SELECT 操作.相反,如果授权使用的是GRANT SELECT ON *.* TO 'pig'@'%';则REVOKE SELECT ON test.user FROM 'pig'@'%';命令也不能撤销该用户对test数据库中user表的Select 权限. 

具体信息可以用命令SHOW GRANTS FOR 'pig'@'%'; 查看. 

五.删除用户 

命令: DROP USER 'username'@'host'; 

附表:在MySQL中的操作权限 

ALTER Allows use of ALTER TABLE.
ALTER ROUTINE Alters or drops stored routines.
CREATE Allows use of CREATE TABLE.
CREATE ROUTINE Creates stored routines.
CREATE TEMPORARY TABLE Allows use of CREATE TEMPORARY TABLE.
CREATE USER Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW Allows use of CREATE VIEW.
DELETE Allows use of DELETE.
DROP Allows use of DROP TABLE.
EXECUTE Allows the user to run stored routines.
FILE Allows use of SELECT... INTO OUTFILE and LOAD DATA INFILE.
INDEX Allows use of CREATE INDEX and DROP INDEX.
INSERT Allows use of INSERT.
LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
PROCESS Allows use of SHOW FULL PROCESSLIST.
RELOAD Allows use of FLUSH.
REPLICATION Allows the user to ask where slave or master
CLIENT servers are.
REPLICATION SLAVE Needed for replication slaves.
SELECT Allows use of SELECT.
SHOW DATABASES Allows use of SHOW DATABASES.
SHOW VIEW Allows use of SHOW CREATE VIEW.
SHUTDOWN Allows use of mysqladmin shutdown.
SUPER Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
UPDATE Allows use of UPDATE.
USAGE Allows connection without any specific privileges.

允许远程连接:
grant all PRIVILEGES on discuz.* to 'ted'@'123.123.123.123' identified by '123456';
上面的语句表示将 discuz 数据库的所有权限授权给 ted 这个用户,允许 ted 用户在 123.123.123.123 这个 IP 进行远程登陆,并设置 ted 用户的密码为 123456 。
下面逐一分析所有的参数:
all PRIVILEGES 表示赋予所有的权限给指定用户,这里也可以替换为赋予某一具体的权限,例如:select,insert,update,delete,create,drop 等,具体权限间用“,”半角逗号分隔。
discuz.* 表示上面的权限是针对于哪个表的,discuz 指的是数据库,后面的 * 表示对于所有的表,由此可以推理出:对于全部数据库的全部表授权为“*.*”,对于某一数据库的全部表授权为“数据库名.*”,对于某一数据库的某一表授 权为“数据库名.表名”。
ted 表示你要给哪个用户授权,这个用户可以是存在的用户,也可以是不存在的用户。
123.123.123.123 表示允许远程连接的 IP 地址,如果想不限制链接的 IP 则设置为“%”即可。
123456 为用户的密码。
执行了上面的语句后,再执行下面的语句,方可立即生效。
CODE: [COPY]
> flush privileges;


desc 表名 ; //显示表结构
show columns from 表名; //表栏目
describe 表名; //显示表结构
show create table 表名; //显示创建表的语句

show index from 表名; //显示表的索引信息
show keys from 表名; //同上
索引说明:
    · Table 表的名称。
    · Non_unique 如果索引不能包括重复词,则为0。如果可以,则为1。
    · Key_name  索引的名称。
    · Seq_in_index  索引中的列序列号,从1开始。
    · Column_name  列名称。
    · Collation  列以什么方式存储在索引中。在MySQL中,有值‘A’(升序)或NULL(无分类)。
    · Cardinality   索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,MySQL使用该索引的机 会就越大。
    · Sub_part    如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL。
    · Packed    指示关键字如何被压缩。如果没有被压缩,则为NULL。
    · Null    如果列含有NULL,则含有YES。如果没有,则该列含有NO。
    · Index_type    用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
    · Comment 

use information_schema; //切换到某库,这里是一个系统库
select * from columns where table_name='表名'; //这个库里保存的有表的数据,这里是查询一个表的栏目
show databases; //显示数据库列表
use 数据库名; //切换到某数据库
show tables; //显示数据表

有这样的问题,在与linux平台有数据导入导出的时候,非常麻烦了,怎么解决呢

停掉数据库,在my.ini配置文件里,找到 [mysqld] 配置那一行,看看下面的有没有这一项:

lower_case_table_names=0

需要改成:

lower_case_table_names=2

没有的自己可以加上这一行配置,然后重启数据库,然后导入数据就可以大小写了

PS : 我是先按某些网友说的改成0,重启发现启动不了数据库了,2可以。你可以试试

附:
1、参数含义:
lower_case_table_names: 此参数不可以动态修改,必须重启数据库
lower_case_table_names = 1 表名存储在磁盘是小写的,但是比较的时候是不区分大小写
lower_case_table_names=0 表名存储为给定的大小和比较是区分大小写的
lower_case_table_names=2, 表名存储为给定的大小写但是比较的时候是小写的

crontab -e

0 5 * * *  /usr/bin/mysqldump -uxxxxxx -pxxxxxx --databases techan > /data/mysqlbackup/techan-`date +"%Y%m%d"`.sql && /usr/bin/zip /data/mysqlbackup/techan-`date +"%Y%m%d"`.zip /data/mysqlbackup/techan-`date +"%Y%m%d"`.sql

xxxxxx替换成你的数据库账号密码

-date +"%Y%m%d"的方法一定很笨,望指点!

MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解

Percona XtraDB Cluster简称PXC。Percona Xtradb Cluster的实现是在原mysql代码上通过Galera包将不同的mysql实例连接起来,实现了multi-master的集群架构。
下图中有三个实例,组成了一个集群,而这三个节点与普通的主从架构不同,它们都可以作为主节点,三个节点是对等的,这种一般称为multi-master架构,当有客户端要写入或者读取数据时,随便连接哪个实例都是一样的,读到的数据是相同的,写入某一个节点之后,集群自己会将新数据同步到其它节点上面,这种架构不共享任何数据,是一种高冗余架构。

技术分享
此方案无法解决较大MySQL数据场景的数据保存问题,即不能实现分库分表,但是提供了一个高冗余的环境,适合于业务不是很大但是要求数据绝对安全的环境。

Percona XtraDBCluster提供的特性有:

1.同步复制,事务要么在所有节点提交或不提交。
2.多主复制,可以在任意节点进行写操作。
3.在从服务器上并行应用事件,真正意义上的并行复制。
4.节点自动配置。
5.数据一致性,不再是异步复制。

Percona XtraDBCluster完全兼容MySQL和Percona Server,表现在:
1.数据的兼容性
2.应用程序的兼容性:无需更改应用程序

集群特点:
a.集群是有节点组成的,推荐配置至少3个节点,但是也可以运行在2个节点上。
b.每个节点都是普通的mysql/percona服务器,可以将现有的数据库服务器组成集群,反之,也可以将集群拆分成单独的服务器。
c.每个节点都包含完整的数据副本。

优点如下:

1.当执行一个查询时,在本地节点上执行。因为所有数据都在本地,无需远程访问。
2.无需集中管理。可以在任何时间点失去任何节点,但是集群将照常工作。
3.良好的读负载扩展,任意节点都可以查询。
缺点如下:
1.加入新节点,开销大。需要复制完整的数据。
2.不能有效的解决写缩放问题,所有的写操作都将发生在所有节点上。
3.有多少个节点就有多少重复的数据。

Percona XtraDB Cluster与MySQL Replication区别在于:
分布式系统的CAP理论:
C— 一致性,所有节点的数据一致;
A— 可用性,一个或多个节点失效,不影响服务请求;
P— 分区容忍性,节点间的连接失效,仍然可以处理请求;
任何一个分布式系统,需要满足这三个中的两个。

MySQLReplication: 可用性和分区容忍性;
Percona XtraDBCluster: 一致性和可用性。
因此MySQL Replication并不保证数据的一致性,而Percona XtraDB Cluster提供数据一致性

下面,开始安装PXC 5.7.18

一、环境说明

技术分享

IP地址 角色 系统 主机名
192.168.3.12 pxc01 CentOS6.5 master
192.168.3.13 pxc02 CentOS6.5 slave01
192.168.3.198 pxc03 CentOS6.5 slave02

二、安装 Percona-XtraDB-Cluster

1、下载依赖包percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm到各节点
下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/
安装:

yum localinstall -y percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm

2、下载Percona-XtraDB-Cluster并上传到各节点
下载地址:https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/

安装包列表:
Percona-XtraDB-Cluster-57-5.7.18-29.20.1.el6.x86_64.rpm
Percona-XtraDB-Cluster-client-57-5.7.18-29.20.1.el6.x86_64.rpm
Percona-XtraDB-Cluster-server-57-5.7.18-29.20.1.el6.x86_64.rpm
Percona-XtraDB-Cluster-shared-57-5.7.18-29.20.1.el6.x86_64.rpm

yum localinstall自动解决依赖关系并进行安装

yum localinstall Percona-XtraDB-Cluster-*.rpm

3、所有节点创建mysql组和用户

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

4、创建相关目录

mkdir /data/mysql/{data,logs,tmp} -p

chown -R mysql.mysql /data

5、配置my.cnf文件
将/etc/percona-xtradb-cluster.conf.d/wsrep.cnf拷贝到/etc/my.cnf

cp /etc/percona-xtradb-cluster.conf.d/wsrep.cnf /etc/my.cnf

编辑my.cnf
第一节点 pxc01

[mysqld]
user=mysql
innodb_buffer_pool_size = 1024M
datadir = /data/mysql/data
port = 3306
server_id = 12
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/logs/mysql.pid
log-error = /data/mysql/logs/error.log
log_warnings = 2
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 0.1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Path to Galera library

wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

Cluster connection URL contains IPs of nodes

If no IP is found, this implies that a new cluster needs to be created,

in order to do that you need to bootstrap this node

wsrep_cluster_address=gcomm://192.168.3.12,192.168.3.13,192.168.3.198

In order for Galera to work correctly binlog format should be ROW

binlog_format=ROW

MyISAM storage engine has only experimental support

default_storage_engine=InnoDB

Slave thread to use

wsrep_slave_threads= 8

wsrep_log_conflicts

This changes how InnoDB autoincrement locks are managed and is a requirement for Galera

innodb_autoinc_lock_mode=2

Node IP address

wsrep_node_address=192.168.3.12

Cluster name

wsrep_cluster_name=my-pxc-cluster

If wsrep_node_name is not specified, then system hostname will be used

wsrep_node_name=pxc01

pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER

pxc_strict_mode=ENFORCING

SST method

wsrep_sst_method=xtrabackup-v2

Authentication for SST method

wsrep_sst_auth="sstuser:sstuser"

第二节点 pxc02
注意修改 server_id 、wsrep_node_name 、 wsrep_node_address

[mysqld]
user=mysql
innodb_buffer_pool_size = 1024M
datadir = /data/mysql/data
port = 3306
server_id = 13
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/logs/mysql.pid
log-error = /data/mysql/logs/error.log
log_warnings = 2
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 0.1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Path to Galera library

wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

Cluster connection URL contains IPs of nodes

If no IP is found, this implies that a new cluster needs to be created,

in order to do that you need to bootstrap this node

wsrep_cluster_address=gcomm://192.168.3.12,192.168.3.13,192.168.3.198

In order for Galera to work correctly binlog format should be ROW

binlog_format=ROW

MyISAM storage engine has only experimental support

default_storage_engine=InnoDB

Slave thread to use

wsrep_slave_threads= 8

wsrep_log_conflicts

This changes how InnoDB autoincrement locks are managed and is a requirement for Galera

innodb_autoinc_lock_mode=2

Node IP address

wsrep_node_address=192.168.3.13

Cluster name

wsrep_cluster_name=my-pxc-cluster

If wsrep_node_name is not specified, then system hostname will be used

wsrep_node_name=pxc02

pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER

pxc_strict_mode=ENFORCING

SST method

wsrep_sst_method=xtrabackup-v2

Authentication for SST method

wsrep_sst_auth="sstuser:sstuser"

第三节点 pxc03
注意修改 server_id 、wsrep_node_name 、 wsrep_node_address

[mysqld]
user=mysql
innodb_buffer_pool_size = 1024M
datadir = /data/mysql/data
port = 3306
server_id = 198
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/logs/mysql.pid
log-error = /data/mysql/logs/error.log
log_warnings = 2
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 0.1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Path to Galera library

wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

Cluster connection URL contains IPs of nodes

If no IP is found, this implies that a new cluster needs to be created,

in order to do that you need to bootstrap this node

wsrep_cluster_address=gcomm://192.168.3.12,192.168.3.13,192.168.3.198

In order for Galera to work correctly binlog format should be ROW

binlog_format=ROW

MyISAM storage engine has only experimental support

default_storage_engine=InnoDB

Slave thread to use

wsrep_slave_threads= 8

wsrep_log_conflicts

This changes how InnoDB autoincrement locks are managed and is a requirement for Galera

innodb_autoinc_lock_mode=2

Node IP address

wsrep_node_address=192.168.3.198

Cluster name

wsrep_cluster_name=my-pxc-cluster

If wsrep_node_name is not specified, then system hostname will be used

wsrep_node_name=pxc03

pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER

pxc_strict_mode=ENFORCING

SST method

wsrep_sst_method=xtrabackup-v2

Authentication for SST method

wsrep_sst_auth="sstuser:sstuser

三、启动PXC

1、启动第一节点

[root@master ~]# /etc/init.d/mysql bootstrap-pxc
Bootstrapping PXC (Percona XtraDB Cluster)Initializing MySQ[ OK ]se:
Starting MySQL (Percona XtraDB Cluster)....................[ OK ]

查看进程和端口是否启动

[root@master ~]# ps -ef|grep mysql
root 27910 1 0 10:21 pts/2 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/logs/mysql.pid --wsrep-new-cluster
mysql 28347 27910 22 10:21 pts/2 00:00:23 /usr/sbin/mysqld --basedir=/usr --datadir=/data/mysql/data --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --wsrep-provider=/usr/lib64/galera3/libgalera_smm.so --wsrep-new-cluster --log-error=/data/mysql/logs/error.log --pid-file=/data/mysql/logs/mysql.pid --socket=/data/mysql/mysql.sock --port=3306 --wsrep_start_position=00000000-0000-0000-0000-000000000000:-1

[root@master mysql]# ss -tnlp|grep 4567
LISTEN 0 128 :4567 :* users:(("mysqld",28347,11))

查看错误日志信息

[root@master mysql]# tail -f logs/error.log

2017-06-07T02:21:30.794514Z 0 [Note] WSREP: Node 04b5f0f9 state primary
2017-06-07T02:21:30.794674Z 0 [Note] WSREP: Current view of cluster as seen by this node
view (view_id(PRIM,04b5f0f9,1)
memb {
04b5f0f9,0
}
joined {
}
left {
}
partitioned {
}
)
2017-06-07T02:21:30.794745Z 0 [Note] WSREP: Save the discovered primary-component to disk
2017-06-07T02:21:30.794882Z 0 [Note] WSREP: discarding pending addr without UUID: tcp://192.168.3.12:4567
2017-06-07T02:21:30.794926Z 0 [Note] WSREP: discarding pending addr proto entry 0x7f16aefcfcc0
2017-06-07T02:21:30.794988Z 0 [Note] WSREP: discarding pending addr without UUID: tcp://192.168.3.13:4567
2017-06-07T02:21:30.795028Z 0 [Note] WSREP: discarding pending addr proto entry 0x7f16aefcff00
2017-06-07T02:21:30.795060Z 0 [Note] WSREP: discarding pending addr without UUID: tcp://192.168.3.198:4567
2017-06-07T02:21:30.795098Z 0 [Note] WSREP: discarding pending addr proto entry 0x7f16aefcffc0
2017-06-07T02:21:30.795158Z 0 [Note] WSREP: gcomm: connected
2017-06-07T02:21:30.795328Z 0 [Note] WSREP: Shifting CLOSED -> OPEN (TO: 0)
2017-06-07T02:21:30.797700Z 0 [Note] WSREP: Waiting for SST/IST to complete.
2017-06-07T02:21:31.391420Z 0 [Note] WSREP: New COMPONENT: primary = yes, bootstrap = no, my_idx = 0, memb_num = 1
2017-06-07T02:21:32.020639Z 0 [Note] WSREP: Starting new group from scratch: 05719dfe-4b28-11e7-a651-ab4ab48f9b60
2017-06-07T02:21:32.021473Z 0 [Note] WSREP: STATE_EXCHANGE: sent state UUID: 0571c6e9-4b28-11e7-b20d-5a8bf004e006
2017-06-07T02:21:32.021523Z 0 [Note] WSREP: STATE EXCHANGE: sent state msg: 0571c6e9-4b28-11e7-b20d-5a8bf004e006
2017-06-07T02:21:32.021547Z 0 [Note] WSREP: STATE EXCHANGE: got state msg: 0571c6e9-4b28-11e7-b20d-5a8bf004e006 from 0 (pxc01)
2017-06-07T02:21:32.021571Z 0 [Note] WSREP: Quorum results:
version = 4,
component = PRIMARY,
conf_id = 0,
members = 1/1 (primary/total),
act_id = 0,
last_appl. = -1,
protocols = 0/7/3 (gcs/repl/appl),
group UUID = 05719dfe-4b28-11e7-a651-ab4ab48f9b60
2017-06-07T02:21:32.021599Z 0 [Note] WSREP: Flow-control interval: [100, 100]
2017-06-07T02:21:32.021615Z 0 [Note] WSREP: Restored state OPEN -> JOINED (0)
2017-06-07T02:21:32.021976Z 0 [Note] WSREP: Member 0.0 (pxc01) synced with group.
2017-06-07T02:21:32.022019Z 0 [Note] WSREP: Shifting JOINED -> SYNCED (TO: 0)
2017-06-07T02:21:32.022101Z 2 [Note] WSREP: New cluster view: global state: 05719dfe-4b28-11e7-a651-ab4ab48f9b60:0, view# 1: Primary, number of nodes: 1, my index: 0, protocol version 3
2017-06-07T02:21:32.023374Z 0 [Note] WSREP: SST complete, seqno: 0
2017-06-07T02:21:32.535748Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-06-07T02:21:32.535815Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-06-07T02:21:32.535826Z 0 [Note] InnoDB: Uses event mutexes
2017-06-07T02:21:32.535832Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
2017-06-07T02:21:32.535837Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-06-07T02:21:32.535842Z 0 [Note] InnoDB: Using Linux native AIO
2017-06-07T02:21:32.536865Z 0 [Note] InnoDB: Number of pools: 1
2017-06-07T02:21:32.536999Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-06-07T02:21:33.194918Z 0 [Note] InnoDB: Initializing buffer pool, total size = 1G, instances = 8, chunk size = 128M
2017-06-07T02:21:41.225447Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-06-07T02:21:45.228192Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2017-06-07T02:21:45.554968Z 0 [Note] InnoDB: Crash recovery did not find the parallel doublewrite buffer at /data/mysql/data/xb_doublewrite
2017-06-07T02:21:45.556158Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2017-06-07T02:21:47.397187Z 0 [Note] InnoDB: Created parallel doublewrite buffer at /data/mysql/data/xb_doublewrite, size 31457280 bytes
2017-06-07T02:21:47.700377Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2017-06-07T02:21:47.700541Z 0 [Note] InnoDB: Setting file ‘./ibtmp1‘ size to 12 MB. Physically writing the file full; Please wait ...
2017-06-07T02:21:48.413452Z 0 [Note] InnoDB: File ‘./ibtmp1‘ size is now 12 MB.
2017-06-07T02:21:48.414626Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2017-06-07T02:21:48.414664Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-06-07T02:21:48.416853Z 0 [Note] InnoDB: Waiting for purge to start
2017-06-07T02:21:48.468115Z 0 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.7.18-15 started; log sequence number 2542677
2017-06-07T02:21:48.468456Z 0 [Note] Plugin ‘FEDERATED‘ is disabled.
2017-06-07T02:21:48.468710Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/data/ib_buffer_pool
2017-06-07T02:21:48.472685Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170607 10:21:48
2017-06-07T02:21:48.477798Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2017-06-07T02:21:48.477886Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2017-06-07T02:21:48.515627Z 0 [Warning] CA certificate ca.pem is self signed.
2017-06-07T02:21:48.544887Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2017-06-07T02:21:48.545210Z 0 [Note] Server hostname (bind-address): ‘*‘; port: 3306
2017-06-07T02:21:48.545499Z 0 [Note] IPv6 is available.
2017-06-07T02:21:48.545557Z 0 [Note] - ‘::‘ resolves to ‘::‘;
2017-06-07T02:21:48.545717Z 0 [Note] Server socket created on IP: ‘::‘.
2017-06-07T02:21:48.611502Z 0 [Note] Event Scheduler: Loaded 0 events
2017-06-07T02:21:48.651338Z 2 [Note] WSREP: Initialized wsrep sidno 2
2017-06-07T02:21:48.651403Z 2 [Note] WSREP: wsrep_notify_cmd is not defined, skipping notification.
2017-06-07T02:21:48.651443Z 2 [Note] WSREP: REPL Protocols: 7 (3, 2)
2017-06-07T02:21:48.651455Z 2 [Note] WSREP: Assign initial position for certification: 0, protocol version: 3
2017-06-07T02:21:48.651862Z 0 [Note] WSREP: Service thread queue flushed.
2017-06-07T02:21:48.765979Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘5.7.18-15-57‘ socket: ‘/data/mysql/mysql.sock‘ port: 3306 Percona XtraDB Cluster (GPL), Release rel15, Revision 7693d6e, WSREP version 29.20, wsrep_29.20
2017-06-07T02:21:48.766032Z 0 [Note] Executing ‘SELECT * FROM INFORMATION_SCHEMA.TABLES;‘ to get a list of tables using the deprecated partition engine. You may use the startup option ‘--disable-partition-engine-check‘ to skip this check.
2017-06-07T02:21:48.766043Z 0 [Note] Beginning of list of non-natively partitioned tables
2017-06-07T02:21:48.766034Z 2 [Note] WSREP: GCache history reset: old(00000000-0000-0000-0000-000000000000:0) -> new(05719dfe-4b28-11e7-a651-ab4ab48f9b60:0)
2017-06-07T02:21:48.767985Z 2 [Note] WSREP: Synchronized with group, ready for connections
2017-06-07T02:21:48.768012Z 2 [Note] WSREP: wsrep_notify_cmd is not defined, skipping notification.
2017-06-07T02:21:48.824186Z 0 [Note] End of list of non-natively partitioned tables

去error.log日志搜索关键字password找到默认root密码,登录后修改密码(如果不修改密码无法进行其他sql操作)
2017-06-07T02:21:11.228127Z 1 [Note] A temporary password is generated for root@localhost: eu1-!5WyRw<7

修改密码:

mysql -S /data/mysql/mysql.sock -uroot -peu1-!5WyRw<7

mysql> alter user ‘root‘@‘localhost‘ identified by ‘123456‘;

第一节点启动后,检查cluster的状态。
当前第一节点是Primary。

mysql> show status like ‘%wsrep%‘;
Variable_nameValue
wsrep_local_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_protocol_version7
wsrep_last_committed1
wsrep_replicated1
wsrep_replicated_bytes230
wsrep_repl_keys1
wsrep_repl_keys_bytes31
wsrep_repl_data_bytes135
wsrep_repl_other_bytes0
wsrep_received2
wsrep_received_bytes141
wsrep_local_commits0
wsrep_local_cert_failures0
wsrep_local_replays0
wsrep_local_send_queue0
wsrep_local_send_queue_max1
wsrep_local_send_queue_min0
wsrep_local_send_queue_avg0.000000
wsrep_local_recv_queue0
wsrep_local_recv_queue_max1
wsrep_local_recv_queue_min0
wsrep_local_recv_queue_avg0.000000
wsrep_local_cached_downto1
wsrep_flow_control_paused_ns0
wsrep_flow_control_paused0.000000
wsrep_flow_control_sent0
wsrep_flow_control_recv0
wsrep_flow_control_interval[ 100, 100 ]
wsrep_flow_control_interval_low100
wsrep_flow_control_interval_high100
wsrep_flow_control_statusOFF
wsrep_cert_deps_distance1.000000
wsrep_apply_oooe0.000000
wsrep_apply_oool0.000000
wsrep_apply_window1.000000
wsrep_commit_oooe0.000000
wsrep_commit_oool0.000000
wsrep_commit_window1.000000
wsrep_local_state4
wsrep_local_state_commentSynced
wsrep_cert_index_size1
wsrep_cert_bucket_count22
wsrep_gcache_pool_size1590
wsrep_causal_reads0
wsrep_cert_interval0.000000
wsrep_ist_receive_status
wsrep_ist_receive_seqno_start0
wsrep_ist_receive_seqno_current0
wsrep_ist_receive_seqno_end0
wsrep_incoming_addresses192.168.3.12:3306
wsrep_desync_count0
wsrep_evs_delayed
wsrep_evs_evict_list
wsrep_evs_repl_latency1.8541e-05/1.8541e-05/1.8541e-05/0/1
wsrep_evs_stateOPERATIONAL
wsrep_gcomm_uuid04b5f0f9-4b28-11e7-aff6-522901b34a55
wsrep_cluster_conf_id1
wsrep_cluster_size1
wsrep_cluster_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_cluster_statusPrimary
wsrep_connectedON
wsrep_local_bf_aborts0
wsrep_local_index0
wsrep_provider_nameGalera
wsrep_provider_vendorCodership Oy <info@codership.com>
wsrep_provider_version3.20(r7e383f7)
wsrep_readyON

第一节点创建后,其他节点初始化,需要使用xtrabackup工具来备份,然后恢复。
所以,需要创建一个用户用于备份。

mysql> create user ‘sstuser‘@‘localhost‘ identified by ‘sstuser‘;
mysql> grant reload,lock tables,replication client,process on . to ‘sstuser‘@‘localhost‘;
mysql> flush privileges;
验证sstuser登录

[root@master ~]# mysql -usstuser -psstuser -S /data/mysql/mysql.sock -e ‘show databases‘

Warning: Using a password on the command line interface can be insecure.
Database
information_schema
mysql
performance_schema
sys

启动第二节点

/etc/init.d/mysql start

确认第二节点的状态

mysql -usstuser -psstuser -S /data/mysql/mysql.sock -e ‘show status like "wsrep%"‘

Warning: Using a password on the command line interface can be insecure.
Variable_nameValue
wsrep_local_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_protocol_version7
wsrep_last_committed4
wsrep_replicated4
wsrep_replicated_bytes950
wsrep_repl_keys4
wsrep_repl_keys_bytes124
wsrep_repl_data_bytes570
wsrep_repl_other_bytes0
wsrep_received6
wsrep_received_bytes423
wsrep_local_commits0
wsrep_local_cert_failures0
wsrep_local_replays0
wsrep_local_send_queue0
wsrep_local_send_queue_max1
wsrep_local_send_queue_min0
wsrep_local_send_queue_avg0.000000
wsrep_local_recv_queue0
wsrep_local_recv_queue_max1
wsrep_local_recv_queue_min0
wsrep_local_recv_queue_avg0.000000
wsrep_local_cached_downto1
wsrep_flow_control_paused_ns0
wsrep_flow_control_paused0.000000
wsrep_flow_control_sent0
wsrep_flow_control_recv0
wsrep_flow_control_interval[ 141, 141 ]
wsrep_flow_control_interval_low141
wsrep_flow_control_interval_high141
wsrep_flow_control_statusOFF
wsrep_cert_deps_distance1.000000
wsrep_apply_oooe0.000000
wsrep_apply_oool0.000000
wsrep_apply_window1.000000
wsrep_commit_oooe0.000000
wsrep_commit_oool0.000000
wsrep_commit_window1.000000
wsrep_local_state4
wsrep_local_state_commentSynced
wsrep_cert_index_size1
wsrep_cert_bucket_count22
wsrep_gcache_pool_size2561
wsrep_causal_reads0
wsrep_cert_interval0.000000
wsrep_ist_receive_status
wsrep_ist_receive_seqno_start0
wsrep_ist_receive_seqno_current0
wsrep_ist_receive_seqno_end0
wsrep_incoming_addresses192.168.3.12:3306,192.168.3.13:3306
wsrep_desync_count0
wsrep_evs_delayed
wsrep_evs_evict_list
wsrep_evs_repl_latency0/0/0/0/0
wsrep_evs_stateOPERATIONAL
wsrep_gcomm_uuid04b5f0f9-4b28-11e7-aff6-522901b34a55
wsrep_cluster_conf_id2
wsrep_cluster_size2
wsrep_cluster_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_cluster_statusPrimary
wsrep_connectedON
wsrep_local_bf_aborts0
wsrep_local_index0
wsrep_provider_nameGalera
wsrep_provider_vendorCodership Oy <info@codership.com>
wsrep_provider_version3.20(r7e383f7)
wsrep_readyON

启动第三节点

/etc/init.d/mysql start

确定状态

[root@slave02 ~]# mysql -S /data/mysql/mysql.sock -usstuser -psstuser -e ‘show status like "wsrep%"‘;

Warning: Using a password on the command line interface can be insecure.
Variable_nameValue
wsrep_local_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_protocol_version7
wsrep_last_committed4
wsrep_replicated0
wsrep_replicated_bytes0
wsrep_repl_keys0
wsrep_repl_keys_bytes0
wsrep_repl_data_bytes0
wsrep_repl_other_bytes0
wsrep_received3
wsrep_received_bytes288
wsrep_local_commits0
wsrep_local_cert_failures0
wsrep_local_replays0
wsrep_local_send_queue0
wsrep_local_send_queue_max1
wsrep_local_send_queue_min0
wsrep_local_send_queue_avg0.000000
wsrep_local_recv_queue0
wsrep_local_recv_queue_max1
wsrep_local_recv_queue_min0
wsrep_local_recv_queue_avg0.000000
wsrep_local_cached_downto0
wsrep_flow_control_paused_ns0
wsrep_flow_control_paused0.000000
wsrep_flow_control_sent0
wsrep_flow_control_recv0
wsrep_flow_control_interval[ 173, 173 ]
wsrep_flow_control_interval_low173
wsrep_flow_control_interval_high173
wsrep_flow_control_statusOFF
wsrep_cert_deps_distance0.000000
wsrep_apply_oooe0.000000
wsrep_apply_oool0.000000
wsrep_apply_window0.000000
wsrep_commit_oooe0.000000
wsrep_commit_oool0.000000
wsrep_commit_window0.000000
wsrep_local_state4
wsrep_local_state_commentSynced
wsrep_cert_index_size0
wsrep_cert_bucket_count22
wsrep_gcache_pool_size1452
wsrep_causal_reads0
wsrep_cert_interval0.000000
wsrep_ist_receive_status
wsrep_ist_receive_seqno_start0
wsrep_ist_receive_seqno_current0
wsrep_ist_receive_seqno_end0
wsrep_incoming_addresses192.168.3.12:3306,192.168.3.13:3306,192.168.3.198:3306
wsrep_desync_count0
wsrep_evs_delayed
wsrep_evs_evict_list
wsrep_evs_repl_latency0/0/0/0/0
wsrep_evs_stateOPERATIONAL
wsrep_gcomm_uuidca7f9f30-4b2b-11e7-a0b7-e29969825862
wsrep_cluster_conf_id3
wsrep_cluster_size3
wsrep_cluster_state_uuid05719dfe-4b28-11e7-a651-ab4ab48f9b60
wsrep_cluster_statusPrimary
wsrep_connectedON
wsrep_local_bf_aborts0
wsrep_local_index2
wsrep_provider_nameGalera
wsrep_provider_vendorCodership Oy <info@codership.com>
wsrep_provider_version3.20(r7e383f7)
wsrep_readyON

简单测试:

在pxc03上创建数据库tdoa,并在上面建表,且插入几条数据

mysql> create database tdoa charset=‘utf8mb4‘;
Query OK, 1 row affected (0.05 sec)

mysql> use tdoa;
Database changed
mysql> create table user(id int unsigned primary key auto_increment,name varchar(30));
Query OK, 0 rows affected (0.05 sec)

mysql> show tables;
Tables_in_tdoa
user

1 row in set (0.00 sec)

mysql> insert into user(name) values(‘jack‘),(‘tom‘),(‘lily‘),(‘lucy‘);
Query OK, 4 rows affected (0.07 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select * from user;
idname
3jack
6tom
9lily
12lucy

4 rows in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;
idname
3jack
6tom
9lily
12lucy

4 rows in set (0.01 sec)
分别在pxc01和pxc02上查看是否有这个表和数据,可以看到数据能够正常同步

[root@master ~]# mysql -S /data/mysql/mysql.sock -uroot -p123456 -e ‘use tdoa;show tables;select * from user;‘

Warning: Using a password on the command line interface can be insecure.
Tables_in_tdoa
user
idname
3jack
6tom
9lily
12lucy

[root@slave01 ~]# mysql -S /data/mysql/mysql.sock -uroot -p123456 -e ‘use tdoa;show tables;select * from user;‘

Warning: Using a password on the command line interface can be insecure.
Tables_in_tdoa
user
idname
3jack
6tom
9lily
12lucy

至此,PXC安装启动完毕。

MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解

标签:other exec member nod dex color eset failure style

原文地址:http://www.cnblogs.com/reblue520/p/6962599.html