mysql> DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); mysql> FLUSH PRIVILEGES;
创建数据库用户并授权
1 2 3 4
CREATEUSER bfg_user IDENTIFIEDBY'gXk9IDpybrJPVMKq'; GRANTSELECT, REPLICATIONSLAVE, REPLICATIONCLIENTON *.* TO'bfg_user'@'%'; -- GRANT ALL PRIVILEGES ON *.* TO 'bfg_user'@'%'; FLUSHPRIVILEGES;
ERROR 3 (HY000): Error writing file ‘/tmp/MYeaZGpS’ (Errcode: 28 - No space left on device)
根据提示,是说mysql中/tmp使用的磁盘空间不足。 在MYSQL命令行下执行:
1 2 3 4 5 6 7
mysql> SHOW VARIABLES LIKE 'tmpdir'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | tmpdir | /tmp | +---------------+-------+ 1 row in set (0.01 sec)
$ cd /opt/data $ mkdir mysqltmp $ chmod a+w mysqltmp
然后修改my.cnf配置文件在其中修改tmpdir
1 2 3 4 5 6
$ whereis my.cnf $ cd {my.cnf所在的目录} $ vi my.cnf
修改的内容如下 tmpdir = /opt/data/mysqltmp
重启mysql
1
$ /etc/init.d/mysqld restart # 不同Linux系统,命令可能不同
在MYSQL命令行中查看下是否生效
1 2 3 4 5 6 7
mysql> SHOW VARIABLES LIKE 'tmpdir'; +---------------+--------------------+ | Variable_name | Value | +---------------+--------------------+ | tmpdir | /opt/data/mysqltmp | +---------------+--------------------+ 1 row in set (0.00 sec)
问题解决。
读一致性
mysql有一个表,有1000W条记录。在9:00的时候A用户对表进行查询,查询需10分钟才能完成,表没索引,FULL SCAN,表中某条记录的值为100。在9:05分的时候B对表进行了UPDATE操作,将记录的值修改为200。那么在9:10的时候,A获致到的是100还是200?结果是100。因为这是读一致性的问题,在A查的时候,它看到的是整个表在那一刻的快照的数据,返回的是那一刻的结果。不过,它有可能会抛出 snapshot too old 这个异常。
mysql 修改表名
1
ALTER TABLE table_name RENAME TO new_table_name;
INSERT INTO插入数据
如果不指定插入的列名,则VALUES的值必须与表的所有列名一一对应
1 2 3 4 5 6 7 8 9
CREATETABLE t_user ( idINT AUTO_INCREMENT PRIMARY KEY, nameVARCHAR(20), age INT );
INSERT IGNORE INTO tableName(column_list) VALUES (value_list), (value_list), ...
将一个数据库中的表导入到另一个数据库中的表
将数据库A中的表a的数据导入到数据库B中的表b中。
1 2 3
INSERT [IGNORE] INTO 数据库B.`表名b` SELECT * FROM 数据库A.`表名a`; INSERT [IGNORE] INTO 数据库B.`表名b` SELECT col1, col2, col3 FROM 数据库A.`表名a`; INSERT [IGNORE] INTO 数据库B.`表名b`(col1, col2, col3) SELECT col1, col2, col3 FROM 数据库A.`表名a`;
MySQL千万级数据快速分页
数据量少的时候,可以这样写:
1
SELECT * FROM tableName ORDER BY id LIMIT 1000, 10;
但在数据量达到百万级,甚至千万级的时候,这样写会慢死:
1
SELECT * FROM tableName ORDER BY id LIMIT 1000000, 10;
可能耗费几十秒,网上有很多优化的方法是这样的:
1
SELECT * FROM tableName WHERE id >= (SELECT id FROM tableName LIMIT 1000000, 1) LIMIT 10;
是的,速度提升到0.x秒了,看样子还行。但,还不是完美的!下面这句才是完美的:
1
SELECT * FROM tableName WHERE id BETWEEN 1000000 AND 1000010;
比上面那句,还要再快5至10倍
另外,如果需要查询id不是连续的一段,最佳的方法就是先找出id,然后用IN查询,代码如下:
1
SELECT * FROM tableName WHERE id IN (10000, 100000, 1000000, ...);
MySQL [test]> select * from t_user; +----+-------+ | id | name | +----+-------+ | 1 | Scott | | 2 | Tiger | | 3 | Larry | +----+-------+ 3 rows in set (0.00 sec)
MySQL [test]> select * from t_contact; +----+------+ | id | card | +----+------+ | 1 | A | | 1 | B | | 2 | C | | 4 | D | +----+------+
join 查询
1 2 3 4 5 6 7 8
MySQL [test]> select * from t_user tu join t_contact tc on tu.id=tc.id; +----+-------+----+------+ | id | name | id | card | +----+-------+----+------+ | 1 | Scott | 1 | A | | 1 | Scott | 1 | B | | 2 | Tiger | 2 | C | +----+-------+----+------+
inner join 查询 只返回两个表中联结字段相等的行
1 2 3 4 5 6 7 8
MySQL [test]> select * from t_user tu inner join t_contact tc on tu.id=tc.id; +----+-------+----+------+ | id | name | id | card | +----+-------+----+------+ | 1 | Scott | 1 | A | | 1 | Scott | 1 | B | | 2 | Tiger | 2 | C | +----+-------+----+------+
left join 查询 返回左表中的所有记录和右表中联结字段相等的记录
1 2 3 4 5 6 7 8 9
MySQL [test]> select * from t_user tu left join t_contact tc on tu.id=tc.id; +----+-------+------+------+ | id | name | id | card | +----+-------+------+------+ | 1 | Scott | 1 | A | | 1 | Scott | 1 | B | | 2 | Tiger | 2 | C | | 3 | Larry | NULL | NULL | +----+-------+------+------+
right join 查询 返回右表中的所有记录和左表中联结字段相等的记录
1 2 3 4 5 6 7 8 9
MySQL [test]> select * from t_user tu right join t_contact tc on tu.id=tc.id; +------+-------+----+------+ | id | name | id | card | +------+-------+----+------+ | 1 | Scott | 1 | A | | 1 | Scott | 1 | B | | 2 | Tiger | 2 | C | | NULL | NULL | 4 | D | +------+-------+----+------+
$ cd /home/hewentian/ProjectD/zookeeperCluster/server2/zookeeper-3.4.6/ $ ./bin/zkServer.sh status JMX enabled by default Using config: /home/hewentian/ProjectD/zookeeperCluster/server2/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: leader
$ cd /home/hewentian/ProjectD/zookeeperCluster/server3/zookeeper-3.4.6/ $ ./bin/zkServer.sh status JMX enabled by default Using config: /home/hewentian/ProjectD/zookeeperCluster/server3/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: follower
$ cd /home/hewentian/ProjectD/zookeeper-3.4.6/ $ mkdir data
修改好后,我们就可以进入bin目录下启动zookeeper了
1 2
$ cd /home/hewentian/ProjectD/zookeeper-3.4.6/bin $ ./zkServer.sh start
启动后,我们可以连到zookeeper服务器,命令如下:
1 2
$ cd /home/hewentian/ProjectD/zookeeper-3.4.6/bin $ ./zkCli.sh -server 127.0.0.1:2181
一些操作例子,如下:
From here, you can try a few simple commands to get a feel for this simple command line interface. First, start by issuing the list command, as in ls, yielding:
1 2
[zkshell: 8] ls / [zookeeper]
Next, create a new znode by running create /zk_test my_data. This creates a new znode and associates the string “my_data” with the node. You should see:
1 2
[zkshell: 9] create /zk_test my_data Created /zk_test
Issue another ls / command to see what the directory looks like:
1 2
[zkshell: 11] ls / [zookeeper, zk_test]
Notice that the zk_test directory has now been created. Next, verify that the data was associated with the znode by running the get command, as in:
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.
一些常用命令: hexo new “postName” #新建文章 hexo new page “pageName” #新建页面 hexo generate #生成静态页面至public目录, hexo g hexo server #开启预览访问端口(默认端口4000,’ctrl + c’关闭server), hexo s hexo deploy #将.deploy目录部署到GitHub, hexo d hexo help # 查看帮助 hexo version #查看Hexo的版本
ENOSPC Error (Linux)报错的处理
如果启动的时候报如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ hexo s INFO Start processing FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html Error: watch /home/hewentian/ProjectD/gitHub/hewentian.github.io/themes/landscape/ ENOSPC at _errnoException (util.js:1022:11) at FSWatcher.start (fs.js:1382:19) at Object.fs.watch (fs.js:1408:11) at createFsWatchInstance (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:37:15) at setFsWatchListener (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:80:15) at FSWatcher.NodeFsHandler._watchWithNodeFs (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:228:14) at FSWatcher.NodeFsHandler._handleDir (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:407:19) at FSWatcher.<anonymous> (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:455:19) at FSWatcher.<anonymous> (/home/hewentian/ProjectD/gitHub/hewentian.github.io/node_modules/chokidar/lib/nodefs-handler.js:460:16) at FSReqWrap.oncomplete (fs.js:153:5)
可以尝试通过如下方式解决:
1 2
$ npm dedupe $ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
publicstaticvoidmain(String[] args)throws InterruptedException, IOException { for (int i = 0; i < 100; i++) { new Thread(new SyncAddRunnable(1, 2)).start(); new Thread(new SyncAddRunnable(2, 1)).start(); } }
Maven dependency scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime etc). Maven provides six scopes i.e. compile, provided, runtime, test, system, and import.
Some times, you will have to refer jar files which are not in maven repository (neither local, central or remote repository). You can use these jars by placing them in project’s lib folder and configure the external dependency like this:
The groupId and artifactId are both set to the name of the dependency.
The scope element value is set to system.
The systemPath element refer to the location of the JAR file.
Maven Dependency Tree
Using maven’s dependency:tree command, you can view list of all dependencies into your project – transitively. Transitive dependency means that if A depends on B and B depends on C, then A depends on both B and C.
Transitivity brings a very serious problem when different versions of the same artifacts are included by different dependencies. It may cause version mismatch issue in runtime. In this case, dependency:tree command is be very useful in dealing with conflicts of JARs.
$ mvn dependency:tree
Maven Dependency Exclusion
Apart from version mismatch issue caused with transitive dependency, there can be version mismatch between project artifacts and artifacts from the platform of deployment, such as Tomcat or another server.
To resolve such version mismatch issues, maven provides tag, in order to break the transitive dependency.
For example, when you have JUnit 4.12 in classpath and including DBUnit dependency, then you will need to remove JUnit 3.8.2 dependency. It can be done with exclusion tag.
<project> ... <build> <!-- To define the plugin version in your parent POM --> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.2</version> </plugin> ... </plugins> </pluginManagement> <!-- To use the plugin goals in your POM or parent POM --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.2</version> </plugin> ... </plugins> </build> ... </project>
Mixed Content: The page at 'https://www.a.com' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.b.com/data/user/save'. This request has been blocked; the content must be served over HTTPS.