使用docker構建簡單的本地的mysql服務

博客原文
安利一篇我翻譯的國外大牛的神經網絡入門文章

環境

liujinliu@liujinliu-OptiPlex-7010:~$ uname -a
Linux liujinliu-OptiPlex-7010 4.4.0-57-generic #78-Ubuntu SMP Fri Dec 9 23:50:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

首先參考https://docs.docker.com/engine/installation/linux/ubuntulinux/#/install-the-latest-version安裝docker服務

下載mysql鏡像

參照/mysql/”>https://hub.docker.com//mysql/的步驟下載mysql

docker pull mysql:5.6

啓動mysql

sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6

連接mysql

找到啓動的msql容器並找到容器的ip

liujinliu@liujinliu-OptiPlex-7010:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7a1400622109        mysql:5.6           "docker-entrypoint.sh"   About an hour ago   Up 35 minutes       3306/tcp            mysql
liujinliu@liujinliu-OptiPlex-7010:~$ sudo docker inspect 7a1400622109 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

接下來使用root用戶連接mysql

liujinliu@liujinliu-OptiPlex-7010:~$ mysql -h 172.17.0.2 -u root -proot -t -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> create database if not exists test default charset utf8 collate utf8_general_ci;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

一個簡單的select for update的示例

mysql> select * from active_records;
+-----------+-------+-------+------+
| user_id   | utime | stime | snum |
+-----------+-------+-------+------+
| abcde1230 |     0 |     0 |    0 |
| abcde1231 |     0 |     0 |    0 |
| abcde1232 |     0 |     0 |    0 |
+-----------+-------+-------+------+
3 rows in set (0.00 sec)

mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> begin work;
Query OK, 0 rows affected (0.00 sec)

# 此時其他連接執行下面的命令會被阻塞住(即這一行被鎖住了,只有commit之後會解鎖)
mysql> select snum from active_records where user_id='abcde1230' for update;
+------+
| snum |
+------+
|    0 |
+------+
1 row in set (0.00 sec)

mysql> update active_records set snum=1 where user_id='abcde1230';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.03 sec)

mysql>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章