1. Cài đặt bằng Docker compose
- Tạo file: docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
version: '3.3' networks: mh_network: driver: bridge services: mysql: container_name: mhc_mysql image: mysql:8.0.31 restart: always networks: - mh_network command: --default-authentication-plugin=mysql_native_password volumes: - ./data/mysql:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=medihome2022 - MYSQL_DATABASE=mh_database - MYSQL_USER=medihome - MYSQL_PASSWORD=mh123456 ports: - "7306:3306" |
2. Run and check MySQL
1 2 3 4 5 |
docker-compose up -d docker exec -it mhc_mysql bash # mysql -u root -p // Điền password: medihome2022 > SHOW DATABASES; |
3. Tạo 1 databases
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
DROP DATABASE test_db; CREATE DATABASE IF NOT EXISTS test_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; USE test_db; DROP TABLE IF EXISTS `Categories`; CREATE TABLE `Categories` ( `categoryID` varchar(10) NOT NULL, `categoryName` varchar(100) DEFAULT NULL, `description` varchar(200) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `Categories` (`categoryID`, `categoryName`, `description`) VALUES ('C0101', 'Beverages', 'Soft drinks, coffees, teas, beers, and ales'), ('C0102', 'Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'), ('C0103', 'Confections', 'Desserts, candies, and sweet breads'), ('C0104', 'Dairy Products', 'Cheeses'), ('C0105', 'Grains_Cereals', 'Breads, crackers, pasta, and cereal'), ('C0106', 'Meat_Poultry', 'Prepared meats'), ('C0107', 'Produce', 'Dried fruit and bean curd'), ('C0108', 'Seafood', 'Seaweed and fish'); DROP TABLE IF EXISTS `Products`; CREATE TABLE `Products` ( `productID` varchar(10) NOT NULL, `categoryID` varchar(10) DEFAULT NULL, `productName` varchar(50) DEFAULT NULL, `price` int(11) DEFAULT NULL, `description` varchar(200) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `Products` (`productID`, `categoryID`, `productName`, `price`, `description`) VALUES ('P0101', 'C0101', 'Chai', 18, 'Good product'), ('P01010', 'C0104', 'Ikura', 31, 'Good'), ('P01011', 'C0105', 'Queso Cabrales', 21, 'Good'), ('P01012', 'C0105', 'Queso Manchego La Pastora', 38, 'Good'), ('P0102', 'C0101', 'Chang', 19, 'best sell product'), ('P0103', 'C0101', 'Aniseed Syrup', 10, 'new produced'), ('P0104', 'C0102', 'Chef Anton\'s Cajun Seasoning', 22, 'popular product'), ('P0105', 'C0102', 'Chef Anton\'s Gumbo Mix', 21, 'Good For life'), ('P0106', 'C0103', 'Grandma\'s Boysenberry Spread', 25, 'Good'), ('P0107', 'C0103', 'Uncle Bob\'s Organic Dried Pears', 30, 'Good'), ('P0108', 'C0103', 'Northwoods Cranberry Sauce', 40, 'Good'), ('P0109', 'C0104', 'Mishi Kobe Niku', 97, 'Good'); ALTER TABLE `Categories` ADD PRIMARY KEY (`categoryID`); ALTER TABLE `Products` ADD PRIMARY KEY (`productID`); COMMIT; |
4. Kiểm tra databases
1 2 3 4 5 |
SHOW TABLES; SELECT * FROM Categories; SELECT * FROM Products; DESCRIBE Categories; DESCRIBE Products; |