如果想新创建集群,需要保证有新的管理节点、计算节点和存储节点。在此给你提个建议,尽量不要把多个项目的数据库放在同一个集群下,很麻烦的,我们公司的项目就因为甲方资源紧张,没办法,三个项目用了一个cluster的集群,结果只要其中一个项目使用数据库资源过大,就会把三个项目一起影响。
所以如果资源足够的话,尽量拆成多个集群。
在开始演示之前,我们先介绍下两个概念。
概念一,数据的可选择性基数,也就是常说的cardinality值。
查询优化器在生成各种执行计划之前,得先从统计信息中取得相关数据,这样才能估算每步 *** 作所涉及到的记录数,而这个相关数据就是cardinality。简单来说,就是每个值在每个字段中的唯一值分布状态。
比如表t1有100行记录,其中一列为f1。f1中唯一值的个数可以是100个,也可以是1个,当然也可以是1到100之间的任何一个数字。这里唯一值越的多少,就是这个列的可选择基数。
那看到这里我们就明白了,为什么要在基数高的字段上建立索引,而基数低的的字段建立索引反而没有全表扫描来的快。当然这个只是一方面,至于更深入的探讨就不在我这篇探讨的范围了。
概念二,关于HINT的使用。
这里我来说下HINT是什么,在什么时候用。
HINT简单来说就是在某些特定的场景下人工协助MySQL优化器的工作,使她生成最优的执行计划。一般来说,优化器的执行计划都是最优化的,不过在某些特定场景下,执行计划可能不是最优化。
比如:表t1经过大量的频繁更新 *** 作,(UPDATE,DELETE,INSERT),cardinality已经很不准确了,这时候刚好执行了一条SQL,那么有可能这条SQL的执行计划就不是最优的。为什么说有可能呢?
来看下具体演示
譬如,以下两条SQL,
A:
select * from t1 where f1 = 20B:
select * from t1 where f1 = 30如果f1的值刚好频繁更新的值为30,并且没有达到MySQL自动更新cardinality值的临界值或者说用户设置了手动更新又或者用户减少了sample page等等,那么对这两条语句来说,可能不准确的就是B了。
这里顺带说下,MySQL提供了自动更新和手动更新表cardinality值的方法,因篇幅有限,需要的可以查阅手册。
那回到正题上,MySQL 8.0 带来了几个HINT,我今天就举个index_merge的例子。
示例表结构:
mysql>desc t1+------------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+------------+--------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || rank1 | int(11) | YES | MUL | NULL | || rank2 | int(11) | YES | MUL | NULL | || log_time | datetime | YES | MUL | NULL | || prefix_uid | varchar(100) | YES | | NULL | || desc1 | text | YES | | NULL | || rank3 | int(11) | YES | MUL | NULL | |+------------+--------------+------+-----+---------+----------------+7 rows in set (0.00 sec)表记录数:
mysql>select count(*) from t1+----------+| count(*) |+----------+| 32768 |+----------+1 row in set (0.01 sec)这里我们两条经典的SQL:
SQL C:
select * from t1 where rank1 = 1 or rank2 = 2 or rank3 = 2SQL D:
select * from t1 where rank1 =100 and rank2 =100 and rank3 =100表t1实际上在rank1,rank2,rank3三列上分别有一个二级索引。
那我们来看SQL C的查询计划。
显然,没有用到任何索引,扫描的行数为32034,cost为3243.65。
mysql>explain format=json select * from t1 where rank1 =1 or rank2 = 2 or rank3 = 2\G*************************** 1. row ***************************EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "3243.65" }, "table": { "table_name": "t1", "access_type": "ALL", "possible_keys": [ "idx_rank1", "idx_rank2", "idx_rank3" ], "rows_examined_per_scan": 32034, "rows_produced_per_join": 115, "filtered": "0.36", "cost_info": { "read_cost": "3232.07", "eval_cost": "11.58", "prefix_cost": "3243.65", "data_read_per_join": "49K" }, "used_columns": [ "id", "rank1", "rank2", "log_time", "prefix_uid", "desc1", "rank3" ], "attached_condition": "((`ytt`.`t1`.`rank1` = 1) or (`ytt`.`t1`.`rank2` = 2) or (`ytt`.`t1`.`rank3` = 2))" } }}1 row in set, 1 warning (0.00 sec)我们加上hint给相同的查询,再次看看查询计划。
这个时候用到了index_merge,union了三个列。扫描的行数为1103,cost为441.09,明显比之前的快了好几倍。
mysql>explain format=json select /*+ index_merge(t1) */ * from t1 where rank1 =1 or rank2 = 2 or rank3 = 2\G*************************** 1. row ***************************EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "441.09" }, "table": { "table_name": "t1", "access_type": "index_merge", "possible_keys": [ "idx_rank1", "idx_rank2", "idx_rank3" ], "key": "union(idx_rank1,idx_rank2,idx_rank3)", "key_length": "5,5,5", "rows_examined_per_scan": 1103, "rows_produced_per_join": 1103, "filtered": "100.00", "cost_info": { "read_cost": "330.79", "eval_cost": "110.30", "prefix_cost": "441.09", "data_read_per_join": "473K" }, "used_columns": [ "id", "rank1", "rank2", "log_time", "prefix_uid", "desc1", "rank3" ], "attached_condition": "((`ytt`.`t1`.`rank1` = 1) or (`ytt`.`t1`.`rank2` = 2) or (`ytt`.`t1`.`rank3` = 2))" } }}1 row in set, 1 warning (0.00 sec)我们再看下SQL D的计划:
不加HINT,
mysql>explain format=json select * from t1 where rank1 =100 and rank2 =100 and rank3 =100\G*************************** 1. row ***************************EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "534.34" }, "table": { "table_name": "t1", "access_type": "ref", "possible_keys": [ "idx_rank1", "idx_rank2", "idx_rank3" ], "key": "idx_rank1", "used_key_parts": [ "rank1" ], "key_length": "5", "ref": [ "const" ], "rows_examined_per_scan": 555, "rows_produced_per_join": 0, "filtered": "0.07", "cost_info": { "read_cost": "478.84", "eval_cost": "0.04", "prefix_cost": "534.34", "data_read_per_join": "176" }, "used_columns": [ "id", "rank1", "rank2", "log_time", "prefix_uid", "desc1", "rank3" ], "attached_condition": "((`ytt`.`t1`.`rank3` = 100) and (`ytt`.`t1`.`rank2` = 100))" } }}1 row in set, 1 warning (0.00 sec)加了HINT,
mysql>explain format=json select /*+ index_merge(t1)*/ * from t1 where rank1 =100 and rank2 =100 and rank3 =100\G*************************** 1. row ***************************EXPLAIN: { "query_block": { "select_id": 1, "cost_info": { "query_cost": "5.23" }, "table": { "table_name": "t1", "access_type": "index_merge", "possible_keys": [ "idx_rank1", "idx_rank2", "idx_rank3" ], "key": "intersect(idx_rank1,idx_rank2,idx_rank3)", "key_length": "5,5,5", "rows_examined_per_scan": 1, "rows_produced_per_join": 1, "filtered": "100.00", "cost_info": { "read_cost": "5.13", "eval_cost": "0.10", "prefix_cost": "5.23", "data_read_per_join": "440" }, "used_columns": [ "id", "rank1", "rank2", "log_time", "prefix_uid", "desc1", "rank3" ], "attached_condition": "((`ytt`.`t1`.`rank3` = 100) and (`ytt`.`t1`.`rank2` = 100) and (`ytt`.`t1`.`rank1` = 100))" } }}1 row in set, 1 warning (0.00 sec)对比下以上两个,加了HINT的比不加HINT的cost小了100倍。
总结下,就是说表的cardinality值影响这张的查询计划,如果这个值没有正常更新的话,就需要手工加HINT了。相信MySQL未来的版本会带来更多的HINT。
mysql ndbcluster 安装1.数据库规划:
*** 作系统 centos 6.3
ndbcluster 版本:MySQL-Cluster-gpl-7.4.8-1.el6.x86_64
2个管理节点、2个sql节点、2个数据节点(管理节点和sql节点放在同一台服务器上)
如下:
管理节点1 192.168.1.17
管理节点2 192.168.1.18
sql节点1 192.168.1.17
sql节点2 192.168.1.18
ndbd 节点1 192.168.1.19
ndbd 节点2 192.168.1.20
2. *** 作系统设置:
设置服务主机名称
关闭selinux和防火墙
设置内核参数和最大进程数
下载mysql集群
MySQL-Cluster-gpl-7.4.8-1.el6.x86_64.rpm-bundle.tar
上传到服务器的相应目录下如 /home/mysqlinstall
3.安装管理节点和sql节点:
To check if your system has any RPM version of any MySQL package currently installed, run:
shell>rpm -qa | grep -i mysql
检查
[root@redis1 ~]# rpm -qa | grep -i mysql
mysql-libs-5.1.61-4.el6.x86_64
如果有删除
shell>rpm -e mysql-libs-5.1.61-4.el6.x86_64
删除报错:
[root@redis1 ~]# rpm -e mysql-libs-5.1.61-4.el6.x86_64
error: Failed dependencies:
libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
mysql-libs is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
[root@redis1 ~]#
解决:
[root@redis1 ~]# rpm -e --nodeps mysql-libs-5.1.61-4.el6.x86_64
[root@redis1 ~]#
[root@ndbcluster1 mysqlinstall]# rpm -ivh MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm
[root@ndbcluster1 mysqlinstall]# rpm -ivh MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm
4.安装数据节点:
执行上面步骤
[root@ndb mysqlinstall]# rpm -ivh MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm
[root@ndb mysqlinstall]# rpm -ivh MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm
5.配置集群
管理节点建立目录
mkdir /var/lib/mysql-cluster
mkdir -p /data/mysql/ndbdata
5.启动关闭集群:
启动管理节点
ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial (第一次启动加--initial修改了配置文件后的启动)
启动数据节点
ndbmtd --initial
启动sql节点
service mysql start
查看集群状态
shell>ndb_mgm -e "SHOW"
关闭管理节点和数据节点
ndb_mgm -e shutdown
关闭sql节点
service mysql stop
安装遇到的问题:
1.权限问题启动mysql报错
[root@sqlmgm2 mysql]# service mysql start
Starting MySQL..The server quit without updating PID file ([FAILED]sql/sqlmgm2.pid).
[root@sqlmgm2 mysql]#
[root@sqlmgm2 mysql]# ll
total 110644
-rw-rw---- 1 mysql mysql 56 Dec 14 17:13 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 Dec 14 17:39 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Dec 14 17:40 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Dec 14 17:13 ib_logfile1
drwx------ 2 mysql mysql 4096 Dec 14 17:39 mysql
drwx------ 2 root root 4096 Dec 14 17:39 ndbinfo
drwx------ 2 root root 4096 Dec 14 17:39 performance_schema
-rw-r----- 1 mysql mysql30068 Dec 14 17:40 sqlmgm2.err
drwx------ 2 root root 4096 Dec 14 17:33 test
[root@sqlmgm2 mysql]# chown mysql:mysql -R mysql
[root@sqlmgm2 mysql]# service mysql start
Starting MySQL.. [ OK ]
[root@sqlmgm2 mysql]#
2.安装rpm包时,报包依赖关系如下:
[root@ndbcluster2 mysqlinstall]# rpm -ivh MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm
warning: MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libnuma.so.1()(64bit) is needed by MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64
libnuma.so.1(libnuma_1.1)(64bit) is needed by MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64
libnuma.so.1(libnuma_1.2)(64bit) is needed by MySQL-Cluster-server-gpl-7.4.8-1.el6.x86_64
解决方法是把安装的依赖的包
[root@ndbcluster2 mysqlinstall]# yum install numactl
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.opencas.cn
* updates: mirrors.opencas.cn
Setting up Install Process
Resolving Dependencies
-->Running transaction check
--->Package numactl.x86_64 0:2.0.9-2.el6 will be installed
-->Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================
Package ArchVersion Repository Size
==============================================================================================================================
Installing:
numactl x86_64 2.0.9-2.el6 base 74 k
Transaction Summary
==============================================================================================================================
Install 1 Package(s)
Total download size: 74 k
Installed size: 171 k
Is this ok [y/N]: y
Downloading Packages:
numactl-2.0.9-2.el6.x86_64.rpm | 74 kB 00:00
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-3.el6.centos.9.x86_64 (@anaconda-CentOS-201207061011.x86_64/6.3)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : numactl-2.0.9-2.el6.x86_64 1/1
Verifying : numactl-2.0.9-2.el6.x86_64 1/1
Installed:
numactl.x86_64 0:2.0.9-2.el6
Complete!
3.启动数据库节点无法和管理节点相连
原因由于防火墙和selinux没有关闭
解决关闭防火墙和selinux
一、关闭防火墙
1、重启后永久性生效:
开启:chkconfig iptables on
关闭:chkconfig iptables off
2、即时生效,重启后失效:
开启:service iptables start
关闭:service iptables stop
二、关闭SELinux
vim /etc/selinux/config # 改为 SELINUX=disabled
# 保存退出,重启服务器
init 6
禁用SeLinux
#永久禁用,需要重启生效。
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# 临时禁用,不需要重启
setenforce 0
4.root用户不能登录
[root@ndbcluster2 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@ndbcluster2 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
设置--skip-grant-tables跳过授权表认证
service mysqld stop
在配置文件中添加--skip-grant-tables(/etc/my.cnf)
service mysqld start
另外开个SSH连接
[root@localhost ~]# mysql
mysql>use mysql
mysql>update user set password=password("123456") where user="root"
mysql>flush privileges
mysql>exit
去掉--skip-grant-tables重启mysql,root用户可以用设置的密码登录
设置root用户远程登录
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456' WITH GRANT OPTION
执行报如下错误,ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
[root@ndbcluster1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with or \g.
Your MySQL connection id is 2
Server version: 5.6.27-ndb-7.4.8-cluster-gpl
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql>
mysql>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'root' WITH GRANT OPTION
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'root001' WITH GRANT OPTION
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
执行SET PASSWORD修改成功
mysql>SET PASSWORD = PASSWORD('root001')
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'root' WITH GRANT OPTION
Query OK, 0 rows affected (0.02 sec)
mysql>
FLUSH PRIVILEGES
执行完成
远程连接测试,mysql集群节点,发现一个节点可以连接,一个节点不能连接
这个问题纠结很久
原因上面的 *** 作,没有在另外一个节点上执行导致的,mysql集群的用户认证是分开管理的,要两个节点都要执行
索引以后创建用户要两个节点都执行
5.配置两个管理节点时show报错Could not get configuration
[root@ndbcluster2 mysql-cluster]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm>show
Connected to Management Server at: 192.168.1.17:1186
ERROR Message: The cluster configuration is not yet confirmed by all defined management servers. This management server is still waiting for node 6 to connect.
Could not get configuration
* 4012: Failed to get configuration
*The cluster configuration is not yet confirmed by all defined management servers. This management server is still waiting for node 6 to connect.
ndb_mgm>show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.1.18)
id=3 (not connected, accepting connect from 192.168.1.17)
[ndb_mgmd(MGM)] 2 node(s)
id=1@192.168.1.17 (mysql-5.6.27 ndb-7.4.8)
id=6@192.168.1.18 (mysql-5.6.27 ndb-7.4.8)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.1.17)
id=5 (not connected, accepting connect from 192.168.1.18)
两个管理节点同时都启动起来,才能看到状态
ndb_mgm>show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2@192.168.1.18 (mysql-5.6.27 ndb-7.4.8, Nodegroup: 0, *)
id=3@192.168.1.17 (mysql-5.6.27 ndb-7.4.8, Nodegroup: 0)
[ndb_mgmd(MGM)] 2 node(s)
id=1@192.168.1.17 (mysql-5.6.27 ndb-7.4.8)
id=6@192.168.1.18 (mysql-5.6.27 ndb-7.4.8)
[mysqld(API)] 2 node(s)
id=4@192.168.1.17 (mysql-5.6.27 ndb-7.4.8)
id=5@192.168.1.18 (mysql-5.6.27 ndb-7.4.8)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)