非root用户安装Mysql8.0

下载mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz包,上传至/usr/local目录下

新增mysql用户

su root
groupadd hbszdb
useradd -r -g hbszdb mysql
passwd mysql

一、解压安装

cd /usr/local
# 将压缩包的所有者授予mysql用户
chown -R mysql:hbszdb mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
chmod -R 750 mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
# 让mysql用户可以在/user/local中新建文件夹,从而可以解压上面的压缩包
chmod 757 /usr/local/
​
# 切换mysql用户,解压压缩包
su mysql
tar -Jxvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
​
# 解压缩后的文件夹名太长,修改下
mv mysql-8.0.27-linux-glibc2.12-x86_64 mysql8
​
# 将/user/local的权限改回来,默认是755
chmod 755 /usr/local/

二、配置环境变量

su root
vi /etc/profile
# 在文件最后插入
export PATH=$PATH:/usr/local/mysql8/bin
# 重新加载环境
source /etc/profile
# 验证
​
mysql
# 说明环境变量生效:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
​
​
​
# 如果mysql用户要验证,也要执行上面这句source

三、创建数据目录

su mysql
mkdir -p /usr/local/mysql8/data

四、添加配置文件

su root
touch /usr/local/etc/my.cnf
chown -R mysql:hbszdb /usr/local/etc/my.cnf
# 更改权限
chmod -R 750 /usr/local/etc/my.cnf
​
su mysql
vi /usr/local/etc/my.cnf

4.1 配置文件内容

[mysql]
# 默认字符集
default-character-set=utf8mb4
[client]
port       = 3306
socket     = /tmp/mysql.sock
[mysqld]
port       = 3306
server-id  = 3306
user       = mysql
socket     = /tmp/mysql.sock
# 安装目录
basedir    = /usr/local/mysql8
# 数据存放目录
datadir    = /usr/local/mysql8/data/mysql
log-bin    = /usr/local/mysql8/data/mysql/mysql-bin
innodb_data_home_dir      =/usr/local/mysql8/data/mysql
innodb_log_group_home_dir =/usr/local/mysql8/data/mysql
# 日志及进程数据的存放目录
log-error =/usr/local/mysql8/data/mysql/mysql.log
pid-file  =/usr/local/mysql8/data/mysql/mysql.pid
# 服务端字符集
character-set-server=utf8mb4
lower_case_table_names=1
autocommit =1
##### 以上涉及文件夹明,注意修改
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
#query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
binlog_format=mixed
binlog_expire_logs_seconds =864000
# 创建表时使用的默认存储引擎
default_storage_engine = InnoDB
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

五、初始化

su mysql
mysqld --defaults-file=/usr/local/etc/my.cnf --basedir=/usr/local/mysql8 --datadir=/usr/local/mysql8/data/mysql --user=mysql --initialize-insecure
  • --defaults-file:指定配置文件(要放在--initialize 前面)

  • --user: 指定用户

  • --basedir:指定安装目录

  • --datadir:指定初始化数据目录

  • --intialize-insecure:初始化无密码(否则生成随机密码)

    可能会报

    mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
    

    使用yum安装即可

    yum -y install libaio		
    

    六、安装

    su mysql
    /usr/local/mysql8/bin/mysqld_safe --defaults-file=/usr/local/etc/my.cnf &

6.1 确认是否启动

# 输出有很多,就启动成功
ps -ef|grep mysql
# 连一下mysql,应该可以直接连上,不用密码
mysql

修改密码

# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

七、将mysql注册为服务,并开机自启动

su root
vi /etc/systemd/system/mysql.service

粘贴如下内容

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=hbszdb
Type=notify
TimeoutSec=0
ExecStart=/usr/local/mysql8/bin/mysqld --defaults-file=/usr/local/etc/my.cnf $MYSQLD_OPTS

LimitNOFILE=65535
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false

重新加载配置文件

systemctl daemon-reload

设置开机自启

systemctl enable mysqld
相关推荐
cheri--16 小时前
adb install失败: INSTALL_PARSE_FAILED_NO_CERTIFICATES
adb
秋月霜风19 小时前
mariadb主从配置步骤
android·adb·mariadb
别不开心ovo19 小时前
【测试】ADB常用命令
adb
CCTV果冻爽3 天前
ADB ROOT开启流程
adb
WZF-Sang3 天前
【MySQL】数据类型【mysql当中各自经典的数据类型的学习和使用】
linux·数据库·sql·学习·mysql·adb
我命由我123453 天前
ADB 之 logcat 极简小抄(过滤日志、保存日志到文件)
android·运维·adb·android studio·安卓·运维开发·android-studio
不吃饭的猪3 天前
mysql一主2从部署
android·mysql·adb
lhxwished4 天前
MySQL
数据库·mysql·adb
CCTV果冻爽4 天前
USB开启ADB设置流程
adb