Ubuntu 18.04 虚拟机安装Mysql

环境准备

这边已经准备好一个空闲的虚拟机,信息如下:

ubuntu@ubuntuMysql:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.133  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:fedf:47ab  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:df:47:ab  txqueuelen 1000  (Ethernet)
        RX packets 65240  bytes 96272315 (96.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15666  bytes 1021191 (1.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 223  bytes 17672 (17.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 223  bytes 17672 (17.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

安装MySQL

首先切换到root用户权限下,然后执行下面的命令安装MySQL

sudo apt update
sudo apt install mysql-server

配置MySQL

1. 安装配置

sudo mysql_secure_installation

配置说明

#1是否安装密码校验插件
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:

#2设置密码
Please set the password for root here.
New password:
Re-enter new password:

#3删除匿名用户(生产环境有必要删除)
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) :

#4是否允许root用户远程登录
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) :

#5是否删除“测试”库
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) :

#6是否立即生效
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) :

2. 检查MySQL服务的运行状态

root@ubuntuMysql:~# systemctl status mysql.service

显示如下为正常状态

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-09-22 15:26:47 UTC; 3min 27s ago
 Main PID: 23814 (mysqld)
    Tasks: 29 (limit: 4633)
   CGroup: /system.slice/mysql.service
           └─23814 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Sep 22 15:26:47 ubuntuMysql systemd[1]: Starting MySQL Community Server...
Sep 22 15:26:47 ubuntuMysql systemd[1]: Started MySQL Community Server.

3. 远程登录MySQL

sudo mysql -uroot -p

执行如下命令配置mysql的root用户的host

use mysql
update user set host='%' where user='root';
flush privileges;
exit;

2003 - Can't connect to MySQL server on '192.168.202.133' (10061 "Unknow error")

root@ubuntuMysql:~# vi /etc/mysql/mysql.conf.d/mysqld.cnf 
# 注释掉bind-address          = 127.0.0.1
root@ubuntuMysql:~# sudo /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.

1698 - Access denied for user 'root'@'192.168.202.1'

跳过密码验证,进入MySQL,然后重置root用户密码即可。

root@ubuntuMysql:~# vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 在[mysqld]段落内增加skip-grant-tables跳过密码验证
skip-grant-tables

# 重启MySQL服务
root@ubuntuMysql:~# sudo /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.

# 重启完成之后,直接使用mysql命令进入mysql
root@ubuntuMysql:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> 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 authentication_string=password("12345678"),plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

上面的配置完成后,还需要把增加的跳过密码验证的配置关闭,则需要进入配置文件中,然后在skip-grant-tables前增加#,然后重启MySQL服务

重启完成后,可以再使用mysql -uroot -p输入密码登录mysql

root@ubuntuMysql:~# sudo /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.
root@ubuntuMysql:~# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> exit
Bye

Navicat连接MySQL

进行完成上述操作后,即可使用Navicat访问MySQL了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容