一.拉取mariadb
獲取最新版:docker pull mariadb
男娄,或者帶上版本號(hào)獲取指定版本:docker pull mariadb:10.1
$ docker pull mariadb
Using default tag: latest
latest: Pulling from library/mariadb
da7391352a9b: Already exists
14428a6d4bcd: Already exists
2c2d948710f2: Already exists
22776aa82430: Pull complete
90e64230d63d: Pull complete
f30861f14a10: Pull complete
e8e9e6a3da24: Pull complete
420a23f08c41: Pull complete
bd73f23de482: Pull complete
a8690a3260b7: Pull complete
4202ba90333a: Pull complete
a33f860b4aa6: Pull complete
Digest: sha256:cdc553f0515a8d41264f0855120874e86761f7c69407b5cfbe49283dc195bea8
Status: Downloaded newer image for mariadb:latest
docker.io/library/mariadb:latest
二.創(chuàng)建容器運(yùn)行mariadb
創(chuàng)建容器:docker run -p 127.0.0.1:3306:3306 --name test-db -e MARIADB_ROOT_PASSWORD=root -d mariadb
$ docker run -p 127.0.0.1:3306:3306 --name test-db -e MARIADB_ROOT_PASSWORD=root -d mariadb
f7d9a7185b91b72f457e2e4a99ab7a31e8d3dc1bbf6fa590f0c9f416fefe9b05
-p:映射端口请敦,127.0.0.1:3306
是容器端口圾亏,3306
是主機(jī)端口
--name:將這個(gè)容器命名為:test-db
-e:設(shè)置系統(tǒng)級(jí)的環(huán)境變量驮肉,MARIADB_ROOT_PASSWORD
是mariadb默認(rèn)的root用戶密碼
-d:在后端運(yùn)行呻袭,創(chuàng)建容器后容器保持運(yùn)行狀態(tài)
這樣就創(chuàng)建好了一個(gè)在后端運(yùn)行的數(shù)據(jù)庫(kù)容器钢颂,使用root用戶和密碼就可以訪問(wèn)這個(gè)數(shù)據(jù)庫(kù)了经窖。
進(jìn)入容器操作數(shù)據(jù)庫(kù):docker exec -it test-db bash
$ docker exec -it test-db bash
root@f3b60845f17b:/#
默認(rèn)是不支持中文的拣展,需要設(shè)定改為utf8,可用locale
查看字符集
echo 'export LANG=C.UTF-8' >> /etc/profile
echo 'source /etc/profile' >> ~/.bashrc
-最好的方式是使用DOCKERFILE彭沼,在dockerfile里面添加上ENV LANG C.UTF-8
數(shù)據(jù)庫(kù)初始化:
-登錄mysql -u root -p
mysql -u root -p #輸入root 用戶密碼
-創(chuàng)建遠(yuǎn)程登錄用戶 ,
格式:create user ‘用戶名’@’%’ identified by ‘密碼’;
create user 'us'@'%' identified by '123';
-授權(quán)(如果權(quán)限不足)
GRANT ALL PRIVILEGES ON *.* TO 'us'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
-創(chuàng)建數(shù)據(jù)庫(kù),設(shè)定字符集utf8备埃,校驗(yàn)規(guī)則utf8_general_ci
CREATE DATABASE IF NOT EXISTS blog
CHARACTER SET utf8 COLLATE
utf8_general_ci;