{"id":79,"date":"2016-04-07T23:27:58","date_gmt":"2016-04-07T22:27:58","guid":{"rendered":"http:\/\/188.166.119.205\/?p=79"},"modified":"2026-11-10T05:52:02","modified_gmt":"2026-11-10T05:52:02","slug":"my-mysql-command-prompt","status":"publish","type":"post","link":"https:\/\/banyek.net\/?p=79&lang=en","title":{"rendered":"My MySQL command prompt"},"content":{"rendered":"<p>Earlier this week we had a discussion with fellow DBAs about our mysql prompts, and at the end of the day it showed up, that a lot of us hit the same problem.<\/p>\n<p>The problem is, that when you set up your mysql prompt then \u2018\\h\u2019 will be resolved to \u2018localhost\u2019 when you connect locally \u2013 instead the name of your host as you expect it. It always bugs me, and once I spent a good afternoon figuring out how to workaround this.<\/p>\n<p>Well, the workaround is not a big deal, because you can insert any text into your mysql prompt, and after you realise that you can do it, then it is easy: just put the hostname into your prompt with your chosen provisioning tool and that will do.<\/p>\n<p><!--more--><\/p>\n<p>For example in my puppet installations I use the following setting in the template file from I generate the \/etc\/my.cnf file:<\/p>\n<p><code>prompt = \"\\u@&lt;%= @fqdn %&gt; (\\h) [\\d]&gt; \"<\/code><\/p>\n<p>It says: \u201cusername@hostname (connected_host) [database_name]\u201d which is really informative. Let me show you this on live servers!<\/p>\n<p><code>[banyek@db-dev.bfc \/home\/banyek]# mysql -s<br \/>\nbanyek@db-dev.bfc.kinja-ops.com (localhost) [(none)]&gt; \\q<br \/>\n[banyek@db-dev.bfc \/home\/banyek]# mysql -s -h db-dev.xyz.kinja-ops.com<br \/>\nbanyek@db-dev.bfc.kinja-ops.com (db-dev.xyz.kinja-ops.com) [(none)]&gt; \\q<br \/>\n[banyek@db-dev.bfc \/home\/banyek]#<\/code><\/p>\n<p>You can see that looking at this prompt I am able to say that which username I used, which host I am running the mysql command, what server will I run my commands and in which database I am.<\/p>\n<p>There is also one more thing I want to show you, and that is colorising!<\/p>\n<p><a href=\"http:\/\/superuser.com\/questions\/397183\/mysql-command-line-color-prompt\">This is not my idea, so here\u2019s the link the superuser article where I\u2019ve read about that<\/a>, but the idea is simple, use the ANSI escape sequences to colorise your mysql prompt. This could not be done inside a \u2018prompt\u2019 setting, but you can to this as creating an alias and echo the whole mysql command line out \u2013 where you can use the terminal control sequences.<\/p>\n<p>For example here is an alias which you could check out:<br \/>\n<code>$ alias colormysql=$(echo -e 'mysql --prompt=\"\\x1B[31m\\\\u\\x1B[34m@\\x1B[32m\\\\h\\x1B[0m:\\x1B[36m\\\\d&gt;\\x1B[0m \"')<\/code><\/p>\n<p>But I am not really sure if this is generally a good idea. What do you think about that?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this week we had a discussion with fellow DBAs about our mysql prompts, and at the end of the day it showed up, that a lot of us hit the same problem. The problem is, that when you set up your mysql prompt then \u2018\\h\u2019 will be resolved to \u2018localhost\u2019 when you connect locally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[186,144,126,130],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-tech-en","tag-cli-en","tag-mysql-en","tag-oldblog-en","tag-work-env-en"],"_links":{"self":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":1,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}