在openEuler-23.09上安装MySQL8.4.0
一、MySQL数据库服务环境搭建
操作系统版本
openEuler-23.09-x86_64-dvd.iso ,安装步骤此处省略。。。
MySQL8.4.0下载地址
https://dev.mysql.com/downloads/mysql/
1.1、下载及上传mysql二进制安装包
上传mysql-8.4.0-linux-glibc2.17-x86_64.tar.xz到soft目录
ls -lsa /soft
1.2、关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
1.3、关闭SELINUX防火墙
vi /etc/selinux/config
将内容
SELINUX=enforcing
更改为
SELINUX=disabled
1.4、创建用户及用户组
1.4.1、创建myql的HOME目录
mkdir -p /home/mysql
1.4.2、创建mysql组
groupadd mysql
1.4.3、创建mysql用户,并指定组和默认路径
useradd -r -d /home/mysql -g mysql mysql
1.4.4、将Mysql默认路径的用户和组改成mysql
chown -R mysql:mysql /home/mysql
1.5、解压mysql到/usr/local
cd /soft
xz -d mysql-8.4.0-linux-glibc2.17-x86_64.tar.xz
tar xvf mysql-8.4.0-linux-glibc2.17-x86_64.tar -C /usr/local
cd /usr/local
mv mysql-8.4.0-linux-glibc2.17-x86_64 mysql
1.6、编辑环境变量增加MySQL HOME路径和登陆提示
vi ~/.bash_profile
PATH=$PATH:/usr/local/mysql/bin:$HOME/bin
export MYSQL_PS1="(\u@\h:\p)[\d]>"
source ~/.bash_profile
二、安装配置MySQL
2.1、MySQL -V 检查版本
[root@openEuler local]# mysql -V
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
使用find命令在系统查找libncurses.so.5 文件
[root@openEuler local]# find / -name libncurses.so.5
[root@openEuler local]# find / -name libncurses.so*
/usr/lib64/libncurses.so.6
/usr/lib64/libncurses.so.6.4
/usr/lib64/libncurses.so
设置软连接libncurses.so.5
[root@openEuler local]# ln -sf /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
[root@openEuler local]# find / -name libncurses.so.5
/usr/lib64/libncurses.so.5
检查版本
[root@openEuler local]# mysql -V
mysql Ver 8.4.0 for Linux on x86_64 (MySQL Community Server - GPL)
2.2、创建mysql数据目录
mkdir -p /mysql_data/3306/data
chown -R mysql:mysql /mysql_data
chmod -R 750 /mysql_data
2.3、编辑my.cnf
vi /etc/my.cnf
[mysqld]
server-id=802393306
port=3306
basedir=/usr/local/mysql
datadir=/mysql_data/3306/data
log-error=/mysql_data/3306/data/error.log
socket=/tmp/mysql.sock
pid-file=/mysql_data/3306/data/mysql.pid
character-set-server=utf8mb4
lower_case_table_names=1
innodb_log_file_size=1G
default-storage-engine=INNODB
mysql_native_password=ON
[mysql]
prompt=(\\u@\\h)[\\d]>\\_
[client]
port=3306
default-character-set=utf8mb4
2.4、初始化数据库
mysqld --initialize --user mysql
[root@openEuler local]# mysqld --initialize --user mysql
-- 初始化完成检查错误日志
cat /mysql_data/3306/data/error.log
-- 检查数据目录下文件
cd /mysql_data/3306/data
ls -lst
2.5、设置开机启动
启动并检查状态
[root@openEuler local]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.. SUCCESS!
[root@openEuler local]# /usr/local/mysql/support-files/mysql.server status
SUCCESS! MySQL running (36869)
设置开机启动
[root@openEuler mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@openEuler mysql]# chkconfig --list
[root@openEuler mysql]# chkconfig --add mysqld
[root@openEuler mysql]# chkconfig --list
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@openEuler ~]# service mysqld status
SUCCESS! MySQL running (10724)
[root@openEuler ~]# service mysqld stop
Shutting down MySQL. SUCCESS!
[root@openEuler ~]# service mysqld start
Starting MySQL. SUCCESS!
2.5、设置用户密码
2.5.1、设置用户’root’@'localhost’密码
[root@openEuler mysql]# cat /mysql_data/3306/data/error.log |grep temporary
2022-08-29T07:41:00.038502Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: sNa1sks(DG,n
[root@openEuler mysql]# mysql -uroot -p
mysql> alter user 'root'@'localhost' identified by 'Root@2024';
Query OK, 0 rows affected (0.00 sec)
2.5.2、创建用户’root’@‘%’ 并授权所有权限
create user 'root'@'%' identified by 'Root@2024';
grant all privileges on *.* to 'root'@'%' ;
flush privileges;
三、创建数据库及数据表
3.1、创建数据库superdb
mysql -uroot -p -h192.168.80.239
create database superdb;
use superdb;
3.2、数据表及插入数据
create table dept
( deptno int unsigned auto_increment primary key COMMENT '部门编号',
dname varchar(15) COMMENT '部门名称' ,
loc varchar(50) COMMENT '部门所在位置'
)engine = InnoDB DEFAULT charset=utf8mb4 COMMENT='员工部门表';
create table emp(
empno int unsigned auto_increment primary key COMMENT '雇员编号',
ename varchar(15) COMMENT '雇员姓名' ,
job varchar(10) COMMENT '雇员职位' ,
mgr int unsigned COMMENT '雇员对应的领导的编号',
hiredate date COMMENT '雇员的雇佣日期' ,
sal decimal(7,2) COMMENT '雇员的基本工资' ,
comm decimal(7,2) COMMENT '奖金' ,
deptno int unsigned COMMENT '所在部门' ,
foreign key(deptno) references dept(deptno)
)engine = innodb default charset =utf8mb4 comment='雇员信息表';
create table salgrade
(
grade int COMMENT '工资等级',
losal int COMMENT '此等级的最低工资',
hisal int COMMENT '此等级的最高工资'
)engine=innodb DEFAULT charset=utf8mb4 COMMENT='工资等级表';
create table bonus
( ename varchar(10) COMMENT '雇员姓名',
job varchar(9) COMMENT '雇员职位',
sal decimal(7,2) COMMENT '雇员工资',
comm decimal(7,2) COMMENT '雇员奖金'
)engine=innodb DEFAULT charset=utf8mb4 COMMENT='雇员奖金表' ;
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO dept VALUES (20,'RESEARCH','DALLAS');
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON');
INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES (7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,30);
INSERT INTO emp VALUES (7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,30);
INSERT INTO emp VALUES (7566,'JONES','MANAGER',7839,'1981-4-2',2975,NULL,20);
INSERT INTO emp VALUES (7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,30);
INSERT INTO emp VALUES (7698,'BLAKE','MANAGER',7839,'1981-5-1',2850,NULL,30);
INSERT INTO emp VALUES (7782,'CLARK','MANAGER',7839,'1981-6-9',2450,NULL,10);
INSERT INTO emp VALUES (7788,'SCOTT','ANALYST',7566,'87-7-13',3000,NULL,20);
INSERT INTO emp VALUES (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES (7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,30);
INSERT INTO emp VALUES (7876,'ADAMS','CLERK',7788,'87-7-13',1100,NULL,20);
INSERT INTO emp VALUES (7900,'JAMES','CLERK',7698,'1981-12-3',950,NULL,30);
INSERT INTO emp VALUES (7902,'FORD','ANALYST',7566,'1981-12-3',3000,NULL,20);
INSERT INTO emp VALUES (7934,'MILLER','CLERK',7782,'1982-1-23',1300,NULL,10);
INSERT INTO salgrade VALUES (1,700,1200);
INSERT INTO salgrade VALUES (2,1201,1400);
INSERT INTO salgrade VALUES (3,1401,2000);
INSERT INTO salgrade VALUES (4,2001,3000);
INSERT INTO salgrade VALUES (5,3001,9999);
select empno,ename,job,deptno,sal,row_number() over(partition by deptno order by sal desc) Rn from emp;
命名 status 和 \s 等价 都是查看数据库状态信息
-- 执行 status 命令
(root@localhost)[superdb]> status
--------------
mysql Ver 8.4.0 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 9
Current database: superdb
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.4.0 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /tmp/mysql.sock
Binary data as: Hexadecimal
Uptime: 1 min 58 sec
Threads: 2 Questions: 14 Slow queries: 0 Opens: 164 Flush tables: 3 Open tables: 83 Queries per second avg: 0.118
--------------
-- 执行 \s 命令
root@localhost)[superdb]> \s
--------------
mysql Ver 8.4.0 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 10
Current database: superdb
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.4.0 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /tmp/mysql.sock
Binary data as: Hexadecimal
Uptime: 34 min 7 sec
Threads: 2 Questions: 27 Slow queries: 0 Opens: 175 Flush tables: 3 Open tables: 94 Queries per second avg: 0.013
--------------