原Linux系统已经安装了一个MySQL8.0版本,现在需要一个5.7版本的。为了区分两个版本,并且可以同时使用,安装时需要注意以下几点。
- 新建用户,使用新的环境变量
- 初始化数据库时,需要指定配置文件路径
1、创建新的用户
创建时指定用户的家目录
# useradd -d /data2/mysqlusr
# passwd mysqlusr
2、下载MySQL压缩包,创建数据目录等路径
mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
# su - mysqlusr
$ tar -zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
$ mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql-5.7.28
$ mkdir -p mysql-5.7.28/data
3、创建my.cnf文件
$ cd mysql-5.7.28
$ touch my.cnf
添加内容如下
[mysqld]
bind-address=0.0.0.0
port=33306
user=mysqlusr
basedir=/data2/mysqlusr/mysql-5.7.28
datadir=/data2/mysqlusr/mysql-5.7.28/data
socket=/tmp/mysql_33306.sock
log-error=/data2/mysqlusr/mysql-5.7.28/data/mysql.err
pid-file=/data2/mysqlusr/mysql-5.7.28/data/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
4、编辑环境变量
$ vi .bash_profile
$ source .bash_profile
export MYSQL_HOME=/data2/mysql33306/mysql-5.7.28
export PATH=$MYSQL_HOME/bin:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MYSQL_HOME/libexport PATH
5、初始化数据库
$ mysqld --defaults-file=$HOME/mysql-5.7.28/my.cnf --initialize --user=mysqlusr --basedir=/data2/mysqlusr/mysql-5.7.28/ --datadir=/data2/mysqlusr/mysql-5.7.28/data
root密码在/data2/mysqlusr/mysql-5.7.28/datamysql.err
2021-06-01T05:42:15.209510Z 1 [Note] A temporary password is generated for root@localhost: !;Rg?dPh6eMw
6、启动数据库
首先验证一下环境变量,是否是新安装的MySQL
$ whereis mysqld_safe
mysqld_safe: /data2/mysqlusr/mysql-5.7.28/bin/mysqld_safe
可以看到mysqld_safe是我们新建的版本
启动
$ nohup mysqld_safe --defaults-file=$HOME/mysql-5.7.28/my.cnf --log-error=$HOME/mysql-5.7.28/data/mysql.err &> $HOME/mysql-5.7.28/data/StartMysql.log &
7、登录数据库
$mysql -u root -P 33306 -h 127.0.0.1 -pwfasfwf_22!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 72
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>
首次操作需要修改root密码
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by 'htsse_werw!’;
mysql> flush privileges;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
停止
mysqladmin shutdown -uroot -p123456