3

After running apt-get update && apt-get upgrade this doesn't work anymore

When I type mysql in the command line this error is returned

-bash: mysql: command not found

If I run this it works until next login

alias mysql=/opt/mysql/server-5.6/bin/mysql
echo "alias mysql=/opt/mysql/server-5.6/bin/mysql" >> /root/.bashrc

I'm logging in via putty as root

/root/.bashrc

alias mysql=/opt/mysql/server-5.6/bin/mysql

/root/.profile

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n

/root/.bash_profile

#############################################################################

export PS1='\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]'
umask 022

#############################################################################

eval "`dircolors`"

#############################################################################

alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
alias ..='cd ..'
alias ...='cd ../..'
alias s='ssh -l root'

#############################################################################

export EDITOR="vim"

export HISTFILESIZE=99999999
export HISTSIZE=99999999
export HISTCONTROL="ignoreboth"

export LS_OPTIONS='--color=auto -h'

#############################################################################

After adding the alias in the command line you can run mysql but if you run a shell script the script returns the same error

restore.sh: 315: restore.sh: mysql: not found

update

# echo $SHELL
/bin/bash
  • Is the ~/profile file in /root or in your normal user's home directory? – terdon Sep 16 '14 at 15:07
  • It is root profile :) – clarkk Sep 16 '14 at 16:45
  • OK, so how do you log in? Do either /root/.bash_profile or /root/.bash_login exist? Is root's shell bash? – terdon Sep 16 '14 at 16:50
  • 1
    Aliases are normally only used by interactive shells, not scripts. So even if you solve the login problem, that won't fix restore.sh. You should solve this problem by putting the command in your PATH, not with aliases. – Barmar Sep 16 '14 at 18:52
2

There are a few possible problems here.

  1. You are not running as root. The alias is defined in root's ~/.bashrc so if you are running as another user, it will not be found. If this is the case, add the alias line to you user's ~/.bashrc or switch to root.

  2. You are running as root but you're not running bash. Log in as root and check the output of echo $SHELL. If that is not /bin/bash, use the chsh command to set it to bash.

  3. You are running as root but this is a login shell (for example if you've used ssh to log in). If the ~/profile you show is not in /root, then the .bashrc alias will not be loaded. Login shells ignore .bashrc which is why you have those lines in your .profile.

    If the .profile is root's, it could still be ignored if either /root/.bash_profile or /root/bash_login exist.


UPDATE

Now that you have given more information, I can tell you that the problem is actually 3. So, you're connecting over ssh which means that you are running a login shell. These shells don't read .bashrc which is why your .profile contains the little loop that calls .bashrc explicitly. However, if either ~/.bash_profile or ~/.bash_login exists, ~/.profile is ignored. So, because you also have ~/.bash_profile, ~/.profileis not read and since~/.bash_profiledoes not source~/.bashrc, your alias is not available. This is explained inman bash`:

When bash is invoked as an interactive login shell, or as a non-inter‐ active shell with the --login option, it first reads and executes com‐ mands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

So, the simplest solution would be to merge the files. Either add the lines from .profile to .bash_profile or vice versa. If you choose to add the lines from ~/.bash_profile to ~/.profile, make sure you delete~/.bash_profile` after merging.


Whatever the reason, the alias will never be available to your scripts. When running a script, bash is invoked as a non-interactive, non-login shell and both ~/.profile and .bashrc are ignored. Additionally, aliases are not made available to scripts unless you activate the expand_aliases option:

shopt -s expand_aliases

So, to make an alias work in your script you will need to i) manually read the file where that alias is defined (because ~/.bashrc is not read by scripts) and ii) enable the expand_aliases option.

For example:

#!/usr/bin/bash

## Make your aliases available to the script
shopt -s expand_aliases

## Read .bashrc
. ~/.bashrc

## Run your mysql command
mysql
  • It used to work.. But I think after running apt-get update && apt-get upgrad the aliases don't work anymore – clarkk Sep 16 '14 at 16:20
  • @clarkk the details will depend on exactly how you're logging in. So, please edit your question and tell us i) which user you are running this as ii) whose .profile you show iii) whether any of the files I mentioned in my 3d point exist iv) confirm that you're actually running bash and not /bin/sh which is the default shell for root AFAIK. – terdon Sep 16 '14 at 16:27
  • have updated the question.. i'm running /bin/bash and i'm logged in as root – clarkk Sep 16 '14 at 17:13
  • 1
    @clarkk please answer all my questions. Also, how are you logging in? Is this a graphical system? Are you logging in from the command line? Are you using ssh? These all can change the behavior of your shell. Also try running bash after you log in. That should read ~/.bashrc. If that works, let me know. – terdon Sep 16 '14 at 17:20
  • Is logging ind via putty (ssh).. what do you mean by running bash? When typing bash the console changes to a gray font – clarkk Sep 16 '14 at 19:08
0

You'll need to add mysql's bin to your system's $PATH. You can see which locations are in your $PATH variable by type echo $PATH. Programs in these locations are launchable by simply typing their name.

There is a section in the mysql docs about installing from binary that addresses adding mysql to your system's $PATH variable. The relevant text is:

**Update environment var /etc/profile    
    nano /etc/profile        
    export MYSQL_HOME=/opt/mysql/server-5.6        
    export PATH=$PATH:$MYSQL_HOME/bin        
    export MANPATH=$MANPATH:$MYSQL_HOME/man

You can read more here: http://dev.mysql.com/doc/refman/5.1/en/binary-installation.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.