安装MySQL在Linux环境下
1,上传源码
2,创建用户组
[root@openEuler-1 ~]# groupadd mysql
3,创建用户来运行数据库
[root@openEuler-1 ~]# useradd -r -g mysql -s /bin/false mysql
4,将压缩文件解压到/usr/local/中
[root@openEuler-1 ~]# tar xf mysql-8.0.36-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@openEuler-1 ~]# ll /usr/local/
total 44
drwxr-xr-x. 2 root root 4096 May 25 2024 bin
drwxr-xr-x. 2 root root 4096 May 25 2024 etc
drwxr-xr-x. 2 root root 4096 May 25 2024 games
drwxr-xr-x. 2 root root 4096 May 25 2024 include
drwxr-xr-x. 2 root root 4096 May 25 2024 lib
drwxr-xr-x. 3 root root 4096 Jan 10 19:30 lib64
drwxr-xr-x. 2 root root 4096 May 25 2024 libexec
drwxr-xr-x 9 root root 4096 Jan 14 21:06 mysql-8.0.36-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 4096 May 25 2024 sbin
drwxr-xr-x. 5 root root 4096 Jan 10 19:30 share
drwxr-xr-x. 2 root root 4096 May 25 2024 src
5,进行软连接
[root@openEuler-1 ~]# ln -sv /usr/local/mysql-8.0.36-linux-glibc2.12-x86_64/ /usr/local/mysql
'/usr/local/mysql' -> '/usr/local/mysql-8.0.36-linux-glibc2.12-x86_64/'
[root@openEuler-1 ~]# ll /usr/local/mysql/data/
total 0
6,进行初始化,这里需要记住登录密码
[root@openEuler-1 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data
2025-01-14T13:14:37.200025Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.36) initializing of server in progress as process 2113
2025-01-14T13:14:37.208018Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-01-14T13:14:37.750996Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-01-14T13:14:39.967122Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: TJZdqB_ue0U8
7,创建配置文件
[root@openEuler-1 ~]# vim /etc/my.cnf
[root@openEuler-1 ~]# mkdir /var/log/mysql
[root@openEuler-1 ~]# chown -R mysql.mysql /usr/local/mysql/*
[root@openEuler-1 ~]# cd /usr/local/mysql
[root@openEuler-1 mysql]# ls
[root@openEuler-1 mysql]# cd support-files/
[root@openEuler-1 support-files]# ls
mysqld_multi.server mysql-log-rotate mysql.server
8,将mysql.server 移动到/etc/init.d/mysqld
[root@openEuler-1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@openEuler-1 support-files]# ll /etc/init.d/mysql
mysqld mysql.server
[root@openEuler-1 support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Jan 14 21:29 /etc/init.d/mysqld
9,查看 /etc/rc.d/rc3.d/ 目录下的文件和目录的详细信息,包括文件类型、权限、所有者、所属组、大小、修改时间和文件名等。在Linux系统中, /etc/rc.d/rc3.d/ 目录通常用于存放系统在运行级别3(多用户模式)下启动的服务脚本的链接。
[root@openEuler-1 support-files]# ll /etc/rc.d/rc3.d/
total 0
10,将MySQL添加到系统服务列表中
[root@openEuler-1 support-files]# chkconfig --add mysqld
[root@openEuler-1 support-files]# ll /etc/rc.d/rc3.d/
total 0
lrwxrwxrwx 1 root root 16 Jan 14 21:30 S64mysqld -> ../init.d/mysqld
11,开机自启动
[root@openEuler-1 support-files]# chkconfig mysqld on
12,列出mysqld服务在各个运行级别下的启动状态
[root@openEuler-1 support-files]# 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
13,启动服务
[root@openEuler-1 ~]# systemctl start mysqld
14,查看服务状态
[root@openEuler-1 ~]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
Active: active (running) since Tue 2025-01-14 21:51:11 CST; 16s ago
Docs: man:systemd-sysv-generator(8)
Process: 3260 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
[root@openEuler-1 support-files]# yum install ncurses-compat-libs
15,登录mysql
[root@openEuler-1 support-files]# /usr/local/mysql/bin/mysql -uroot -p
16,修改密码
mysql> alter user root@localhost identified by '123456';
mysql> \q
17,编写脚本,使登录不需要输入完整路径
[root@openEuler-1 support-files]# vim /etc/profile.d/mysql.sh
18,使定义的环境变量生效
[root@openEuler-1 support-files]# source /etc/profile.d/mysql.sh
19,测试登录
[root@openEuler-1 support-files]# mysql -uroot -p123456