进入网站,下载MySQL二进制文件MySQLhttps://www.mysql.com/
1、清理安装环境:
# yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
# userdel -r mysql
# rm -rf /etc/my*
# rm -rf /var/lib/mysql
2、下载二进制文件
3、创建用户,用于稍后的文件属组,防止发生权限问题
[root@mysql-server ~]# useradd -r mysql -M -s /sbin/nologin
4、创建mysql目录
[root@mysql-server ~]# mkdir -p /opt/xiaolu/{data,mysql,log}
#创建目录位置可自定义
5、解压 压缩文件
[root@localhost opt]# tar xzvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
#理论上可以将该文件解压到任意位置,但在上一步我们已经创建了指定的文件位置,所以将解压后的文件内容移动到/opt/liuyh/mysql中
[root@localhost opt]#mv mysql-5.7.26-linux-glibc2.12-x86_64/* /opt/xiaolu/mysql
6、创建系统指定命令软连接,保证linux可以准确找到mysql
[root@localhost opt]# ln -s /opt/xiaolu/mysql/bin/mysql /usr/bin
7、将上述所创建文件夹用户改为mysql
[root@localhost mysql]# chown -R mysql:mysql /opt/xiaolu/{mysql,data,log}
8、配置参数
#创建并配置/etc/my.cnf
[root@localhost etc]# vi /etc/my.cnf
#内容为
[mysqld]
bind-address=0.0.0.0 # 任意地址都可以访问
port=3306 # 端口号
user=mysql
basedir=/opt/xiaolu/mysql #设置安装目录
datadir=/opt/xiaolu/data/mysql #设置数据存放目录
socket=/tmp/mysql.sock
log-error=/opt/xiaolu/log/mysql.err
pid-file=/opt/xiaolu/data/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
plugin-load=validate_password.so
validate-password=ON #开启密码复杂度校验
9、初始化mysql
#进入mysql的bin目录
[root@localhost bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/opt/xiaolu/mysql/ --datadir=/opt/xiaolu/data/mysql/ --user=mysql --initialize
10、查看临时密码(文件内容最后root@localhost:密码)
#临时密码被保存在data/mysql中的mysql.err文件中
[root@localhost bin]# cat /opt/xiaolu/data/mysql/mysql.err
2023-11-12T10:17:27.211163Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-11-12T10:17:27.414458Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-11-12T10:17:27.453012Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-11-12T10:17:27.523849Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ae308415-8144-11ee-a08a-000c2936dde3.
2023-11-12T10:17:27.524201Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-11-12T10:17:27.524567Z 1 [Note] A temporary password is generated for root@localhost: Jsdrf(&Gr4zR
11、启动mysql
#启动时可能会出现权限问题导致启动失败,所以先开发一些权限,如下
[root@localhost bin]# cp /opt/xiaolu/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost bin]# chmod 644 /etc/my.cnf
[root@localhost bin]# chmod +x /etc/init.d/mysqld
#启动mysql
[root@localhost bin]# service mysqld start
Starting MySQL. SUCCESS! #启动成功,可以查询进程确定
#关闭mysql
[root@localhost bin]# service mysqld stop
12、登录mysql
[root@localhost bin]# mysql -u root -p
Enter password:
#输入刚才获取的临时密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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>
#此处登陆成功
13、修改密码
#第一次登陆后必须先修改密码之后才能进行其他操作
mysql> set password = password('Ghl@1234'); #注意:该命令后必须添加分号
Query OK, 0 rows affected, 1 warning (0.00 sec) #出现此消息证明密码修改成功,
可尝试使用新密码重新登录
14、开启远程链接
#mysql默认只能本机登录,这不符合实际,需要修改
mysql> use mysql; #命令1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user = 'root'; #命令2
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#执行如上两条命令后开启mysql远程访问
mysql> flush privileges; #命令3
Query OK, 0 rows affected (0.00 sec)
#执行命令3立刻刷新mysql,保证命令1命令2执行,如此在保证本机linux防护墙开启状态下mysql可被外界访问
#注意: 在mysql中每一条命令后都要加分号
15、关闭密码复杂度校验,工作时不要使用!!
#修改/etc/my.cnf
validate-password=OFF #关闭密码复杂度校验
#重启mysql,可将密码修改为简答密码
16、设置mysql开机自启
[root@localhost ~]# chkconfig --add mysqld #命令1,将mysql添加到开机自启项
[root@localhost ~]# chkconfig --list #命令2,查看
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关 #此处3,4,5为开则设置成功
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
17、用navicat远程连接也可以