想要安裝一個mysql服務實例,最簡單的方式當然是用docker啟動一個
只需一個命令搞定:
docker run name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
其中: –name 後面的是docker容器名 -e MYSQL_ROOT_PASSWORD 是設置mysql的root賬號密碼 -d mysql 是你的鏡像標簽
還可以加上 -v
#先創建目錄和my.cf配置文件
#mkdir -p /home/mysql/{conf,logs,data/mysql}
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /home/mysql/logs:/logs -v /home/mysql/data/mysql:/var/lib/mysql
用來掛載數據目錄、配置文件、日志等 這樣重啟實例後數據不會丟失
如果連接Docker啟動的mysql出現報錯: ERROR 2059 (HY000): Authentication plugin ‘caching_sha2_password’ cannot be loaded 解決辦法,重新設置下密碼:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';
啟動成功後:
[root@iZttsn6fen661yZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e661357b4012 mysql "docker-entrypoint" 3 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
[root@iZttsn6fen661yZ ~]# docker exec ti e661357b4012 bash
unknown flag: ti
See 'docker exec help'.
[root@iZttsn6fen661yZ ~]#
[root@iZttsn6fen661yZ ~]#
[root@iZttsn6fen661yZ ~]# docker exec -ti e661357b4012 bash
root@e661357b4012:/#
root@e661357b4012:/#
root@e661357b4012:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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>
mysql>
mysql> create database test;
Query OK, 1 row affected (0.05 sec)
mysql> use test;
Database changed
mysql> create table user(id VARCHAR(20),name VARCHAR(25),age INT(4));
Query OK, 0 rows affected (0.11 sec)
mysql>