第一种,配置文件分开设置
1、下载包:https://downloads.mysql.com/archives/community/
2、将软件包放置/usr/local目录下,解压包
xz -d mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
tar xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar
3、重命名软件包
4、新增用户名和组,授权目录
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql/
5、/opt目录新建mysql,各自创建新建目录
mkdir -pv /opt/mysql/{3306,3307}/{data,etc,pid,socket,log}
赋值目录权限
chown -R mysql:mysql /opt/mysql/
6、编辑各自配置文件 vi /opt/mysql/3306/etc/my.cnf ,权限设置 640,设置用户
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/opt/mysql/3306/data
socket=/opt/mysql/3306/socket/mysql.sock
log-error=/opt/mysql/3306/log/mysql.err
pid-file=/opt/mysql/3306/pid/mysql.pid
#character config
character_set_server=utf8mb4
sed 's/3306/3307/' /opt/mysql/3306/etc/my.cnf >/opt/mysql/3307/etc/my.cnf
chown mysql:mysql /opt/mysql/3306/etc/my.cnf
chown mysql:mysql /opt/mysql/3307/etc/my.cnf
7、切换bin下初始化基础
./mysqld --defaults-file=/opt/mysql/3306/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/opt/mysql/3306/data --initialize-insecure
查看cat /opt/mysql/3306/log/mysql.err
./mysqld --defaults-file=/opt/mysql/3307/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/opt/mysql/3307/data --initialize-insecure
./mysqld --initialize-insecure --user=mysql --defaults-file=/opt/mysql/3307/etc/my.cnf --datadir=/opt/mysql/3307/data --explicit_defaults_for_timestamp
--initialize:会生成随机密码
--initialize-insecure:初始化无密码
8、添加mysql服务到系统并设置权限
cp -a support-files/mysql.server /etc/init.d/mysql3306
cp -a support-files/mysql.server /etc/init.d/mysql3307
chmod 777 /etc/init.d/mysql3306
chmod 777 /etc/init.d/mysql3307
9、修改启动配置文件vi /etc/init.d/mysql3306 ,开头修改 basedir 、datadir 、新增conf ,修改文件中conf和执行脚本修改
_
mysqld_safe 新增参数:--defaults-file="$conf"
同修改3307
10、添加系统服务并启动数据库,查看数据库状态
chkconfig --add mysql3306
chkconfig --add mysql3307
启动服务
/etc/init.d/mysql3306 start
/etc/init.d/mysql3307 start
service mysql3306 start
service mysql3307 start
查看服务状态:
查看mysql进程
10、将服务添加到启动PATH目录下
ln -s /usr/local/mysql/bin/mysql /usr/bin/
11、登录数据库,免密登录
mysql -h 127.0.0.1 -uroot -p -S /opt/mysql/3306/socket/mysql.sock
mysql -h 127.0.0.1 -uroot -p -S /opt/mysql/3307/socket/mysql.sock
12、修改root密码
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
flush privileges;