1. 下载上传解压
1.1 下载
1.2 上传
$ rz
1.3 解压
$ tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
$ mv mysql-5.7.36-linux-glibc2.12-x86_64 /home/flying/program/mysql-5.7.36
2. 配置
2.1 使用root用户创建mysql组及mysql用户,并授权
$ groupadd mysql
$ useradd -g mysql mysql
更改mysql-5.7.36 文件夹所有者属性和对应权限
$ chown -R mysql:mysql /home/flying/program/mysql-5.7.36
$ chown -R mysql /home/flying/program/mysql-5.7.36
$ chmod -R 755 /home/flying/program/mysql-5.7.36
2.2 创建数据存放目录并授权
$ mkdir data
$ cd data
$ chown -R mysql:mysql /home/mysql/data
$ chown -R mysql /home/mysql/data
$ chmod -R 755 /home/mysql/data
2.3 配置my.cnf文件
$ vim /etc/my.cnf
[mysqld]
basedir=/home/flying/program/mysql-5.7.36
datadir=/home/mysql/data
socket=/var/lib/mysql/mysql.sock
character_set_server=utf8mb4
explicit_defaults_for_timestamp=true
3. 初始化mysql命令
$ ./mysqld --user=mysql --basedir=/home/flying/program/mysql-5.7.36 --datadir=/home/mysql/data --initialize
4. 将mysql加入服务并设置开机自启动
$ cp /home/flying/program/mysql-5.7.36/support-files/mysql.server /etc/init.d/mysql
$ chkconfig mysql on
5. 启动mysql
service mysql start
启动报错
Starting MySQL.2022-05-22T12:46:45.126162Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/home/mysql/data/host2100.pid).
错误修复
$ mkdir /var/log/mariadb
$ touch /var/log/mariadb/mariadb.log
$ chown -R mysql:mysql /var/log/mariadb/
重新启动出现socket异常
Starting MySQL.2022-05-22T12:50:59.338574Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists
修复socket
socket=/home/mysql/data/mysql.sock
再次重启,mysql成功启动
Starting MySQL. SUCCESS!
6. linux系统内mysql命令登录测试
6.1 登录异常,bash: mysql: 未找到命令
因为/usr/local/bin目录下缺失mysql导致,只需要一下方法建立软链接,即可以解决:
把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:
$ cd /usr/local/bin
$ ln -fs /MYSQLPATH/bin/mysql mysql
6.2 重新登录出现以下问题
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
修复问题
原因找不到mysql.sock,解决:只需在tmp文件夹下创建一个软连接即可解决
$ find / -name mysql.sock
/home/mysql/data/mysql.sock #找到路径
$ ln -s /home/mysql/data/mysql.sock /tmp/mysql.sock
6.3 重新登录,忘记初始密码,解决方案
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
在/etc/my.cnf中加入如下内容:在[mysqld]下添加skip-grant-tables,然后保存并退出
[mysqld]
skip-grant-tables #新增这一行
basedir=/home/flying/program/mysql-5.7.36
重启mysql服务,重新登录无密码登录
$ mysql -uroot -p
修改密码
mysql> use mysql
mysql> update user set authentication_string=password('123456') where user='root';
mysql> flush privileges;
mysql> exit;
密码修改后,最后把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql
[mysqld]
#skip-grant-tables #新增这一行
basedir=/home/flying/program/mysql-5.7.36
$ service mysql restart
$ mysql -uroot -p
输入新设置的密码登录成功
7. linux系统外通过客户端工具登录测试
7.1. 对外开放3306端口
$ firewall-cmd --permanent --add-port=3306/tcp
$ firewall-cmd --reload
7.2 通过客户端连接mysql
修改mysql root用户只允许本地连接为允许远程连接
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> flush privileges;
mysql> select user,host from user;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> flush privileges;
再次重新连接
至此,mysql数据库安装成功