Archive for the ‘mysql’ Category

Mysql Invalid default date value if zeros

Step 1 mysql> show variables like ‘sql_mode’ ; | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION   Step 2 Add to mysqld.cnf <pre lang="bash"> sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Step 3 Restart mysql

Docker if you need to edit file on stopped docker

I had a problem with a container which wouldn’t start due to a bad config change I made. I was able to copy the file out of the stopped container and edit it. something like: docker cp test-mysql:/etc/mysql/my.cnf . Edit it and copy back docker cp my.cnf test-mysql:/etc/mysql/my.cnf docker start mysql docker exec -ti mysql […]

Simple php based mysql query script

<?php   $dbname = ‘dbname’; $dbuser = ‘user’; $dbpass = ‘pass’; $dbhost = ‘hostname’;   $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to ‘$dbhost’"); mysql_select_db($dbname) or die("Could not open the db ‘$dbname’"); $test_query = "SELECT 1,2,3,4 FROM yourtable"; $result = mysql_query($test_query); $tblCnt = 0; while($tbl = mysql_fetch_array($result)) { $tblCnt++; echo $tbl[0]."<br />\n"; echo […]

Mysql show full list of processes in memory

SHOW FULL PROCESSLIST;

Mysql convert timestamp to date

SELECT DATE_FORMAT(FROM_UNIXTIME(yourfield), ‘%e %b %Y’) FROM yourtable;

When NoSql is better over sql database

Для начала нужно понять, когда хорошо использовать Sql? Когда данные структурированы и легко ложатся на модель кортежей. Если же данные имеют привычку менять схему/структуру, то можно использовать NoSql. Единственное, что у NoSql баз проблемы с ACID (транзакциями, атомарностью и т.п.)

Check if database table column exists

SHOW COLUMNS FROM Tablename WHERE FIELD = "Fieldname";

Remove duplicates

DELETE FROM main_table USING main_table, main_table e1 WHERE main_table.id > e1.id AND main_table.entity_id = e1.entity_id;

Awk Insert into table

cat file.csv | awk -F, ‘{print "INSERT INTO mytable VALUES ("$1",*"$2"*,"$3");"}’ echo https://api.trustpilot.com/tagname/tags | awk -F/ ‘{print $2}’

CREATE COPY OF THE TABLE WITH INDEXES

CREATE TABLE newtable LIKE oldtable;