欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Linux服务器上安装MySQL并配置,远程连接

程序员文章站 2024-03-25 14:26:58
...

        MySQL是用的很多的关系型数据库。今天来分享一下安装,配置及其连接教程。这里以Debian服务器安装MySQL 8为例。

1,下载MySQL并解压上传

下载页面下载linux版的mysql安装包。

在Linux服务器上安装MySQL并配置,远程连接

然后下载了一个tar文件,解压你会发现解压出来了一堆deb文件,把这些文件全部上传到linux服务器的一个目录里,并cd命令进入那个目录,然后安装所有的deb文件:

dpkg -i *.deb

 若安装后有错误信息,则执行:

apt update
apt --fix-broken install

期间会显示一个界面让你设置root密码,即数据库超级管理员root的密码,设置即可。

这样就安装完成了!

2,修改配置文件

mysql 8的配置文件在/etc/mysql/my.cnf,没有特殊需要一般不用修改。不过我们一般只需修改服务端的配置即可。服务端配置需要修改/etc/mysql/mysql.conf.d/mysqld.cnf文件,打开这个文件可以看到:

# Copyright (c) 2014, 2017, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log

里面设定了一些默认的数据库文件、日志文件位置。

服务端的默认端口是3306,不过在这个文件这可以添加配置修改其端口,接着加入下列语句:

port=自定义端口号

3,启动数据库服务

service mysql start

还可以停止或者重启:

停止:
service mysql stop

重启:
service mysql restart

4,设置外网可以连接数据库

在服务器上面输入命令连接数据库:

mysql -u root -p

然后输入管理员密码,就连接成功了。

然后依次执行下列命令:

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

就设置成功了。

5,Windows上连接MySQL

我们在自己的电脑上也要安装MySQL,这个从官网下载Windows MySQL即可。安装配置方法很简单,百度即可,这里不再赘述。

配置好后打开cmd,输入命令:

mysql -h 服务器地址 -P 端口(若服务端没有配置端口可以不要这个-P选项) -u 数据库用户名 -p

接着要输入密码,然后就可以了。