用户和权限操作
查看用户
select user, host from mysql.user;
查看用户权限
select * from mysql.user where user='root'\G;
设置用户权限
grant ALL on *.* to 'root'@'localhost' identified by 'root' [with grant option];
revoke ALL, grant option on *.* from 'root'@'localhost';
开启远程访问
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf bind-address 修改为 0.0.0.0
建库操作
建库
create database if not exists my_db default charset utf8 COLLATE utf8_general_ci;
常用function
sequence
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `nextval`(`seq_name` varchar(100)) RETURNS bigint(20)
BEGIN
DECLARE cur_val bigint;
UPDATE
ushare_sequence
SET
name = seq_name
WHERE
name = seq_name;
SELECT
cur_value INTO cur_val
FROM
sequence_table_name
WHERE
name = seq_name;
IF cur_val IS NOT NULL THEN
UPDATE
sequence_table_name
SET
cur_value = IF (
(cur_value + increment) > max_value OR (cur_value + increment) < min_value,
IF (
cycle = TRUE,
IF (
(cur_value + increment) > max_value,
min_value,
max_value
),
NULL
),
cur_value + increment
)
WHERE
name = seq_name;
END IF;
RETURN cur_val;
END$$
DELIMITER ;
对应表
CREATE TABLE `sequence_table_name` (
`name` varchar(100) NOT NULL,
`increment` int(11) NOT NULL DEFAULT '1' COMMENT='步长',
`min_value` int(11) NOT NULL DEFAULT '1' COMMENT='最小值',
`max_value` bigint(20) NOT NULL DEFAULT '9223372036854775807' COMMENT='最大值',
`cur_value` bigint(20) DEFAULT '1' COMMENT='当前值',
`cycle` tinyint(1) NOT NULL DEFAULT '0' COMMENT='是否循环',
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='序列表';
binlog日志
mysqlbinlog -d ushare --start-datetime='2018-08-27 09:00:00' --stop-datetime='2018-08-27 09:10:00'
基础
Chapter 1
SQL, structured query language
writen in c and c++
provides transactional and untransactional storage engine
uses very fast b-tree disk table and index compression
uses a very fast thread-based memory allocation system
executes very fast joins using an optimized nested-loop join
implements in-memory hash tables, which are used as temporary tables
for a function call, no spaces are permitted between the function name and the “(” that follows it.
Support for up to 64 indexes per table. Each index may consist of 1 to 16 columns or parts of columns.
MySQL is named after co-founder Monty Widenius’s daughter, My.
Chapter 3
database names and table names are case sensitive.
Store birth date rather than age
NULL
NULL is a special value that cannot be compared using usual comparison operators. The result of any arithmetic comparison with NULL is also NULL.
Two NULL values are regarded as equal in a group by
MOD(something, 12) returns a number between 0 and 11
pattern character ‘_’ is regarded as a letter.
regexp or rlike
ONLY_FULL_GROUP_BY, cannot execute aggregation without group by
Chapter 4
memory table
database/schema, table, event, view, function/procedure, trigger
mysqlslap, test the performance
(4.6.7.3) Using mysqlbinlog to Back Up Binary Log Files
mysqlslap —delimiter=”;” —create=”CREATE TABLE a (b int);INSERT INTO a VALUES (23)” —query=”SELECT * FROM a” —concurrency=50 —iterations=200
安装
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
yum -y install mysql-community-server