docker容器ubuntu:18.04镜像安装mysql以及一些配置

注:拉取镜像后,启动容器时一定要配置下权限,否则安装mysql时会出现二中的错误。

docker run -itd --privileged=true --name=ubuntudemo ubuntu:18.04 /bin/bash

否则会出现如下错误

Setting up mysql-server-5.7 (5.7.36-0ubuntu0.18.04.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of stop.
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Cannot stat file /proc/244/fd/0: Permission denied
Cannot stat file /proc/244/fd/1: Permission denied
Cannot stat file /proc/244/fd/2: Permission denied
Cannot stat file /proc/244/fd/3: Permission denied
Cannot stat file /proc/244/fd/4: Permission denied
Cannot stat file /proc/244/fd/5: Permission denied
Cannot stat file /proc/244/fd/6: Permission denied
Cannot stat file /proc/244/fd/7: Permission denied
Cannot stat file /proc/244/fd/8: Permission denied
Cannot stat file /proc/244/fd/9: Permission denied
Cannot stat file /proc/244/fd/10: Permission denied
Cannot stat file /proc/244/fd/11: Permission denied
Cannot stat file /proc/244/fd/12: Permission denied
Cannot stat file /proc/244/fd/13: Permission denied
Cannot stat file /proc/244/fd/14: Permission denied
Cannot stat file /proc/244/fd/15: Permission denied
Cannot stat file /proc/244/fd/16: Permission denied
Cannot stat file /proc/244/fd/17: Permission denied
Cannot stat file /proc/244/fd/18: Permission denied
Cannot stat file /proc/244/fd/19: Permission denied
Cannot stat file /proc/244/fd/20: Permission denied
Cannot stat file /proc/244/fd/21: Permission denied
Cannot stat file /proc/244/fd/22: Permission denied
Cannot stat file /proc/244/fd/23: Permission denied
Cannot stat file /proc/244/fd/24: Permission denied
Cannot stat file /proc/244/fd/25: Permission denied
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up libfcgi-perl (0.78-2build1) ...
Setting up libhtml-tagset-perl (3.20-3) ...
Setting up mysql-server (5.7.36-0ubuntu0.18.04.1) ...
Setting up libencode-locale-perl (1.05-1) ...
Setting up libtimedate-perl (2.3000-2) ...
Setting up libio-html-perl (1.001-1) ...
Setting up libhtml-parser-perl (3.72-3build1) ...
Setting up libcgi-pm-perl (4.38-1) ...
Setting up libhttp-date-perl (6.02-1) ...
Setting up libhtml-template-perl (2.97-1) ...
Setting up libcgi-fast-perl (1:2.13-1) ...
Setting up libhttp-message-perl (6.14-1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
root@2688f80945f7:/#

一、ubuntu18.04下安装mysql:

1.1、安装mysql:

#命令1

sudo apt-get update

#命令2

sudo apt-get install mysql-server

1.2、新建相应的目录避免后续出错

mkdir -p /var/run/mysqld
chown mysql /var/run/mysqld/
service mysql restart

1.3、配置MySQL

1.3.1、初始化配置

mysql_secure_installation

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (用来测试的密码选择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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
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 (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#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 (我的选项)

1.3.2、检查mysql服务状态

#ubuntu下systemctl 命令
systemctl status mysql.service 
#docker ubuntu镜像 service mysql status


#查看内容如下
root@18f4411cbf2f:/# service mysql status
 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.7.36, for Linux on x86_64
Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          5.7.36-0ubuntu0.18.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 4 sec

Threads: 1  Questions: 8  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 2.000

1.3.3、登录mysql  

mysql -uroot -p
#1、此时密码并不是按之前自己的设置密码登录,可以直接回车登录到mysql

二、权限等配置

2.1、修改mysql配置文件中:

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

找到address=127.0.0.1这一段文本,将它注释掉或则将它改成address =0.0.0.0。这些方法目的是允许其他的主机可以访问服务,而不仅仅是127.0.0.1(localhost)。

备注:PC端Navicat连接docker容器里面的数据库 连接不上,报一下错误:2003-Can’t connect to MySql server on ‘x.x.x.x'(10061 “Unknown error”)

用2.1的方法修改配置文件就可以解决。

2.2、新增用户测试:

#命令1 输入密码或者直接回车

mysql -uroot -p

#命令2 创建用户

create user'ruhr'@'%' identified by 'Aa.12345';

#命令3  查看是否创建成功
select * from mysql.user;

#命令4 修改密码 修改root密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Aa.12345';

#命令5 刷新系统权限表
flush privileges;


2.3、修改用户权限设置:

#命令1
mysql -uroot -p

mysql> grant all privileges on *.* to 'ruhr'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql>

2.4、启动mysql时提示错误信息:

mysql启动报错【No directory, logging in with HOME=/】

解决方法:

 usermod -d /var/lib/mysql/ mysql

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值