方式1:
# 解压 tar zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz # 安装 mv mysql-5.7.35-linux-glibc2.12-x86_64 /usr/local/mysql mkdir /usr/local/mysql/data groupadd mysql useradd -g mysql mysql passwd mysql chown -R mysql:mysql /usr/local/mysql # 初始化数据库 cd /usr/local/mysql/bin ./mysqld --initialize --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ # 添加命令 ln -s /usr/local/mysql/bin/mysql /usr/bin/ # 添加配置文件 vim /etc/my.cnf ---[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] skip-name-resolve #设置3306端口 port = 3306 # 设置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 max_allowed_packet=16M --- # 添加到系统服务 cp support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld # 启动 systemctl start mysqld # 修改密码 mysql> set PASSWORD=PASSWORD("admin123"); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 添加远程访问权限 mysql> use mysql 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'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 # 重启生效 systemctl restart mysqld # 查看版本 mysql -V 方式二:
环境准备:
1. 数据库下载地址:https://dev.mysql.com/downloads/mysql/ 2. 创建cd /data/mysql文件夹,并把下载好的数据库包上传到该目录下 3. 先查看一下是否已经安装了,命令:rpm -qa|grep mariadb 4. 删除mariadb,命令:rpm -e --nodeps mariadb-libs
-
解压数据库压缩包:mysql-8.0.32-1.el7.x86_64.rpm-bundle.tar
tar -xf mysql-8.0.32-1.el7.x86_64.rpm-bundle.tar
-
安装依赖包:
yum install perl.x86_64 perl-devel.x86_64 -y --downloadonly --downloaddir=/data/mysql/ylb yum install perl-JSON.noarch -y --downloadonly --downloaddir=/data/mysql/ylb yum -y install autoconf -y --downloadonly --downloaddir=/data/mysql/ylb yum -y install net-tools -y --downloadonly --downloaddir=/data/mysql/ylb yum install libaio -y --downloadonly --downloaddir=/data/mysql/ylb yum -y install numactl -y --downloadonly --downloaddir=/data/mysql/ylb 或 rpm -ivh *rpm --nodeps --force
-
安装数据库
【必须安装】 rpm -ivh mysql-community- common-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-server-8.0.32-1.el7.x86_64.rpm 【非必须安装】 rpm -ivh mysql-community-libs-compat-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-embedded-compat-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-devel-8.0.32-1.el7.x86_64.rpm rpm -ivh mysql-community-test-8.0.32-1.el7.x86_64.rpm
-
创建mysql用户和组
groupadd mysql useradd -g mysql mysql
-
查看服务状态
systemctl status mysqld #查看mysql状态
-
停止mysql服务
service mysqld stop #停止mysql服务
-
初始化数据库
mysqld --initialize --console --lower-case-table-names=1
-
目录授权
chown -R mysql:mysql /var/lib/mysql/
-
启动mysql服务
systemctl start mysqld
-
查看临时密码:
cat /var/log/mysqld.log
-
用临时密码登录数据库
mysql -u root -p
-
修改mysql密码
alter USER 'root'@'localhost' IDENTIFIED BY 'TJ5846@9090';
-
授权远程连接
show databases; #展示数据库 use mysql; #登录数据库 select host, user, authentication_string, plugin from user; #查看权限 update user set host = "%" where user='root'; #更改远程连接权限 flush privileges; #刷新配置 alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'TJ5846@9090';
遇到的问题:
1:本地连接时报一下错误
解决办法:
1、通过find / -name mysql.sock 2、查询到结果,做一个软链接到/var/lib/mysql/mysql.sock目录下即可解决问题 ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock