linux安装MySQL并配置远程连接

1、安装MySQL

linux命令:

sudo apt install mysql-server 或 sudo apt install -y mysql-server

后面命令中的-y表示:当安装包的时候会询问y/n,这个参数是所有询问默认y,直接下载安装,不再要求确认。

如果安装过程中遇到如下报错代码

E: Unable to fetch some archives, maybe run apt-get update or try with –fix

请按照这个教程更换软件的下载镜像源

http://www.manongjc.com/detail/26-scavrjkfmkyeafi.html

这个教程完成之后再重新执行安装命令

检查是否安装成功

linux命令:

netstat -tap | grep mysql

通过上述命令检查之后,如果看到有 mysql 的 socket 处于 LISTEN 状态则表示安装成功。

在这里插入图片描述

查看数据库服务状态

linux命令:

service mysql status 或 systemctl status mysql

执行命令后出现 active(running) 则表示已启动

在这里插入图片描述

注:操作数据库的一些其他命令:

查看数据库的编码方式命令:

show variables like 'character%';

查看MySQL数据库状态:

service mysqld status` 或 `service mysql status

启动MySQL:

service mysql start

停止MySQL:

service mysql stop

重启MySQL:

service mysql restart

连接Mysql:

mysql -uroot -proot  (mysql -u用户名 -p密码)

查看Mysql数据库:

show databases;

创建数据库:

create database name;

直接删除数据库,不提示:

drop database name;

``
使用数据库:`

use mysql; (use 数据库名;)

登录mysql数据库

linux命令:

mysql -u root -p

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,新安装的mysql数据库是没有密码的,Enter password: 处直接回车,就能够进入mysql数据库。

linux命令:

show databases;

`就可以查看当前的所有数据库。

在这里插入图片描述

MySQL初始配置

· linux命令:

mysql_secure_installation

为了确保数据库的安全性和正常运转,对数据库进行初始化操作。这个初始化操作涉及下面5个步骤。

(1)安装验证密码插件。

(2)设置root管理员在数据库中的专有密码。

(3)随后删除匿名账户,并使用root管理员从远程登录数据库,以确保数据库上运行的业务的安全性。

(4)删除默认的测试数据库,取消测试数据库的一系列访问权限。

(5)刷新授权列表,让初始化的设定立即生效。

代码执行演示如下:

root@zwp:/home/zwp*# mysql_secure_installation*

 

Securing the MySQL server deployment.

 

Connecting to MySQL using a blank password.

 

VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD plugin?  *#**要安装验证密码插件吗**?*

 

Press y|Y for Yes, any other key for No: N  *#* *这里我选择**N*

Please set the password for root here.

 

New password: *#* *输入要为**root**管理员设置的数据库密码*

 

Re-enter new password: *#* *再次输入密码*

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.

 

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y *#* *删除匿名账户*

Success.

 

 

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.

 

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n  *#* *禁止**root**管理员从远程登录,这里我没有禁止*

 

 ... skipping.

By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.

 

 

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y  *#* *删除**test**数据库并取消对它的访问权限*

 \- Dropping test database...

Success.

 

 \- Removing privileges on test database...

Success.

 

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

 

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y  *#* *刷新授权表,让初始化后的设定立即生效*

Success.

 

All done

修改root账户密码认证方式

· 连接到Mysql:

mysql -uroot -p 或 sudo mysql -uroot -p

· 查看用户:

mysql> select user, plugin from mysql.user;

在这里插入图片描述

· 重置Root密码,修改认证方式:

·

    mysql> update mysql.user set authentication_string=PASSWORD('root'), plugin='mysql_native_password' where user='root';//修改密码为root

·        Query OK, 1 row affected, 1 warning (0.05 sec)

·        Rows matched: 1  Changed: 1  Warnings: 1

·         

·        mysql> flush privileges;  *#* *刷新权限*

·        Query OK, 0 rows affected (0.04 sec)

·         

·        mysql> exit  *#* *退出

· 重置成功后,需使用新密码登录。

配置MySQL允许远程访问

首先编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 配置文件:

· linux命令:

vim /etc/mysql/mysql.conf.d/mysqld.cnf

注释掉

bind-address = 127.0.0.1 

如下图:
在这里插入图片描述

保存退出,然后进入mysql数据库,执行授权命令:

mysql>GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

Query OK, 0 rows affected, 1 warning (0.02 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

mysql> exit

Bye

其中root@%,localhost就是本地访问,配置成%就是所有主机都可连接;第二个’root’为你给新增权限用户设置的密码。

再执行如下命令重启mysql:


linux命令:systemctl restart mysql 或 service mysql restart 或 sudo /etc/init.d/mysql restart

在 Windows 下可以使用 SQLyog 图形化工具远程连接 Ubuntu 下的 MySQL 数据库,输入刚授权远程权限的密码。

其他操作

1、查看端口:在SQL中执行下面 linux 命令

show global variables like 'port';
  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值