Tag Archives: bash

Stash your mysql password in the .my.cnf file

Hopefully you are using usernames and passwords when accessing your MySQL databases. Security can make it hard to quickly jump in and start working in the MySQL shell if your password is quite complicated.

Here’s the solution:

create a ~/.my.cnf file and fill it with:

[client]
database=yourDBname
user=yourDBusername
password=yourSuperSecretPassword

Next, make sure that only you can read it:

chmod 600 ~/.my.cnf

Lastly, test it out:

user@host:~$ mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 65863
Server version: 5.5.31-0ubuntu0.12.04.2 (Ubuntu)

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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
+-----------------------+
| Tables_in_yourDBname |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
11 rows in set (0.00 sec)

mysql>

 

Voila! In there without having to worry about needing to look up your password.

Have your bash prompt tell you git information

While you can obviously type in “git status” to see what’s happening (git-wise) in a directory, wouldn’t it be nice if you could automagically see that info?

Well, you can. Just add this snippet to your ~/.bash_profile file:

[code lang=”bash”]
if [ -f /etc/bash_completion.d/git ]; then
. /etc/bash_completion.d/git
PS1=’\[\u@\h \e[33m\]\w\[\e[0m\] $(__git_ps1 " (%s)")\n\$ ‘
fi
[/code]

Or, if that file doesn’t exist, you can create it with all of this code:

[bash]
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
if [ -f /etc/bash_completion.d/git ]; then
. /etc/bash_completion.d/git
PS1=’\[\u@\h \e[33m\]\w\[\e[0m\] $(__git_ps1 " (%s)")\n\$ ‘
fi

####
# Stash away examples of other possibilities for the prompt
#####
# PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ‘
# PS1=’\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$’
# PS1='[\u@\h \W$(__git_ps1 " (%s)")] \[\e[33m\]\w\[\e[0m\]\n\$’
[/bash]


Update!

I tried this on an older server that didn’t have /etc/bash_completion.d on it, and here’s how I got it to work.

1) Downloaded the latest stable version of https://github.com/telemachus/bash_completion.d and copied that directory into /etc/bash_completion.d

2) Created a symbolic link:
[bash]
ln -s /etc/bash_completion.d/git-completion.bash /etc/bash_completion.d/git
[/bash]

3) If you get this error:
[bash]
-bash: __git_ps1: command not found
[/bash] then you will need to add this

[bash]
if [ -f /etc/bash_completion.d/git-prompt.bash ]; then
. /etc/bash_completion.d/git-prompt.bash
fi
[/bash]

before the call to . /etc/bash_completion.d/git

Changing the default editor from pico to vim

On new (or new to me systems) I find it annoying to use a program like git, or crontab -e and find myself in the pico editor.  🙁

So, here’s how to change it:

[bash]
sudo update-alternatives –config editor

There are 4 choices for the alternative editor (providing /usr/bin/editor).

Selection Path Priority Status
————————————————————
* 0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
3 /usr/bin/vim.basic 30 manual mode
4 /usr/bin/vim.tiny 10 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode.
[/bash]

and to confirm your selection, run the same command again, and check out the item that has the asterisk next to it:

[bash]
sudo update-alternatives –config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).

Selection Path Priority Status
————————————————————
0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
* 3 /usr/bin/vim.basic 30 manual mode
4 /usr/bin/vim.tiny 10 manual mode

Press enter to keep the current choice[*], or type selection number:
[/bash]

If that still doesn’t work, try this instead:

[bash]
export EDITOR=/usr/bin/vim.basic
[/bash]