1.参照官网,安装docker
2.拉取mysql镜像(默认拉取最新的镜像)8.0.11
dockerpullmysql
3.在宿主机创建持久化mysqldata及mysql.cnf
mkdir/usr/local/mysqlData/test/cnf mkdir/usr/local/mysqlData/test/data vi/usr/loal/mysqlData/test/cnf/mysql.cnf
设置本地文件共享:
Docker->Preferences...->FileSharing
4.添加操作权限
chmod777/usr/local/mysqlData/test/data 备注:挂载时权限验证(操作权限)
5.运行镜像,设置初始密码、本机与docker端口的映射与挂载本地数据盘(启动msyql服务)
dockerrun-itd-p3307:3306--nametest_mysql-v/usr/local/mysqlData/test/conf:/etc/mysql -v/usr/local/mysqlData/test/data:/var/lib/mysql-eMYSQL_ROOT_PASSWORD=123456mysql
运行结果:
6.进入test_mysql容器
Dockerexec-ittest_mysqlbash
如图:
7.在容器内登录mysql
8.查看用户信息
mysql>selectuser,host,authentication_stringfrommysql.user; +------------------+-----------+------------------------------------------------------------------------+ |user|host|authentication_string| +------------------+-----------+------------------------------------------------------------------------+ |root|%|$A$005$7o{'|'AomAw(QvF#.p5wLtCnrG6yX6XQdDVQivGr96POVL.gKnhIAhUhl3.| |mysql.infoschema|localhost|*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE| |mysql.session|localhost|*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE| |mysql.sys|localhost|*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE| |root|localhost|$A$005$0.-%i)H{uYi@zFo7uYF82fYw7DsA93vYLr4uZv6I1tSKao0sbzzcDap3| +------------------+-----------+------------------------------------------------------------------------+ 5rowsinset(0.00sec)
9.设置权限(为root分配权限,以便可以远程连接)
mysql>grantallPRIVILEGESon*.*toroot@'%'WITHGRANTOPTION; QueryOK,0rowsaffected(0.01sec)
10.由于Mysql5.6以上的版本修改了Password算法,这里需要更新密码算法,便于使用Navicat连接
mysql>grantallPRIVILEGESon*.*toroot@'%'WITHGRANTOPTION; QueryOK,0rowsaffected(0.01sec) mysql>ALTERuser'root'@'%'IDENTIFIEDBY'123456'PASSWORDEXPIRENEVER; QueryOK,0rowsaffected(0.11sec) mysql>ALTERuser'root'@'%'IDENTIFIEDWITHmysql_native_passwordBY'123456'; QueryOK,0rowsaffected(0.11sec) mysql>FLUSHPRIVILEGES; QueryOK,0rowsaffected(0.01sec)
11.使用navicat连接mysql,如图:
12.建库、建表、加数据
查看挂载本地数据盘的内容:
13.测试将容器移除后,数据是否仍然存在
dockerrm-ftest_msyql
容器已经移除了。重新部署test_mysql,参照第5的步骤,进入新容器,访问数据库:
xushijiandeiMac:dataxushijian$dockerrun-itd-p3307:3306--nametest_mysql-v/usr/local/mysqlData/test/conf:/etc/mysql-v/usr/local/mysqlData/test/data:/var/lib/mysql-eMYSQL_ROOT_PASSWORD=123456mysql 65b7a60050aaef5765ed055acfd071c7c76f60e85dc25d0e73e0d56eae14aed1 xushijiandeiMac:dataxushijian$dockerexec-ittest_mysqlbash root@65b7a60050aa:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis8 Serverversion:8.0.11MySQLCommunityServer-GPL Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| |test| +--------------------+ 5rowsinset(0.01sec) mysql>usetest; Readingtableinformationforcompletionoftableandcolumnnames Youcanturnoffthisfeaturetogetaquickerstartupwith-A Databasechanged mysql>select*fromuser_user; +---------+-----------+-------------+--------+ |user_id|user_name|phone|note| +---------+-----------+-------------+--------+ |1|没长正|13980000000|测试| +---------+-----------+-------------+--------+ 1rowinset(0.06sec)
发现数据仍然可以使用,不需要额外的配置,实现了数据的持久化。
阿里云上docker部署MySQL(通过编排模板部署)
<1.配置同第3步类似(只是目录变化)
master: image:'mysql:latest' environment: -MYSQL_ROOT_PASSWORD=123456 ports: -'3307:3306/tcp' volumes: -'/usr/local/mysqlData/master/conf:/etc/mysql:rw' -'/usr/local/mysqlData/master/data:/var/lib/mysql:rw' labels: aliyun.scale:'1'
<2.如下图,已部署完成
[root@c13a6d832fd0a49398c62002361d75c60-node1~]#clear [root@c13a6d832fd0a49398c62002361d75c60-node1~]#dockerps CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 8597b7539a3amysql:latest"docker-entrypoint..."3minutesagoUp3minutes0.0.0.0:3307->3306/tcpmysql_master_1
<3.进入容器,进行权限设置,后续过程参照本机
[root@c13a6d832fd0a49398c62002361d75c60-node1/]#dockerexec-itmysql_master_1bash root@2fc0bbf48941-mysql-master-1:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis8 Serverversion:8.0.11MySQLCommunityServer-GPL Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>
<4.开放3307端口,使得外网可以访问
云服务器ECS->安全组->选择所在的地区->配置规则->添加安全组
添加安全组,如图:
5.外网访问,如下图
已连接成功。
主从环境搭建:
主库:
[root@c13a6d832fd0a49398c62002361d75c60-node1~]#dockerexec-itmysql_master_1bash root@2fc0bbf48941-mysql-master-1:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis9 Serverversion:8.0.11MySQLCommunityServer-GPL Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| +--------------------+ 4rowsinset(0.30sec) mysql>createdatabasetest; QueryOK,1rowaffected(0.12sec) mysql>showdatabase; ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'database'atline1 mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| |test| +--------------------+ 5rowsinset(0.00sec)
从库:
[root@c13a6d832fd0a49398c62002361d75c60-node1~]#dockerexec-itmysql-slave_slave_1bash root@c8661e16e3fd-mysql-slave-slave-1:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis10 Serverversion:8.0.11MySQLCommunityServer-GPL Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| +--------------------+ 4rowsinset(0.40sec) mysql>showslavestatus\G ***************************1.row*************************** Slave_IO_State:Waitingformastertosendevent主从配置成功! Master_Host:47.94.225.124 Master_User:rep Master_Port:3307 Connect_Retry:60 Master_Log_File:binlog.000003 Read_Master_Log_Pos:155 Relay_Log_File:c8661e16e3fd-mysql-slave-slave-1-relay-bin.000004 Relay_Log_Pos:363 Relay_Master_Log_File:binlog.000003 Slave_IO_Running:Yes Slave_SQL_Running:Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno:0 Last_Error: Skip_Counter:0 Exec_Master_Log_Pos:155 Relay_Log_Space:762 Until_Condition:None Until_Log_File: Until_Log_Pos:0 Master_SSL_Allowed:No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master:0 Master_SSL_Verify_Server_Cert:No Last_IO_Errno:0 Last_IO_Error: Last_SQL_Errno:0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id:1 Master_UUID:a482f5fe-80fb-11e8-9fb1-0242ac12020c Master_Info_File:mysql.slave_master_info SQL_Delay:0 SQL_Remaining_Delay:NULL Slave_SQL_Running_State:Slavehasreadallrelaylog;waitingformoreupdates Master_Retry_Count:86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position:0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key:0 1rowinset(0.00sec) mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| +--------------------+ 4rowsinset(1.01sec) mysql>showdatabase; ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'database'atline1 mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| |sys| |test| +--------------------+ 5rowsinset(0.00sec)
主从库原理分析:
i/o线程去请求主库的binlog,并将得到的binlog日志写到relaylog(中继日志)文件中;主库会生成一个logdump线程,用来给从库i/o线程传binlog;
SQL线程,会读取relaylog文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致;
即:从库IO线程请求->中继日志->获取binlog->从库SQL线程,解析
参考:
https://docs.docker.com/ docker 官网文档
https://blog.csdn.net/gf0515/article/details/80466213 Mac Navicat连接Docker MySql8.0
https://blog.csdn.net/qq_28804275/article/details/80891951 主从环境搭建
docker mysql Dockerfile mysql 开源镜像Dockerfile及配置
https://www.cnblogs.com/Aiapple/p/5792939.html 主从复制原理、高可用分析
http://www.cnblogs.com/Aiapple/p/5793786.html 实战
到此这篇关于Docker部署Mysql8.0的方法示例的文章就介绍到这了,更多相关Docker部署Mysql8.0内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/xsj34567/article/details/80940238