sudo mysql.server start | stop | restart | pause
https://mariadb.com/kb/en/library/compound-composite-indexes/
Running SQL via pipe
echo "show variables like 'char%';" | mysql -u root -p
select
dataPointTimeBeginning,
dataPointTimeEnd,
TIMESTAMPDIFF(MINUTE, dataPointTimeBeginning, dataPointTimeEnd),
TIME_TO_SEC(TIMEDIFF(dataPointTimeEnd, dataPointTimeBeginning))/60
from asdb.MaccorCycle LIMIT 1;
+------------------------+---------------------+-----------------------------------------------------------------+-----------------------------------------------------------------
| dataPointTimeBeginning | dataPointTimeEnd | TIMESTAMPDIFF(MINUTE, dataPointTimeBeginning, dataPointTimeEnd) | TIME_TO_SEC(TIMEDIFF(dataPointTimeEnd, dataPointTimeBeginning))/60 |
+------------------------+---------------------+-----------------------------------------------------------------+-----------------------------------------------------------------
| 2014-11-01 18:32:26 | 2014-11-01 20:31:23 | 118 | 118.9500 |
Dynamic columns
https://mariadb.com/kb/en/mariadb/dynamic-columns/
create table assets ( item_name varchar(32) primary key, -- A common attribute for all items dynamic_cols blob -- Dynamic columns will be stored here);
INSERT INTO assets VALUES ('MariaDB T-shirt', COLUMN_CREATE('color', 'blue', 'size', 'XL'));
INSERT INTO assets VALUES ('Thinkpad Laptop', COLUMN_CREATE('color', 'black', 'price', 500));
SELECT item_name, COLUMN_GET(dynamic_cols, 'color' as char) AS color FROM assets; +-----------------+-------+ | item_name | color | +-----------------+-------+ | MariaDB T-shirt | blue | | Thinkpad Laptop | black | +-----------------+-------+
-- Remove a column:UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, "price") WHERE COLUMN_GET(dynamic_cols, 'color' as char)='black'; -- Add a column:UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, 'warranty', '3 years')WHERE item_name='Thinkpad Laptop';
http://www.eversql.com/mysql-5-7-vs-mysql-8-0-whats-new-in-mysql-8-0/
https://habrahabr.ru/post/312134/
SHOW INDEXES FROM A;
SHOW CREATE TABLE A;
SHOW FULL PROCESSLIST; <--- all running SQLs
SHOW TABLE STATUS;
http://www.unofficialmysqlguide.com/
CASE WHEN THEN, IF, IFNULL in SQL
http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html
CTE
http://mysqlserverteam.com/mysql-8-0-labs-recursive-common-table-expressions-in-mysql-ctes/
Configuration and Administration
SHOW VARIABLES LIKE "%version%" ;
+-------------------------+--------------------------+
| Variable_name | Value |
+-------------------------+---------------------------+
| innodb_version | 5.6.32-78.1 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 10.1.18-MariaDB |
| version_comment | MariaDB Server |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
| version_malloc_library | system jemalloc |
| version_ssl_library | OpenSSL 1.0.1e-fips 11 Feb 2013 |
| wsrep_patch_version | wsrep_25.16 |
+-------------------------+---------------------------------+
mysql> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.1.18-MariaDB |
+-----------------+
SHOW VARIABLES
show variables like 'GENERAL%';
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | OFF |
| general_log_file | /var/log/mysql_general_query.log |
+------------------+----------------------------------+
SET GLOBAL general_log=1;
mysqladmin variables
http://dev.mysql.com/doc/refman/5.7/en/show-variables.html
Which my.cnf in use?
mysqladmin --help | grep -A1 'Default options'
mysqld --help --verbose
strace mysql ";" 2>&1 | grep cnf
stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=2608, ...}) = 0 open("/etc/my.cnf", O_RDONLY|O_CLOEXEC) = 3
/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf
USER management
SELECT user,host FROM mysql.user ;
select USER(), CURRENT_USER();
CREATE USER 'golden'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'golden'@'%'; FLUSH PRIVILEGES;
MariaDB [mysql]> status
--------------
mysql Ver 15.1 Distrib 10.1.7-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 38
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.1.7-MariaDB-log MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 20 hours 15 min 9 sec
Threads: 1 Questions: 284 Slow queries: 4 Opens: 92 Flush tables: 2 Open tables: 30 Queries per second avg: 0.003
--------------
http://www.heidisql.com GUI client
https://habrahabr.ru/post/310954/
Query profiling
https://www.digitalocean.com/community/tutorials/how-to-use-mysql-query-profiling
http://blog.code4hire.com/2016/04/MySql-default-timezone/ timezone
https://habrahabr.ru/company/oleg-bunin/blog/309328/
http://habrahabr.ru/post/238513/ locks on InnoDB
https://engineering.pinterest.com/blog/open-sourcing-pinterest-mysql-management-tools
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.FileSize
https://nylas.com/blog/growing-up-with-mysql/
http://mysqlserverteam.com/mysql-5-7-innodb-intrinsic-tables/
https://mariadb.com/kb/en/mariadb/optimizing-tables/
https://mariadb.org/defragmenting-unused-space-on-innodb-tablespace/
http://dba.stackexchange.com/questions/64134/deleting-reclaiming-space-from-innodb-table
UPDATE table and using the same table in FROM :
UPDATE film SET language_id = 2 WHERE film_id IN (SELECT * FROM (SELECT f.film_id FROM film f INNER JOIN film_actor fa ON f.film_id = fa.film_id WHERE fa.actor_id = 12) tblTmp);
delete tmp_a from tmp_a inner join
(SELECT * FROM (select a, b from tmp_a group by a,b) A) TMP
ON tmp_a.a=TMP.a and tmp_a.b=TMP.b;
https://habrahabr.ru/post/302926/ how to generate numeric series
http://dom.as/2015/07/30/on-order-by-optimization/
http://blog.stephenn.com/2015/03/how-to-examine-and-optimise-locking-in.html
https://github.com/sysown/proxysql
https://eng.uber.com/schemaless-part-one/
https://eng.uber.com/schemaless-part-two/
https://eng.uber.com/schemaless-part-three/
http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/
http://www.folkstalk.com/2013/03/grouped-row-number-function-mysql.html
Simulating ROW_NUMBER() in mysql
SELECT @row_number:=@row_number+1 AS row_number,file from TMP_OUT , (SELECT @row_number:=0) AS t where protocol='APPLE-TP7-4600' and chemistry like 'Co%';
JSON in mysql
https://habrahabr.ru/post/279155/
IMPORT from file
create table TMP_MATERIAL (name varchar(25), chemistry varchar(50), PRIMARY KEY(id));
mysqlimport -h 17.208.224.27 -ubdms -pbdms -v --local --ignore-lines=1 --fields-terminated-by='\t' abe TMP_MATERIAL
only after that add AUTO_INCREMENT:
ALTER table TMP_MATERIAL ADD (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id));
mysql -h 17.208.224.19 -ubdms -pbdms -v asdb < create_ls.sql
mysqlimport -h 17.208.224.19 -ubdms -pbdms -v --local --ignore-lines=1 --fields-terminated-by=, asdb TMP_LS
Export to File
SELECT a,b,a+b
INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
Partitioning
http://habrahabr.ru/post/269647/
https://habrahabr.ru/company/oleg-bunin/blog/309330/
https://www.youtube.com/watch?v=tJL3XPnh954
http://blog.monitis.com/2011/07/12/101-tips-to-mysql-tuning-and-optimization/
https://mariadb.com/kb/en/mariadb/spider-storage-engine-overview/
http://habrahabr.ru/company/mailru/blog/234125/ MySQL +PHP
https://mathiasbynens.be/notes/mysql-utf8mb4 UNICODE
Sharding/ clustering
http://www.cloud-elements.com/postgresql-mysql-manage-transition-automatic-manual-2/
https://engineering.pinterest.com/blog/sharding-pinterest-how-we-scaled-our-mysql-fleet/
https://news.ycombinator.com/item?id=10086782
mysql -h 9.1.1.101 -ubdms -pbdms asdb -v < SELECT_SQL > delete_progress4
http://gomorpheus.com/blog/2014-10-31-how-to-handle-huge-database-tables
From mysql docs for function TIMESTAMPDIFF:
The unit for the result (an integer) is given by the unit argument. The legal values for unit are MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR. Result is integer:
TIMESTAMPDIFF(MINUTE, job_start, job_end) > 150
select count(*) from Distillation where date(distillationEnd)='2016-06-29';
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
ALL VIEWS
SHOW FULL TABLES IN abe WHERE TABLE_TYPE LIKE 'VIEW';
http://www.databasejournal.com/features/mysql/handling-slow-processes-in-mysql.html
mysql> show processlist;
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND != 'Sleep';
Creating sort index
SELECT
dataPointTimeBeginning,
voltageBeginning,
capacityBeginning,
energyBeginning,
currentBeginning FROM (
SELECT dataPointTime as dataPointTimeBeginning,
voltage as voltageBeginning,
capacity as capacityBeginning,
energy as energyBeginning,
current as currentBeginning
FROM MaccorRaw WHERE projectId=11 AND testId=308 AND testRunId=4292 AND cycle=474 AND state='C' AND stateIndex=0
ORDER BY fileDate, fileRecordNumber ASC) as sub LIMIT 1
Creating sort index |
SELECT dataPointTime as dataPointTimeEnd,
voltage as voltageEnd,
capacity as capacityEnd,
energy as energyEnd,
current as currentEnd
FROM MaccorRaw WHERE projectId=11 AND testId=308 and testRunId=4292 AND cycle=113 AND state='R' AND stateIndex=1
ORDER BY fileDate, fileRecordNumber DESC LIMIT 1
DROP CONSTRAINT
http://stackoverflow.com/questions/3487691/dropping-unique-constraint-from-mysql-table
SHOW INDEX FROM tbl_name to find out the name of the index.
The name of the index is stored in the column called key_name in the results of that query.
Then you can use DROP INDEX:
DROP INDEX index_name ON tbl_name
or the ALTER TABLE syntax:
ALTER TABLE tbl_name DROP INDEX index_name
mysql from command line
http://datamonkey.pro/blog/mysql_give_me_my_data/
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html
mysql -u bdms -pbdms -Bse "show databases;select count(*) from asdb.maccor_cycle"
here -B supress header
How to show/hide | ??
show create table <table>;
mysql> select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='artifact_id'; +-----------------------+ | TABLE_NAME | +-----------------------+ | ArtifactParameter | | ArtifactParameterList | | ArtifactProperty | | ArtifactPropertyList | | ProjectArtifact | | TestArtifact | | TestRunArtifact | +-----------------------+ 7 rows in set (0.06 sec)
Logging all queries
http://stackoverflow.com/questions/303994/log-all-queries-in-mysql
http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/
http://serverfault.com/questions/71071/how-to-enable-mysql-logging
Beaver mysql logger
http://www.pontikis.net/blog/how-and-when-to-enable-mysql-logs
http://learninglamp.wordpress.com/2012/04/14/tuning-mysql/
show used settings
./mysqladmin variables
/mysqld --help --verbose
http://habrahabr.ru/post/196692/
http://gahcep.github.io/blog/2013/03/11/linux-mysql-setup/
http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html
http://dev.mysql.com/doc/refman/5.6/en/replication.html
http://dev.mysql.com/doc/refman/5.6/en/partitioning-overview.html
http://37signals.com/svn/posts/3571-scaling-your-database-via-innodb-table-compression
http://forge.mysql.com/wiki/MySQL_Internals
http://dev.mysql.com/doc/refman/5.7/en/storage-engines.html
http://dev.mysql.com/doc/refman/5.7/en/join.html
http://habrahabr.ru/post/130905/
http://habrahabr.ru/post/44807/
http://habrahabr.ru/post/108418/
http://habrahabr.ru/post/43336/ MySQL versus PostgreSQL
http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
Replication
http://habrahabr.ru/post/262925/
https://habrahabr.ru/company/oleg-bunin/blog/313594/
https://habrahabr.ru/company/oleg-bunin/blog/309326/
http://dev.mysql.com/doc/refman/5.6/en/replication.html
http://java.dzone.com/articles/mariadb-replication-maxscale
Percona Cluster
http://www.mysqlperformanceblog.com/2014/07/25/monitoring-flow-control-percona-xtradb-cluster-5-6/
Galera+Percona Cluster
http://matthewcasperson.blogspot.com/2013/07/setting-up-galera-cluster-in-centos-6.html
MariaDB 10
http://www.slideshare.net/izoratti/131111-maria-db10tutorial
https://mariadb.com/blog/mariadb-10-vs-mysql-56-feature-comparison-update
http://dz-media-source.zend.co
m/2013/zendcon/Sergei%20Golubchik%20-%20MariaDB-10.0%20What's%20New.pdf
http://habrahabr.ru/company/centosadmin/blog/227533/ MySQL backup
https://mariadb.com/kb/en/dynamic-columns/
InnoDB clustered index:
http://umumble.com/blogs/mysql/mysql-%28innodb%29-clustered-and-non_clustered-indexes-/
http://anothermysqldba.blogspot.com/2013/06/pivot-tables-example-in-mysql.html
https://github.com/shoma/mysqlmonitor/blob/master/src/mysqlstatus.py
http://code.openark.org/blog/mysql/bash-script-report-largest-innodb-files
http://geert.vanderkelen.org/refactored-poor-man%E2%80%99s-mysql-replication-monitoring/
http://www.openlogic.com/wazi/bid/272031/Replacing-MySQL-with-MariaDB
http://stackoverflow.com/questions/23023655/python-database-cursor-does-not-see-changes-in-database
Changing password:
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES;
http://habrahabr.ru/post/125428/ complex query
Calling stored procedure from python
http://geert.vanderkelen.org/2014/03/
Host '192.168.1.4' is not allowed to connect to this MySQL server
http://www.openlogic.com/wazi/bid/336655/How-to-securely-administer-MySQL-over-SSH
http://blog.wassupy.com/2011/02/host-is-not-allowed-to-connect-to-this.html
https://rtcamp.com/tutorials/mysql/remote-access/
mysql -h 192.168.1.8 -u root -pmypasswd:
ERROR 1130: Host '192.168.1.4' is not allowed to connect to this MySQL server
$ mysql -u root -pXXX
use mysql;
SELECT user,host FROM user;
GRANT ALL ON *.* to root@'192.168.1.4' IDENTIFIED BY 'your-root-password';
FLUSH PRIVILEGES;
If you want to allow all clients to connect you may do following
GRANT ALL ON *.* to root@’%’ IDENTIFIED BY ‘your-root-password’;
MySQL on Mac OX
/Applications/MAMP/Library/bin/mysqld
/Applications/XAMPP/xamppfiles/sbin/mysqld
/usr/local/mysql-5.6.16-osx10.7-x86_64/bin/mysqld
mlubinsky$ ps -ef | grep mysql
/bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/Michaels-MacBook-Pro-5.local.pid
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/Michaels-MacBook-Pro-5.local.err --pid-file=/usr/local/mysql/data/Michaels-MacBook-Pro-5.local.pid
Installing / Starting another instance of MySQL without MAMP
sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
pip install --allow-external mysql-connector-python mysql-connector-python
MySQL - attempt to install python driver
There are several drivers:
https://wiki.python.org/moin/MySQL
https://github.com/Benoss/PythonMysqlDriversTest/blob/master/compare.py
http://sourceforge.net/projects/mysql-python/files/mysql-python/
tar -xvzf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
Edit site.cfg on your favorite text editor and change the mysql path as below: mysql_config = /usr/local/mysql/bin/mysql_config
You are now ready to build and install MySQL-python:
sudo python setup.py build
sudo python setup.py install
Edit your ~/.bash_profile, add the following line:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
If you are already using MAMP you still have to follow the above MySQL installation process, as MAMP by default doesn’t include the required header files to build MySQL-python.
Moreover if you prefer MAMP, you don’t have to run mysql from /usr/local/mysql.
On your settings.py remember to set “127.0.0.1″ as your preferred db host in case that “localhost” doesn’t work.
Driver https://pypi.python.org/pypi/PyMySQL
pip install PyMySQL
>>> import MySQLdb
ImportError: No module named MySQLdb
MOST POPULAR DRIVER
pip install mysql-python # Failed with message
sh: mysql_config: command not found
find / -name mysql_config
/Applications/MAMP/Library/bin/mysql_config
/Applications/XAMPP/xamppfiles/bin/mysql_config
/usr/local/mysql-5.6.16-osx10.7-x86_64/bin/mysql_config
Attempt to fix - include folder with mysql_config:
export PATH=$PATH:/Applications/MAMP/Library/bin/
get new error:
mysql.c:44:10: fatal error: 'my_config.h' file not found
you need the dev headers for MySQL, which don't come with MAMP by default.
Install MySQL from scratch without using MAMP, do this
find / -name my_config.h
/Applications/XAMPP/xamppfiles/include/my_config.h
/usr/local/mysql-5.6.16-osx10.7-x86_64/include/my_config.h
export PATH=$PATH:/usr/local/mysql-5.6.16-osx10.7-x86_64/bin/
end finally pip install will work!!
it's possible to add the headers to the MAMP version. There are good instructions for doing so here: http://dreamconception.com/tech/how-to-install-mysqldb-mysql-python-on-mamp/
Make sure not to just copy-paste all the commands, they're a bit out of date so you'll need to change the version numbers in some of them.
This driver has least dependency: mysql-connector-python
Hide Copy Code
down vote
accepted
How to install mysql-connector-python without pip
git clone git@github.com:mysql/mysql-connector-python.git cd mysql-connector-python python setup.py install
http://dev.mysql.com/doc/connector-python/en/index.html
http://geert.vanderkelen.org/installing-coy-using-pip/
pip install mysql-connector-python <- fails, but the following work:
pip install --allow-external mysql-connector-python mysql-connector-python
import sys
import platform
print "* Python version: {} ".format(sys.version)
print "* Platform: {} ".format(platform.platform())
#from pprint import pprint as pp
#pp(sys.path)
try:
import mysql.connector
from mysql.connector import version
except ImportError as e:
sys.stderr.write("Error: failed to import mysql.connecgtor module ({})".format(e))
exit(1)
print "* mysql.connector version: {} url:{}".format(".".join(str(x) for x in version.VERSION[:3]), "http://dev.mysql.com/downloads/connector/python/")
try:
_host='localhost'
_user='root'
_pwd='root'
_db='information_schema'
_port=8889
con = mysql.connector.connect(port=_port, host=_host, user=_user, password=_pwd, database=_db);
cur = con.cursor()
cur.execute("SELECT VERSION()")
ver = cur.fetchone()
print "Database version : %s " % ver
except mysql.connector.Error, e:
print "=====Error:======== "
print e
#print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
#finally:
# if con:
# con.close()
import mysql.connector
con = mysql.connector.connect(host='localhost', user='root', password='root', port=8889);
>>> dir (mysql.connector)
['BINARY', 'Binary', 'CNX_POOL_ARGS', 'CONNECTION_POOL_LOCK', 'CharacterSet', 'ClientFlag', 'Connect', 'DATETIME', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FieldFlag', 'FieldType', 'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLConnection', 'MySQLConnectionPool', 'NUMBER', 'NotSupportedError', 'OperationalError', 'PoolError', 'ProgrammingError', 'ROWID', 'RefreshOption', 'STRING', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'Warning', '_CONNECTION_POOLS', '__all__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__version__', '__version_info__', 'apilevel', 'connect', 'connection', 'constants', 'conversion', 'cursor', 'custom_error_exception', 'dbapi', 'errorcode', 'errors', 'generate_pool_name', 'locales', 'network', 'paramstyle', 'pooling', 'protocol', 'threadsafety', 'utils', 'version']
>>> dir(con)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_autocommit', '_buffered', '_charset_id', '_client_flags', '_client_host', '_client_port', '_connection_timeout', '_converter_class', '_database', '_do_auth', '_do_handshake', '_execute_query', '_force_ipv6', '_get_connection', '_get_getwarnings', '_get_raise_on_warnings', '_get_self', '_get_unread_result', '_get_warnings', '_handle_binary_ok', '_handle_binary_result', '_handle_eof', '_handle_load_data_infile', '_handle_ok', '_handle_result', '_handle_server_status', '_handshake', '_have_next_result', '_host', '_in_transaction', '_info_query', '_open_connection', '_password', '_port', '_post_connection', '_prepared_statements', '_protocol', '_raise_on_warnings', '_raw', '_send_cmd', '_send_data', '_server_version', '_set_getwarnings', '_set_raise_on_warnings', '_set_unread_result', '_socket', '_sql_mode', '_ssl', '_time_zone', '_unix_socket', '_unread_result', '_use_unicode', '_user', 'autocommit', 'charset', 'close', 'cmd_change_user', 'cmd_debug', 'cmd_init_db', 'cmd_ping', 'cmd_process_info', 'cmd_process_kill', 'cmd_query', 'cmd_query_iter', 'cmd_quit', 'cmd_refresh', 'cmd_shutdown', 'cmd_statistics', 'cmd_stmt_close', 'cmd_stmt_execute', 'cmd_stmt_prepare', 'cmd_stmt_reset', 'cmd_stmt_send_long_data', 'collation', 'commit', 'config', 'connect', 'connection_id', 'converter', 'cursor', 'database', 'disconnect', 'get_autocommit', 'get_database', 'get_row', 'get_rows', 'get_server_info', 'get_server_version', 'get_sql_mode', 'get_time_zone', 'get_warnings', 'in_transaction', 'is_connected', 'isset_client_flag', 'ping', 'python_charset', 'raise_on_warnings', 'reconnect', 'rollback', 'server_host', 'server_port', 'set_autocommit', 'set_charset_collation', 'set_client_flags', 'set_converter_class', 'set_database', 'set_login', 'set_sql_mode', 'set_time_zone', 'set_unicode', 'sql_mode', 'start_transaction', 'time_zone', 'unix_socket', 'unread_result', 'user']
>>> dir(_cur)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_binary', '_connection', '_description', '_execute_iter', '_executed', '_executed_list', '_fetch_row', '_fetch_warnings', '_handle_eof', '_handle_noresultset', '_handle_result', '_handle_resultset', '_have_unread_result', '_last_insert_id', '_lastrowid', '_nextrow', '_process_params', '_process_params_dict', '_reset_result', '_row_to_python', '_rowcount', '_set_connection', '_stored_results', '_warning_count', '_warnings', 'arraysize', 'callproc', 'close',
'column_names', 'description', 'execute', 'executemany', 'fetchall', 'fetchmany', 'fetchone', 'fetchwarnings', 'getlastrowid', 'lastrowid', 'next', 'nextset', 'reset', 'rowcount', 'setinputsizes', 'setoutputsize', 'statement', 'stored_results', 'with_rows']
Help on MySQLCursor in module mysql.connector.cursor object:
class MySQLCursor(CursorBase)
| Default cursor for interacting with MySQL
|
| This cursor will execute statements and handle the result. It will
| not automatically fetch all rows.
|
| MySQLCursor should be inherited whenever other functionallity is
| required. An example would to change the fetch* member functions
| to return dictionaries instead of lists of values.
|
| Implements the Python Database API Specification v2.0 (PEP-249)
|
| Method resolution order:
| MySQLCursor
| CursorBase
| __builtin__.object
|
| Methods defined here:
|
| __init__(self, connection=None)
|
| __iter__(self)
| Iteration over the result set which calls self.fetchone()
| and returns the next row.
|
| __str__(self)
| __unicode__(self)
|
| callproc(self, procname, args=())
| Calls a stored procedue with the given arguments
|
| The arguments will be set during this session, meaning
| they will be called like _<procname>__arg<nr> where
| <nr> is an enumeration (+1) of the arguments.
|
| Coding Example:
| 1) Definining the Stored Routine in MySQL:
| CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
| BEGIN
| SET pProd := pFac1 * pFac2;
| END
|
| 2) Executing in Python:
| args = (5,5,0) # 0 is to hold pprod
| cursor.callproc('multiply', args)
| print(cursor.fetchone())
|
| Does not return a value, but a result set will be
| available when the CALL-statement execute successfully.
| Raises exceptions when something is wrong.
|
| close(self)
| Close the cursor
|
| Returns True when successful, otherwise False.
execute(self, operation, params=None, multi=False)
| Executes the given operation
|
| Executes the given operation substituting any markers with
| the given parameters.
|
| For example, getting all rows where id is 5:
| cursor.execute("SELECT * FROM t1 WHERE id = %s", (5,))
|
| The multi argument should be set to True when executing multiple
| statements in one operation. If not set and multiple results are
| found, an InterfaceError will be raised.
|
| If warnings where generated, and connection.get_warnings is True, then
| self._warnings will be a list containing these warnings.
|
| Returns an iterator when multi is True, otherwise None.
|
| executemany(self, operation, seq_params)
| Execute the given operation multiple times
|
| The executemany() method will execute the operation iterating
| over the list of parameters in seq_params.
|
| Example: Inserting 3 new employees and their phone number
|
| data = [
| ('Jane','555-001'),
| ('Joe', '555-001'),
| ('John', '555-003')
| ]
| stmt = "INSERT INTO employees (name, phone) VALUES ('%s','%s')"
| cursor.executemany(stmt, data)
|
| INSERT statements are optimized by batching the data, that is
| using the MySQL multiple rows syntax.
|
| Results are discarded. If they are needed, consider looping over
| data using the execute() method.
|
| fetchall(self)
|
| fetchmany(self, size=None)
|
| fetchone(self)
| Returns next row of a query result set
|
| Returns a tuple or None.
|
| fetchwarnings(self)
| Returns warnings
|
| getlastrowid(self)
| Returns the value generated for an AUTO_INCREMENT column
|
| This method is kept for backward compatibility. Please use the
| property lastrowid instead.
| Returns a long value or None.
|
| next(self)
| Used for iterating over the result set. Calles self.fetchone()
| to get the next row.
|
| stored_results(self)
| Returns an iterator for stored results
|
| This method returns an iterator over results which are stored when
| callproc() is called. The iterator will provide MySQLCursorBuffered
| instances.
|
| Returns a iterator.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| column_names
| Returns column names
|
| This property returns the columns names as a tuple.
|
| Returns a tuple.
|
| statement
| Returns the executed statement
| This property returns the executed statement. When multiple
| statements were executed, the current statement in the iterator
| will be returned.
|
| with_rows
| Returns whether the cursor could have rows returned
|
| This property returns True when column descriptions are available
| and possibly also rows, which will need to be fetched.
|
| Returns True or False.
|
| ----------------------------------------------------------------------
| Methods inherited from CursorBase:
|
| nextset(self)
| Not Implemented.
|
| reset(self)
| Reset the cursor to default
|
| setinputsizes(self, sizes)
| Not Implemeted.
|
| setoutputsize(self, size, column=None)
| Not Implemeted.
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| description
| Returns description of columns in a result
|
| This property returns a list of tuples describing the columns in
| in a result set. A tuple is described as follows::
|
| (column_name,
| type,
| None,
| None,
| None,
| None,
| null_ok,
| column_flags) # Addition to PEP-249 specs
|
| Returns a list of tuples.
|
| lastrowid
| Returns the value generated for an AUTO_INCREMENT column
|
| Returns the value generated for an AUTO_INCREMENT column by
| the previous INSERT or UPDATE statement or None when there is
| no such value available.
|
| Returns a long value or None.
|
| rowcount
| Returns the number of rows produced or affected
|
| This property returns the number of rows produced by queries
| such as a SELECT, or affected rows when executing DML statements
| like INSERT or UPDATE.
|
| Note that for non-buffered cursors it is impossible to know the
| number of rows produced before having fetched them all. For those,
| the number of rows will be -1 right after execution, and
| incremented when fetching rows.
|
| Returns an integer.
SQLALCHEMY
http://stackoverflow.com/questions/14343740/flask-sqlalchemy-or-sqlalchemy
http://geert.vanderkelen.org/connectorpython-sqlalchemy/
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysqlconnector://root:root@localhost:8889/MACCOR'
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysqlconnector
https://stackoverflow.com/questions/22521103/using-mysql-connector-python-driver-in-sqlalchemy
using mysqldb driver gives another error
ImportError: dlopen(/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
FIX which works:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
otool -L /usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so
libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
OK, may be the issue in line above is what libmysqlclient has not path
/usr/local/mysql-5.6.16-osx10.7-x86_64/lib/libmysqlclient.18.dylib
sudo install_name_tool -change libmysqlclient.18.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib
/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so
otool -L /usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so: /Applications/MAMP/Library/lib/libmysqlclient.16.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
/Library/Python/2.7/site-packages/_mysql.so
Code example
http://alexharvey.eu/database/mysql/on-working-with-mysql-and-python/
http://zetcode.com/db/mysqlpython/
XAMPP
/Applications/XAMPP/xamppfiles/manager-osx.app START/STOP
$ pwd
/Applications/MAMP/Library/bin
mysql -u root -p // password: root
show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| performance_schema |
+------------------------+
$ ls mysql*
mysql mysql_upgrade mysqld_multi
mysql_client_test mysql_waitpid mysqld_safe
mysql_config mysql_zap mysqldump
mysql_convert_table_format mysqlaccess mysqldumpslow
mysql_find_rows mysqlaccess.conf mysqlhotcopy
mysql_fix_extensions mysqladmin mysqlimport
mysql_plugin mysqlbinlog mysqlshow
mysql_secure_installation mysqlbug mysqlslap
mysql_setpermission mysqlcheck mysqltest
mysql_tzinfo_to_sql mysqld
Michaels-MacBook-Pro-4:bin mlubinsky$ ./mysql --help
./mysql Ver 14.14 Distrib 5.5.33, for osx10.6 (i386) using EditLine wrapper
Copyright (c) 2000, 2013, 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.
Usage: ./mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
--auto-vertical-output
Automatically switch to vertical output mode if the
result is wider than the terminal width.
-B, --batch Don't use history file. Disable interactive behavior.
(Enables --silent.)
--character-sets-dir=name
Directory for character set files.
--column-type-info Display column type information.
-c, --comments Preserve comments. Send comments to the server. The
default is --skip-comments (discard comments), enable
with --comments.
-C, --compress Use compression in server/client protocol.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check Check memory and open file usage at exit.
-T, --debug-info Print some debug info at exit.
-D, --database=name Database to use.
--default-character-set=name
Set the default character set.
--delimiter=name Delimiter to be used.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
-e, --execute=name Execute command and quit. (Disables --force and history
file.)
-E, --vertical Print the output of a query (rows) vertically.
-f, --force Continue even if we get an SQL error.
-G, --named-commands
Enable named commands. Named commands mean this program's
internal commands; see mysql> help . When enabled, the
named commands can be used from any line of the query,
otherwise only from the first line, before an enter.
Disable with --disable-named-commands. This option is
disabled by default.
-i, --ignore-spaces Ignore space after function names.
--init-command=name SQL Command to execute when connecting to MySQL server.
Will automatically be re-executed when reconnecting.
--local-infile Enable/disable LOAD DATA LOCAL INFILE.
-b, --no-beep Turn off beep on error.
-h, --host=name Connect to host.
-H, --html Produce HTML output.
-X, --xml Produce XML output.
--line-numbers Write line numbers for errors.
(Defaults to on; use --skip-line-numbers to disable.)
-L, --skip-line-numbers
Don't write line number for errors.
-n, --unbuffered Flush buffer after each query.
--column-names Write column names in results.
(Defaults to on; use --skip-column-names to disable.)
-N, --skip-column-names
Don't write column names in results.
--sigint-ignore Ignore SIGINT (CTRL-C).
-o, --one-database Ignore statements except those that occur while the
default database is the one named at the command line.
--pager[=name] Pager to use to display results. If you don't supply an
option, the default pager is taken from your ENV variable
PAGER. Valid pagers are less, more, cat [> filename],
etc. See interactive help (\h) also. This option does not
work in batch mode. Disable with --disable-pager. This
option is disabled by default.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
--prompt=name Set the mysql prompt to this value.
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-q, --quick Don't cache result, print it row by row. This may slow
down the server if the output is suspended. Doesn't use
history file.
-r, --raw Write fields without conversion. Used with --batch.
--reconnect Reconnect if the connection is lost. Disable with
--disable-reconnect. This option is enabled by default.
(Defaults to on; use --skip-reconnect to disable.)
-s, --silent Be more silent. Print results with a tab as separator,
each row on new line.
-S, --socket=name The socket file to use for connection.
-t, --table Output in table format.
--tee=name Append everything into outfile. See interactive help (\h)
also. Does not work in batch mode. Disable with
--disable-tee. This option is disabled by default.
-u, --user=name User for login if not current user.
-U, --safe-updates Only allow UPDATE and DELETE that uses keys.
-U, --i-am-a-dummy Synonym for option --safe-updates, -U.
-v, --verbose Write more. (-v -v -v gives the table output format).
-V, --version Output version information and exit.
-w, --wait Wait and retry if connection is down.
--connect-timeout=# Number of seconds before connection timeout.
--max-allowed-packet=#
The maximum packet length to send to or receive from
server.
--net-buffer-length=#
The buffer size for TCP/IP and socket communication.
--select-limit=# Automatic limit for SELECT when using --safe-updates.
--max-join-size=# Automatic limit for rows in a join when using
--safe-updates.
--secure-auth Refuse client connecting to server if it uses old
(pre-4.1.1) protocol.
--server-arg=name Send embedded server this as a parameter.
--show-warnings Show warnings after every statement.
--plugin-dir=name Directory for client-side plugins.
--default-auth=name Default authentication client-side plugin to use.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /Applications/MAMP/conf/my.cnf ~/.my.cnf
<input type=number step=0.01 />
create database MACCOR;
use maccor;
use maccor;
CREATE TABLE MACCOR_STATE (station VARCHAR(5), channel SMALLINT, state VARCHAR(15), station_type VARCHAR(10), v SMALLINT, a SMALLINT, voltage FLOAT, capacity FLOAT );
ALTER TABLE MACCOR_STATE ADD PRIMARY KEY (station, channel);
drop table M001;
create table M001 (
CHANNEL int PRIMARY KEY,
TYPE varchar (10),
STATE varchar(10),
VOLTAGE float,
CAPACITY FLOAT
);
insert into M001 (CHANNEL, TYPE, STATE, VOLTAGE, CAPACITY) values (1, 'CELL_IOS_1', 'ACTIVE', 1.0, 10.0);
crosstab in MySQL
http://stackoverflow.com/questions/10569142/crosstab-in-mysql/11262856#11262856
select TYPE, max(if(STATE = 'ACTIVE', cnt, 0)) as ACTIVE, max(if(STATE = 'AVAILABLE', cnt, 0)) as AVAILABLE
from (select TYPE, STATE, COUNT(*) as cnt FROM M001 GROUP BY TYPE, STATE) as GGG GROUP BY TYPE
select state,
max(if(type = 'Cell_iOS', cnt, 0)) as CELL_iOS,
max(if(type = 'Pack_Mac', cnt, 0)) as Pac_Mac,
max(if(type = 'Cell_Mac', cnt, 0)) as Cell_Mac
from (select type, state, COUNT(*) as cnt FROM MACCOR_STATE
GROUP BY type, state
#union .. attempt to fill unexisting states with 0
#select ' ', state, 0 from dual ( select distinct type)
) as GGG
GROUP BY state
SELECT * FROM M001;
>cat routers.py
from flask import Flask, request, jsonify
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
#this works
app.config['SQLALCHEMY_DATABASE_URI'] = #'mysql://root:root@localhost/MACCOR'
'mysql+mysqlconnector://root:root@localhost/MACCOR'
class Maccor(db.Model):
__tablename__ = "M001"
CHANNEL = db.Column(db.Integer, primary_key = True)
TYPE = db.Column(db.String(10))
STATE = db.Column(db.String(10))
VOLTAGE = db.Column(db.Float(10))
CAPACITY = db.Column(db.Float(10))
@app.route('/M001/', methods=['GET'])
def M001():
if request.method =='GET':
results = Maccor.query.limit(10).offset(0).all()
json_results = []
for result in results:
print "In LOOP"
d = {
'CHANNEL' : result.CHANNEL,
'TYPE' : result.TYPE,
'STATE' : result.STATE,
'VOLTAGE' : result.VOLTAGE,
'CAPACITY': result.CAPACITY
}
json_results.append(d)
return jsonify(items=json_results)
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)