{"id":1169,"date":"2020-03-20T21:58:05","date_gmt":"2020-03-20T21:58:05","guid":{"rendered":"https:\/\/tech.avant.net\/q\/?p=1169"},"modified":"2020-03-20T22:02:40","modified_gmt":"2020-03-20T22:02:40","slug":"mysql-cli","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/mysql-cli\/","title":{"rendered":"MySQL CLI"},"content":{"rendered":"\n<p>Typically, you would use mysql client cli as follows,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mysql -h some.random.hostname.com -P 3306 -u bob -D somedb -p<\/pre>\n\n\n\n<p>You could also create a<strong> ~\/.my.cnf<\/strong> file&nbsp;to set default properties, including connection info,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[client]\nprompt=\\\\u@\\\\d>\\\\_\nport = 3306\nhost = some.random.hostname.com\ndatabase = somedb\nuser = bob<\/pre>\n\n\n\n<p>You could even store a password but I wouldn&#8217;t recommend storing it in the clear. Also, the above approach is not useful when you have more than one MySQL database across multiple hosts, ports, etc.<\/p>\n\n\n\n<h2>Managing Multiple Connections<\/h2>\n\n\n\n<p>Unfortunately, mysql still doesn&#8217;t have an easy way to do this, but if you add the following to your <strong>~\/.bashrc<\/strong>,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function mysqle() { mysql --defaults-group-suffix=$1 --password \"${@:2}\"; }\n<\/pre>\n\n\n\n<p>You can then setup your <strong>~\/.my.cnf<\/strong> as follows,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[client]\nprompt=\\\\u@\\\\d>\\\\_\nport = 3306\n\n[clientsomedb]\nhost = some.random.hostname.com\ndatabase = somedb\nuser = bob\n\n[clientsuperdb]\nhost = some.other.random.hostname.com\nport = 63306\ndatabase = superdb\nuser = bob<\/pre>\n\n\n\n<p>As long as you prefix the section with &#8220;client&#8221;, then this will allow you to create as many different sections as you need (for different apps, different environments, whatever).<\/p>\n\n\n\n<p>To connect, just use the shell function from earlier,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mysqle superdb<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Typically, you would use mysql client cli as follows, You could also create a ~\/.my.cnf file&nbsp;to set default properties, including connection info, You could even store a password but I wouldn&#8217;t recommend storing it in the clear. Also, the above approach is not useful when you have more than one MySQL database across multiple hosts, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,15,14],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/1169"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":3,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1172,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1172"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}