linux安装MySQL5.7

安装MySQL5.7

自查有没有mysql

[root@My-Server ~]# rpm -qa | grep mysql

如果有则清理干净在安装

[root@My-Server ~]# whereis mysql

找到文件夹目录,再把它删除。

[root@My-Server ~]#rpm -e --nodeps mysql-xxxx

检查系统是否自带mariadb,

[root@My-Server ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64

有则需要把它卸载掉,因为会和Mysql引起冲突

[root@My-Server ~]# rpm -e --nodeps mariadb-libs
警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsave
[root@My-Server ~]# rpm -qa | grep mariadb

解压

[root@My-Server local]# tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

修改名称

[root@My-Server local]# mv mysql-5.7.36-linux-glibc2.12-x86_64/ mysql

创建属组

[root@My-Server local]# groupadd mysql
[root@My-Server local]# useradd -r -g mysql mysql
[root@My-Server local]# chown -R mysql:mysql mysql
[root@My-Server local]# chown -R 775 mysql

创建data用于保存数据

[root@My-Server local]# cd mysql/
[root@My-Server mysql]# mkdir data

执行初始化命令

[root@My-Server mysql]# cd bin/
[root@SQL-Server bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2022-07-23T02:08:48.796039Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-23T02:08:48.929499Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-23T02:08:48.967462Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-23T02:08:49.036866Z 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: 63f7ae0d-0a2c-11ed-8598-000c298d2e7c.
2022-07-23T02:08:49.038071Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-23T02:08:49.295775Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-23T02:08:49.295805Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-23T02:08:49.296408Z 0 [Warning] CA certificate ca.pem is self signed.

生成随机验证码

记得记录后续登一次登录需要使用

2022-07-23T02:08:49.375844Z 1 [Note] A temporary password is generated for root@localhost: f:ege3PEwp5I

修改mysql.server的环境变量路径

[root@My-Server bin]# vi /usr/local/mysql/support-files/mysql.server

linux安装MySQL5.7_第1张图片

建立软连接

[root@My-Server bin]# cd ../
[root@My-Server mysql]# cd ../
[root@My-Server local]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

查看是否成功

[root@My-Server local]# ll /etc/init.d/mysql
lrwxrwxrwx. 1 root root 43 7月  22 22:06 /etc/init.d/mysql -> /usr/local/mysql/support-files/mysql.server

创建配置文件

[root@My-Server local]# vi /etc/my.cnf

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 作用是禁止域名解析:在mysql的授权表中就不能使用主机名了,只能使用IP
skip-name-resolve
# 设置3306端口
port = 3306
#设置远程访问ip
bind-address=0.0.0.0
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#设置查询操作等不区分大小写
lower_case_table_names=1

设置文件权限

[root@My-Server local]# chmod -R 775 /etc/my.cnf

配置环境变量

[root@Mysql-server support-files]# vim /etc/profile

文件最后添加内容


export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin:$MYSQL_HOME/lib

更新环境变量

[root@Mysql-server support-files]# source /etc/profile

启动MySQL服务

[root@My-Server local]# service mysql start
[root@My-Server ~]# service mysql status
 SUCCESS! MySQL running (4580)

进入mysql

[root@SQL-Server local]# 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.36

Copyright (c) 2000, 2021, 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>

修改root账户

mysql> set password for root@localhost=password('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)

如果要配置远程连接用户的话,可以输入

mysql> grant all privileges on *.* to 'root'@'%'identified by '123456'
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

如下为随意建立一个账户,使用

select host,user,password_expired,password_last_changed,password_lifetime FROM mysql.user

如果要删除用户,使用如下即可。

drop user <用户名>

开机自启

[root@My-Server local]# chkconfig --add mysql

查看是否开启

[root@My-Server local]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysql           0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

防火墙开放端口

[root@SQL-Server local]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success

开放3306端口,然后再使用

[root@SQL-Server local]# firewall-cmd --reload
success

查看开放端口

[root@SQL-Server local]# firewall-cmd --list-ports
3306/tcp

你可能感兴趣的:(mysql,服务器,linux,mysql,服务器)