{"id":55,"date":"2012-03-01T23:53:39","date_gmt":"2012-03-02T04:53:39","guid":{"rendered":"http:\/\/en.latindevelopers.com\/ivancp\/?p=55"},"modified":"2012-03-01T23:55:20","modified_gmt":"2012-03-02T04:55:20","slug":"alias-shortcuts-to-mysql-cli","status":"publish","type":"post","link":"http:\/\/en.latindevelopers.com\/ivancp\/2012\/alias-shortcuts-to-mysql-cli\/","title":{"rendered":"Alias shortcuts to MySQL CLI"},"content":{"rendered":"<p>Do you get write laziness in the command line everything what you need to connect to a MySQL server every time?<\/p>\n<p>It may take less than minute, but sometimes one minute is vital (especially if we&#8217;re near the end of the world):<\/p>\n<pre lang=\"bash\">ivancp@ubuntu$ mysql -u root -p -h mysqlhost database<\/pre>\n<p>When we are hurry, these commands often fail several times per minute.<\/p>\n<p>The solution: we can create shortcuts with bash alias commands in file ~\/.bashrc :<\/p>\n<pre lang=\"bash\"># File ~ \/. Bashrc\r\n\r\n# Command \"my\" to connect to a local server\r\nalias my='mysql -u root -p'\r\n\r\n# Command \"my2\" to connect to a remote server\r\nalias my2='mysql -u root -h 192.168.1.56 -p'<\/pre>\n<p>Next time if you want to access the local server just type the command <code>my [database name]<\/code> , there only ask for database password. You can use any command aliases, I prefer\u00a0 &#8220;my&#8221; and &#8220;my2&#8221; they are short and useful.<\/p>\n<p>But if you have several servers comes another problem, how to know in which server I&#8217;m?<\/p>\n<p>Open a mysql-cli can be super fast with alias shortcuts, but all terminals have the same default prompt: <code>mysql&gt;<\/code>\u00a0 To avoid disasters (ex. run DROP in wrong place) you can change mysql-cli prompt with option <code>--prompt<\/code> , then finally our .bashrc look like this:<\/p>\n<pre lang=\"bash\">#improved ~\/.bashrc \r\n\r\n# Command \"my\" to connect to a local server\r\nalias my='mysql -u root --password=secret --prompt=\"local> \"'\r\n\r\n# Command \"my2\" to connect to a remote server\r\nalias my2='mysql -u root --password=secret  -h 192.168.1.56 --prompt=\"server 1> \"'<\/pre>\n<p>Note I added <code>--password<\/code> parameter, it can be dangerous, use it under your own risk.<\/p>\n<p>Enjoy!<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/alias-shortcuts-to-mysql-cli\/\" data-text=\"Alias shortcuts to MySQL CLI\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/alias-shortcuts-to-mysql-cli\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Do you get write laziness in the command line everything what you need to connect to a MySQL server every time? It may take less than minute, but sometimes one minute is vital (especially if we&#8217;re near the end of the world): ivancp@ubuntu$ mysql -u root -p -h mysqlhost database When we are hurry, these [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/alias-shortcuts-to-mysql-cli\/\" data-text=\"Alias shortcuts to MySQL CLI\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/alias-shortcuts-to-mysql-cli\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":21,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4,8],"tags":[15,5,14],"jetpack_featured_media_url":"http:\/\/en.latindevelopers.com\/ivancp\/wp-content\/uploads\/2012\/02\/logo-mysql-170x115.png","jetpack_shortlink":"https:\/\/wp.me\/p2f09V-T","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/55"}],"collection":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":3,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/55\/revisions\/58"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media\/21"}],"wp:attachment":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}