1. 下载mysql 安装包
mysql 安装包下载地址: https://dev.mysql.com/downloads/mysql/
2. mysql 部署
2.1 创建soft目录
1. 在根目录创建 soft 目录
cd /
mkdir soft
cd /soft
上传压缩包到此目录
2.2 进入压缩包存放目录
cd /soft
2.3 解压压缩包
tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
2.4 修改文件夹名称
mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql-8.0.32
2.5 检查并创建用户和用户组
查询用户是否存在
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
用户不存在, 执行下面两行即可
groupadd mysql
useradd -r -g mysql mysql
2.6 创建data和log文件夹
[root@localhost mysql-8.0.32]# mkdir /soft/mysql-8.0.32/data
[root@localhost mysql-8.0.32]# mkdir /soft/mysql-8.0.32/log
2.7 授权目录和用户
[root@localhost mysql-8.0.32]# cd /soft/
[root@localhost soft]# chown -R mysql:mysql mysql-8.0.32
2.8 编辑my.cnf 文件
vim /etc/my.cnf
将下面配置拷贝到文件中, 并保存
[client]
#客户端设置
port = 3306
socket = /soft/mysql-8.0.32/data/mysql.sock
default-character-set = utf8mb4
[mysqld]
#skip-grant-tables
user = mysql
port = 3306
socket = /soft/mysql-8.0.32/data/mysql.sock
server-id = 2
pid-file = /soft/mysql-8.0.32/data/mysql.pid
#安装目录
basedir = /soft/mysql-8.0.32/
#数据库存放目录
datadir = /soft/mysql-8.0.32/data/
secure_file_priv=''
#系统数据库编码设置,排序规则
character_set_server = utf8mb4
collation_server = utf8mb4_bin
back_log = 1024
explicit_defaults_for_timestamp = ON
lower_case_table_names = 1
# sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
default_password_lifetime = 0
# 开启bin——log
log-bin=mysql-bin
binlog-format=ROW
log_bin_trust_function_creators=1
log-error = /soft/mysql-8.0.32/log/mysqld.log
[client]
socket=/soft/geoam/data/mysql.sock
[mysqldump]
socket=/soft/geoam/data/mysql.sock
[mysqladmin]
socket=/soft/mysql-8.0.32/data/mysql.sock
2.9 安装并初始化
[root@localhost bin]# cd /soft/mysql-8.0.32/bin/
[root@localhost bin]# ./mysqld --user=mysql --basedir=/soft/mysql-8.0.32 --datadir=/soft/mysql-8.0.32/data/ --initialize
执行后查看mysql 日志, 查询es 当前密码
[root@localhost bin]# tail -f /soft/mysql-8.0.32/log/mysqld.log
2023-04-01T10:50:42.623750Z 0 [System] [MY-013169] [Server] /soft/mysql-8.0.32/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 15532
2023-04-01T10:50:42.631612Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-04-01T10:50:43.560087Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-04-01T10:50:44.807743Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xy*,eYZ!y1lV
查询日志, mysql 默认密码为: xy*,eYZ!y1lV
2.10 复制启动脚本到资源目录
如果原来存在, 直接覆盖
[root@localhost bin]# cd /soft/mysql-8.0.32
[root@localhost mysql-8.0.32]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
cp: overwrite ‘/etc/rc.d/init.d/mysqld’? y
[root@localhost mysql-8.0.32]#
2.11 增加mysqld服务控制脚本执行权限
[root@localhost mysql-8.0.32]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql-8.0.32]#
2.12 将mysqld服务加入到系统服务
[root@localhost mysql-8.0.32]# chkconfig --add mysqld
[root@localhost mysql-8.0.32]#
2.13 检查mysqld服务是否已经生效
root@localhost mysql-8.0.32]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost mysql-8.0.32]#
2.14 启动mysql
[root@localhost mysql-8.0.32]# service mysqld start
Starting MySQL.. SUCCESS!
[root@localhost mysql-8.0.32]#
启动失败的情况, 提示找不到mysql命令的话
ln -s /soft/mysql-8.0.32/bin/mysql /usr/bin
2.15 登录mysql
[root@localhost mysql-8.0.32]# mysql -u root -p
-bash: /bin/mysql: No such file or directory
[root@localhost mysql-8.0.32]# ln -s /soft/mysql-8.0.32/bin/mysql /usr/bin
ln: failed to create symbolic link ‘/usr/bin/mysql’: File exists
[root@localhost mysql-8.0.32]#
报错为 软连接已经存在了, 删除存在的软连接, 然后重新创建即可
[root@localhost bin]# rm /usr/bin/mysql
rm: remove symbolic link ‘/usr/bin/mysql’? y
[root@localhost bin]#
重新创建软连接, 然后连接mysql
[root@localhost mysql-8.0.32]# ln -s /soft/mysql-8.0.32/bin/mysql /usr/bin
[root@localhost mysql-8.0.32]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
2.16 修改密码 和 权限
修改 root 用户密码
alter user 'root'@'localhost' identified with mysql_native_password by 'xxxxx';
flush privileges;
设置root 用户外部访问权限
create user 'root'@'%' identified by 'xxxxx';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
使用root 用户连接, (关闭服务器防火墙, 或者开放3306 端口)
2.17 创建自定义用户
create user 'xxxxx'@'%' identified by 'xxxx';
CREATE DATABASE [IF NOT EXISTS] <数据库名>
grant all privileges on 数据库名.* to 'xxxxx'@'%' with grant option;
flush privileges;