ÔõÑùÔÚLinuxÉϴ¸ß¿ÉÓõÄMySQLÈÝÆ÷¼¯Èº£¿
ÔõÑùÔÚlinuxÉϴ¸ß¿ÉÓõÄmysqlÈÝÆ÷¼¯Èº£¿
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷ÊÖÒÕµÄÐËÆð£¬Ô½À´Ô½¶àµÄÆóÒµ×îÏÈʹÓÃÈÝÆ÷À´¹¹½¨Ó¦ÓóÌÐò¡£MySQL×÷Ϊ×î³£ÓõĹØϵÐÍÊý¾Ý¿âÖ®Ò»£¬Ò²¿ÉÒÔͨ¹ýÈÝÆ÷»¯À´ÊµÏָ߿ÉÓÃÐÔ¡£ÔÚ±¾ÎÄÖУ¬½«ÏÈÈÝÔõÑùÔÚLinuxÉϴ¸ß¿ÉÓõÄMySQLÈÝÆ÷¼¯Èº£¬²¢ÌṩÏà¹ØµÄ´úÂëʾÀý¡£
°ì·¨Ò»£º×¼±¸ÇéÐÎ
Ê×ÏÈ£¬ÐèҪһ̨LinuxÖ÷»úÀ´´î½¨MySQLÈÝÆ÷¼¯Èº¡£È·±£¸ÃÖ÷»úÉÏÒѾװÖÃÁËDockerºÍDocker Compose¡£ÈôÊÇδװÖ㬿Éͨ¹ýÒÔÏÂÏÂÁî×°Öãº
$ sudo apt-get update $ sudo apt-get install docker.io $ sudo apt-get install docker-compose
µÇ¼ºó¸´ÖÆ
°ì·¨¶þ£º½¨ÉèDocker¾µÏñ
½ÓÏÂÀ´£¬ÐèÒª½¨ÉèÒ»¸öMySQL¾µÏñÀ´ÔËÐÐÈÝÆ÷¡£¿ÉÒÔͨ¹ýDockerfileÀ´½ç˵¾µÏñµÄ¹¹½¨Àú³Ì¡£½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
FROM mysql:8.0 ENV MYSQL_ROOT_PASSWORD your_password ENV MYSQL_DATABASE your_database COPY your_script.sql /docker-entrypoint-initdb.d/
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬your_passwordΪMySQLµÄrootÓû§ÃÜÂ룬your_databaseΪҪ½¨ÉèµÄÊý¾Ý¿âÃû³Æ£¬your_script.sqlΪҪִÐеijõʼ»¯¾ç±¾Îļþ¡£¿ÉÒÔƾ֤ÏÖÕæÏàÐÎÐÞ¸ÄÕâЩ²ÎÊý¡£
ÉúÑIJ¢Í˳öÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´¹¹½¨¾µÏñ£º
$ sudo docker build -t your_image_name .
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬your_image_nameÊÇÄã¸ø¾µÏñÆðµÄÃû³Æ¡£
°ì·¨Èý£º½¨ÉèDocker ComposeÎļþ
ÔڴMySQLÈÝÆ÷¼¯ÈºÖ®Ç°£¬ÐèÒª½¨ÉèÒ»¸ödocker-compose.ymlÎļþÀ´½ç˵¼¯ÈºÖеÄÈÝÆ÷¡£½¨ÉèÒ»¸öÃûΪdocker-compose.ymlµÄÎļþ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
version: '3' services: mysql1: image: your_image_name restart: always ports: - 3306:3306 volumes: - ./mysql1:/var/lib/mysql environment: - MYSQL_REPLICATION_MODE=master - MYSQL_REPLICATION_USER=repl_user - MYSQL_REPLICATION_PASSWORD=repl_password mysql2: image: your_image_name restart: always volumes: - ./mysql2:/var/lib/mysql environment: - MYSQL_REPLICATION_MODE=slave - MYSQL_MASTER_HOST=mysql1 - MYSQL_MASTER_PORT=3306 - MYSQL_MASTER_USER=repl_user - MYSQL_MASTER_PASSWORD=repl_password
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬your_image_nameÊÇ֮ǰ¹¹½¨µÄMySQL¾µÏñÃû³Æ¡£mysql1ºÍmysql2»®·ÖÌåÏÖÁ½¸öMySQLÈÝÆ÷µÄÃû³Æ¡£MYSQL_REPLICATION_MODE²ÎÊýÉèÖÃÈÝÆ÷µÄ¸´ÖÆģʽ£¬masterÌåÏÖÖ÷½Úµã£¬slaveÌåÏִӽڵ㡣MYSQL_REPLICATION_USERºÍMYSQL_REPLICATION_PASSWORDΪ¸´ÖÆÓû§µÄÓû§ÃûºÍÃÜÂë¡£MYSQL_MASTER_HOSTºÍMYSQL_MASTER_PORTΪÖ÷½ÚµãµÄµØµãºÍ¶Ë¿Ú¡£MYSQL_MASTER_USERºÍMYSQL_MASTER_PASSWORDΪÅþÁ¬Ö÷½ÚµãµÄÓû§ÃûºÍÃÜÂë¡£
°ì·¨ËÄ£ºÆô¶¯ÈÝÆ÷¼¯Èº
ÉúÑIJ¢Í˳öÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯ÈÝÆ÷¼¯Èº£º
$ sudo docker-compose up -d
µÇ¼ºó¸´ÖÆ
ͨ¹ý-d²ÎÊý¿ÉʹÈÝÆ÷ÔÚºǫ́ÔËÐС£
°ì·¨Î壺ÑéÖ¤ÈÝÆ÷״̬
ʹÓÃÒÔÏÂÏÂÁî¿ÉÒÔÉó²éÈÝÆ÷µÄ״̬£º
$ sudo docker-compose ps
µÇ¼ºó¸´ÖÆ
¿ÉÒÔ¿´µ½ÈÝÆ÷µÄ״̬ÊÇrunning£¬ÌåÏÖÈÝÆ÷ÔËÐÐÕý³£¡£
°ì·¨Áù£º²âÊÔÈÝÆ÷¼¯Èº
ÏÖÔÚ£¬¿ÉÒÔ²âÊÔMySQLÈÝÆ÷¼¯ÈºÊÇ·ñÕý³£ÊÂÇé¡£Ê×ÏÈ£¬ÅþÁ¬µ½MySQLÈÝÆ÷µÄÖ÷½Úµã£¬²¢½¨ÉèÒ»¸ö²âÊÔÊý¾Ý¿â£º
$ sudo docker exec -it mysql1 mysql -uroot -p Enter password: your_password mysql> CREATE DATABASE test; mysql> EXIT;
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÅþÁ¬µ½´Ó½Úµã£¬²¢ÑéÖ¤ÊÇ·ñÄܹ»¶ÁÈ¡µ½Ö÷½ÚµãÉϵÄÊý¾Ý£º
$ sudo docker exec -it mysql2 mysql -uroot -p Enter password: your_password mysql> USE test; mysql> SELECT * FROM your_table; mysql> EXIT;
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬your_tableΪÄãÔÚÖ÷½ÚµãÉϽ¨ÉèµÄ±íÃû¡£
ͨ¹ýÒÔÉÏ°ì·¨£¬¾ÍÀֳɴÁ˸߿ÉÓõÄMySQLÈÝÆ÷¼¯Èº¡£Í¨¹ýÔÚ¶à¸ö´Ó½ÚµãÉϸ´ÖÆÖ÷½ÚµãµÄÊý¾Ý£¬¿ÉÒÔʵÏÖÊý¾ÝµÄÈßÓàºÍ¸ß¿ÉÓÃÐÔ¡£ÈôÊÇÖ÷½Úµã·ºÆð¹ÊÕÏ£¬¿ÉÒÔ´Ó´Ó½ÚµãÖÐÑ¡¾ÙеÄÖ÷½Úµã£¬²¢¼ÌÐøÌṩЧÀÍ¡£
Ï£Íû±¾ÎÄÄܹ»¶ÔÄãÔÚLinuxÉϴ¸ß¿ÉÓõÄMySQLÈÝÆ÷¼¯ÈºÓÐËù×ÊÖú¡£×£ÄãÀֳɣ¡
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉϴ¸ß¿ÉÓõÄMySQLÈÝÆ÷¼¯Èº£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡