{"id":287,"date":"2009-09-04T18:37:24","date_gmt":"2009-09-05T02:37:24","guid":{"rendered":"http:\/\/jim-zimmerman.com\/blog\/?p=287"},"modified":"2022-07-13T16:09:42","modified_gmt":"2022-07-13T23:09:42","slug":"mysql-commands-2","status":"publish","type":"post","link":"https:\/\/jim-zimmerman.com\/?p=287","title":{"rendered":"MySQL Commands"},"content":{"rendered":"<p>I intend to add to this as I have time.<\/p>\n<p>To display the schema of a table:<\/p>\n<blockquote><p>mysql&gt; desc <em>tablename<\/em>;<\/p><\/blockquote>\n<p>To restore a database from backup:<\/p>\n<blockquote><p>mysql -u root -p<br \/>\nmysql&gt; create database <em>databasename<\/em>;<br \/>\nmysql&gt; use <em>databasename<\/em>;<br \/>\nmysql&gt; source <em>databasebackupfile<\/em>;<br \/>\nNote: I back my databases up to a file name of <em>databasename.sql<\/em>.<\/p><\/blockquote>\n<p>To update all records in a column of a table:<\/p>\n<blockquote><p>mysql&gt; update <em>tablename<\/em> set <em>columnname<\/em>=&#8217;<em>value<\/em>&#8216;;<\/p><\/blockquote>\n<p>To update one record in a column of a table.  Note: othercolumnname needs to be unique to the record.:<\/p>\n<blockquote><p>mysql&gt; update <em>tablename<\/em> set <em>columnname<\/em>=&#8217;<em>value<\/em>&#8216; where <em>othercolumnname<\/em>=`<em>somevalue<\/em>&#8216;;<\/p><\/blockquote>\n<p>To create an account:<\/p>\n<blockquote><p>GRANT ALL PRIVILEGES ON <em>databasename<\/em>.* TO &#8216;<em>username<\/em>&#8216;@&#8217;<em>hostname<\/em>&#8216; IDENTIFIED BY &#8216;<em>password<\/em>&#8216;;<br \/>\nGRANT ALL PRIVILEGES ON <em>databasename<\/em>.* TO &#8216;<em>username<\/em>&#8216;@&#8217;localhost&#8217; IDENTIFIED BY &#8216;<em>password<\/em>&#8216;;<br \/>\nFLUSH PRIVILEGES;<\/p><\/blockquote>\n<p>To make mysqld listen only localhost:<\/p>\n<blockquote><p>vi \/etc\/my.cnf<br \/>\n&#8230;<br \/>\nbind-address=127.0.0.1<br \/>\n&#8230;<\/p><\/blockquote>\n<p>To count the number of rows in a table:<\/p>\n<blockquote><p>SELECT COUNT(*) FROM tablename;<\/p><\/blockquote>\n<p>To remove formatting and heading from a SELECT statement and delimit using a TAB character:<\/p>\n<blockquote><p>mysql mydatabase -N -B -u myuser -p -e &#8220;myselect statement;&#8221;<\/p><\/blockquote>\n<p>How to convert timestamp to date from a SELECT statement:<\/p>\n<blockquote><p>SELECT column1,DATE_FORMAT(FROM_UNIXTIME(`column2`), &#8216;%b %e %Y&#8217;) AS &#8216;date_formatted&#8217; FROM tablename;<\/p><\/blockquote>\n<p>Here is a simpler example, where timestampcol is the name of date column you want to convert:<\/p>\n<blockquote><p>SELECT FROM_UNIXTIME(timestampcol) FROM <em>tablename<\/em>;<\/p><\/blockquote>\n<p>How to get the time an hour ago:<\/p>\n<blockquote><p>SELECT TIME(DATE_SUB(NOW(), INTERVAL 1 HOUR));<\/p><\/blockquote>\n<p>To access a MySql database from a script without having the password stored in the script:<br \/>\nAdd the information to the .my.cny file in the user&#8217;s home directory that the script will run under.<\/p>\n<p>Edit or create the file:<\/p>\n<blockquote><p># vi ~\/.my.cny<\/p><\/blockquote>\n<p>Protect the file:<\/p>\n<blockquote><p># chmod 600 ~\/.my.cny<\/p><\/blockquote>\n<p>Sample file contents:<\/p>\n<blockquote><p># cat ~\/.my.cny<br \/>\n[client]<br \/>\nuser=mysqlusername<br \/>\npassword=mysqlpassword<br \/>\ndatabase=defaultdatabase<\/p><\/blockquote>\n<p>Then, to use it:<\/p>\n<blockquote><p># mysql &lt; mysqlscript &gt; my.output<\/p><\/blockquote>\n<p>Display table information:<\/p>\n<blockquote><p>show table status from <em>databasename<\/em>;<\/p><\/blockquote>\n<p>How to backup one table:<\/p>\n<blockquote><p># mysqldump -u <em>mysqlusername<\/em> -h <em>mysqlhostname<\/em> -p <em>databasename<\/em> <em>tablename<\/em> &gt; <em>tablename<\/em>.sql<\/p><\/blockquote>\n<p>To search or output of a query.  This would also apply to a SELECT statement.:<br \/>\nSHOW TABLES LIKE &#8216;%<em>whatever<\/em>%&#8217;;<\/p>\n<p>To limit the number of rows:<br \/>\nSELECT * FROM <em>mytable<\/em>LIMIT 100;<\/p>\n<p>To sort output based on a particular column:<br \/>\nSELECT * FROM <em>mytable <\/em>ORDER BY <em>mycolumn <\/em>DESC;<\/p>\n<p>To get the size of a databse:<br \/>\nSELECT table_schema &#8220;Database Name&#8221;, SUM( data_length + index_length)\/1024\/1024<br \/>\n&#8220;Database Size (MB)&#8221; FROM information_schema.TABLES where table_schema = &#8216;<em>mydatabase<\/em>&#8216;;<\/p>\n<p>To get the size of all the tables in a database:<br \/>\nSELECT table_name &#8220;Table&#8221;, table_rows &#8220;Number of Rows&#8221;, round(((data_length + index_length)\/1024\/1024),2)<br \/>\n&#8220;Size of Table&#8221; FROM information_schema.TABLES WHERE table_schema = &#8220;<em>mydatabase<\/em>&#8220;;<\/p>\n<p>To allow an account to export data to a file:<br \/>\nGRANT FILE ON *.* TO &#8216;<em>username<\/em>&#8216;@&#8217;<em>hostname<\/em>&#8216;;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I intend to add to this as I have time. To display the schema of a table: mysql&gt; desc tablename; To restore a database from backup: mysql -u root -p mysql&gt; create database databasename; mysql&gt; use databasename; mysql&gt; source databasebackupfile; Note: I back my databases up to a file name of databasename.sql. To update all [&#038;hellip<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-documentation"],"share_on_mastodon":{"url":"","error":""},"_links":{"self":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":20,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/1899"}],"wp:attachment":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}