To add MySQL's login command to an environment variable and create an alias for it, follow these steps:
1. Open a terminal and edit the `/etc/profile` file (use global settings for all users)
vim /etc/profile
2. Add the following lines at the end of the file to set environment variables and aliases
# 将 "/usr/local/mysql" 替换为您的 MySQL 安装路径
export MYSQL_HOME=/usr/local/mysql57 # 将 "/usr/local/mysql57" 替换为您的 MySQL 安装路径
export PATH=$PATH:$MYSQL_HOME/bin
# 如果使用了免密安装,则将 -p123456 参数去掉
# mysql57 为别名
alias mysql57='/usr/local/mysql57/bin/mysql -h127.0.0.1 -uroot mysql -p123456 -P3306'
/* -------------------------------------------------------- */
# 假设机器上有两个mysql:5.7 和 8.0,下面分别为他们起别名
export MYSQL_HOME=/usr/local/mysql57
export PATH=$PATH:$MYSQL_HOME/bin
alias mysql57start='/usr/local/mysql57/bin/mysqld_safe --defaults-file=/etc/my57.cnf --user=mysql57 --port=3306 &'
alias mysql57='/usr/local/mysql57/bin/mysql -h127.0.0.1 -uroot mysql -p123456 -P3306'
export MYSQL_HOME=/usr/local/mysql80
export PATH=$PATH:$MYSQL_HOME/bin
alias mysql80start='/usr/local/mysql80/bin/mysqld_safe --defaults-file=/etc/my80.cnf --user=mysql80 --port=3307 &'
alias mysql80='/usr/local/mysql80/bin/mysql -h127.0.0.1 -uroot mysql -p123456 -P3307'
3. Save and close the file
4. Use the following command to make the changes effective
source /etc/profile
5. Start MySQL
mysql80start
6. Quick login
Now you can use the `mysql57` command to log into MySQL
mysql80
The login results are as follows:
Take starting and logging in to mysql80 as an example
By adding the MySQL login command to the environment variable and creating an alias, you can use customized commands to log in to the MySQL database more conveniently.