gdjlc

培养良好的习惯,每天一点一滴的进步,终将会有收获。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、准备好MySQL安装包,官网下载地址:https://downloads.mysql.com/archives/community/

2、把MySQL安装压缩包包上传到Linux服务器的/opt目录

3、XShell连上Linux服务器

4、运行下面命令查看是否已安装MySQL

rpm -qa|grep mysql

5、运行下面命令查看是否已安装mariadb

rpm -qa|grep mariadb

本机输出:mariadb-libs-5.5.65-1.el7.x86_64

可使用下面命令卸载

yum remove mariadb-libs-5.5.65-1.el7.x86_64

6、切换到/opt目录,解压MySQL压缩包,重命名

cd /opt
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql

7、创建mysql用户组和用户,创建存放日志和数据目录,修改目录所属用户

groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
mkdir -pv /opt/mysql/data /opt/mysql/logs
chown -R mysql:mysql /opt/mysql

8、创建MySQL配置文件

vim /etc/my.cnf

编辑为下面内容后保存(basedir、datadir、log-error、pid-file路径和上面保持一致):

[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
port = 3306
socket=/tmp/mysql.sock
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
symbolic-links=0
log-error=/opt/mysql/logs/mysqld.log
pid-file=/opt/mysql/mysqld.pid
lower_case_table_names=1
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
[client]
default-character-set = utf8mb4
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock

9、MySQL初始化、配置环境变量

/opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
echo "export PATH=/opt/mysql/bin:\$PATH" >> /etc/profile
source /etc/profile

备注:
如果运行出错,检查/etc/my.cnf里面的配置是否有错,修改完后删除/opt/mysql/data和/opt/mysql/logs里面的文件,再执行初始化命令。

10、MySQL启动关闭配置

vim /usr/lib/systemd/system/mysqld.service

编辑为下面内容后保存:

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

11、重新加载新的服务,设置MySQL开机自启,启动服务,查看启动状态

systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld

12、查看上面生成的临时密码,后面mysql_secure_installation初始化要用到

head /opt/mysql/logs/mysqld.log

里面有一行:

A temporary password is generated for root@localhost: XajJlcb(C6KV

13、执行mysql_secure_installation初始化配置向导设置root用户、密码、权限。

mysql_secure_installation

部分内容如下:

Enter password for user root: ----输入上面的临时密码
New password: ----设置新密码
Re-enter new password: ----再次输入新密码
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y ----是否删除匿名用户
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n----是否禁止root远程登录,根据需求选择
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n----是否删除test数据库,根据需求选择
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y----是否重新加载权限表

14、登录,输入下面命令行后,输入新密码

mysql -uroot -p

查询数据库

show databases;

查询用户表

select user,host from mysql.user;

 如果需要允许在别的机器远程连接,则需要更新字段值

use mysql;
update user set host = '%' where user = 'root' and host='localhost'; FLUSH PRIVILEGES;

 退出

exit

另外防火墙3306端口也需要开启。

posted on 2021-12-26 12:25  gdjlc  阅读(416)  评论(0编辑  收藏  举报