【数据库】Ubuntu18.04安装MySQL详解

00. 目录

01. 安装MySQL

1.1 更新软件源

deng@itcast:~$ sudo apt update 

1.2 安装mysql-server

deng@itcast:~$ sudo apt install mysql-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common
  nginx-core
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
建议安装:
  libipc-sharedcache-perl mailx tinyca
下列【新】软件包将被安装:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7
  mysql-server-core-5.7
升级了 0 个软件包,新安装了 9 个软件包,要卸载 0 个软件包,有 635 个软件包未被升级。
需要下载 19.1 MB 的归档。
解压缩后会消耗 155 MB 的额外空间。
您希望继续执行吗? [Y/n] 

1.3 安装libmysqlclient-dev

deng@itcast:~$ sudo apt install libmysqlclient-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common
  nginx-core
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  libmysqlclient20 libssl-dev libssl1.1 zlib1g-dev
建议安装:
  libssl-doc

02. 配置MySQL

执行如下命令

deng@itcast:~$ sudo 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?
#1. 选择N
Press y|Y for Yes, any other key for No: N

#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) : N

 ... skipping.

#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) : Y
Success.
#5. 是否移除test数据库
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) : N

 ... skipping.
 
#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) : Y
Success.

All done! 
deng@itcast:~$ 

03. 查看MySQL状态

显示active表示已经在运行

deng@itcast:~$ systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled
   Active: active (running) since Sat 2020-02-29 22:00:07 CST; 9min ago
 Main PID: 6032 (mysqld)
    Tasks: 29 (limit: 2292)
   CGroup: /system.slice/mysql.service
           └─6032 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

2月 29 22:00:07 itcast systemd[1]: Starting MySQL Community Server...
2月 29 22:00:07 itcast systemd[1]: Started MySQL Community Server.

04. 配置远程访问MySQL

在Ubuntu下MySQL缺省是只允许本地访问的,如果需要第三方工具连接MySQL则需要进行配置。

4.1 登录MySQL数据库

deng@itcast:~$ sudo mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, 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> 

4.2 授权(具有所有数据库所有的权限)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

4.3 刷新权限

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

05. 问题分析

问题1

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.13.143' (111)

解决办法:

将mysql配置文件中的bind-address = 127.0.0.1注释掉

deng@itcast:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 

 41 # Instead of skip-networking the default is now to listen only on
 42 # localhost which is more compatible and is not less secure.
 43 #bind-address       = 127.0.0.1

问题2

ERROR 1045 (28000): Access denied for user 'root'@'192.168.13.143' (using password: YES)

解决办法

在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-grant-tables

修改密码

mysql> use mysql
Database changed
mysql> update user set authentication_string=password('123456') where user='root';
Query OK, 2 rows affected, 1 warning (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 1

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

mysql> 

去掉刚才添加“skip-grant-tables” 然后重启

deng@itcast:~$ sudo systemctl restart mysql
deng@itcast:~$ mysql -h 192.168.13.143 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, 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> quit
Bye

06. 附录

发布了641 篇原创文章 · 获赞 2518 · 访问量 86万+

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/104584837