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

ubuntu下在docker中安装mysql5.6 的方法

程序员文章站 2022-06-24 12:18:17
1.安装mysql5.6 docker run mysql:5.6 等所有项目都是download complet 安装完毕 5.6: pull...

1.安装mysql5.6

docker run mysql:5.6

等所有项目都是download complet 安装完毕

5.6: pulling from library/mysql
10a267c67f42: pull complete 
c2dcc7bb2a88: pull complete 
17e7a0445698: pull complete 
9a61839a176f: pull complete 
d4657fda01d9: pull complete 
c5278a445e5d: pull complete 
197c0b76f238: pull complete 
f54c5432bac4: pull complete 
c26b15abee8a: pull complete 
80dbe6022c18: pull complete 
435678c09612: pull complete 
digest: sha256:84372107beb1de34bd4673049ea61bc35f374a2b4f19fd5a3abd4d0a2a50b715
status: downloaded newer image for mysql:5.6
error: database is uninitialized and password option is not specified 
 you need to specify one of mysql_root_password, mysql_allow_empty_password and mysql_random_root_password

2.使用本地账号创建一个文件夹用来映射docker中mysql的配置文件

其中继续创建conf、data、logs 3个文件夹

我的配置如下

val@val-linux:~/docker/mysql1$ pwd
/home/val/docker/mysql1
val@val-linux:~/docker/mysql1$ ls
conf data logs

3.创建mysql容器

切换到root账号进入到刚才创建的mysql1文件夹中

root@val-linux:/home/val# cd /home/val/docker/mysql1/
root@val-linux:/home/val/docker/mysql1# 

创建并启动容器

docker run -p 33061:3306 --name mysql1 -v $pwd/conf:/etc/mysql -v $pwd/logs:/logs -v $pwd/data:/mysql_data -e mysql_root_password=123456 -d mysql:5.61
用本地33061端口映射docker的3306端口 
给容器命名mysql1 
用本地/home/val/docker/mysql1/conf映射mysql的conf 
用本地/home/val/docker/mysql1/logs映射mysql的logs 
用本地/home/val/docker/mysql1/data映射mysql的mysql_data 

给root设置密码123456

4.启动mysql容器

docker start mysql11

5.进入mysql的bash

docker exec -it mysql1 env lang=c.utf-8 bash

其中 env lang=c.utf-8 bash 让docker命令行支持中文

6.复制配置文件

cp /usr/my.cnf /etc/mysql/my.cnf

7.登录mysql

mysql -u root -p

输入密码进入

8.查看mysql编码

mysql> show variables like '%character%';
+--------------------------+----------------------------+
| variable_name      | value           |
+--------------------------+----------------------------+
| character_set_client   | latin1           |
| character_set_connection | latin1           |
| character_set_database  | latin1           |
| character_set_filesystem | binary           |
| character_set_results  | latin1           |
| character_set_server   | latin1           |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

发现不是utf-8编码

9.修改mysql编码

退出mysql,退出docker

mysql> exit
bye
root@8b83a30acbf8:/# exit
exit
root@val-linux:/home/val/docker/mysql1# 

修改my.cnf配置文件

root@val-linux:/home/val/docker/mysql1# vim conf/my.cnf 

按i进入编辑模式

加入

[client] default-character-set=utf8 
[mysql] default-character-set=utf8 
[mysqld] character-set-server=utf8 1
esc :wq! 保存退出

10.重启docker的mysql再次查看编码

root@val-linux:/home/val/docker/mysql1# docker stop mysql1
mysql1
root@val-linux:/home/val/docker/mysql1# docker start mysql1
mysql1
root@val-linux:/home/val/docker/mysql1# docker exec -it mysql1 env lang=c.utf-8 bash
root@8b83a30acbf8:/# mysql -u root -p
enter password: 
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 1
server version: 5.6.36 mysql community server (gpl)
copyright (c) 2000, 2017, 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> show variables like '%character%';
+--------------------------+----------------------------+
| variable_name      | value           |
+--------------------------+----------------------------+
| character_set_client   | utf8            |
| character_set_connection | utf8            |
| character_set_database  | latin1           |
| character_set_filesystem | binary           |
| character_set_results  | utf8            |
| character_set_server   | latin1           |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

11.创建数据库

mysql> create database rise;
query ok, 1 row affected (0.00 sec) 
mysql> use rise;
database changed

12.创建用户并授权

mysql> grant all privileges on rise.* to val@'%' identified by '123456';
query ok, 0 rows affected (0.00 sec)
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)

%表示任意ip(可以指定ip)、val是用户名、123456是密码

以上所述是小编给大家介绍的ubuntu下在docker中安装mysql5.6 的方法,希望对大家有所帮助