How to install mongodb-4.4 as systemd service with podman

1、安装

1.1、创建卷

  • 配置卷
podman volume create --label type=mongo-4.4 --label env=dev mongo-4.4-conf
  • 数据卷
podman volume create --label type=mongo-4.4 --label env=dev mongo-4.4-data
  • 查看卷
lwk@qwfys:~$ podman volume inspect mongo-4.4-conf 
[
    {
        "Name": "mongo-4.4-conf",
        "Driver": "local",
        "Mountpoint": "/home/lwk/.local/share/containers/storage/volumes/mongo-4.4-conf/_data",
        "CreatedAt": "2023-10-20T01:03:51.772469157+08:00",
        "Labels": {
            "env": "dev",
            "type": "mongo-4.4"
        },
        "Scope": "local",
        "Options": {}
    }
]
lwk@qwfys:~$

1.2、配置文件

touch ~/.local/share/containers/storage/volumes/mongo-4.4-conf/_data/mongod.conf
lwk@qwfys:~$ ll ~/.local/share/containers/storage/volumes/mongo-4.4-conf/_data/mongod.conf
-rw-rw-r-- 1 lwk lwk 0 Oct 20 01:06 /home/lwk/.local/share/containers/storage/volumes/mongo-4.4-conf/_data/mongod.conf
lwk@qwfys:~$

1.3、创建容器

这里,我们基于卷创建容器

podman create  \
--restart always \
--publish 37017:27017 \
--name mongo-4.4.25 \
--volume mongo-4.4-conf:/etc/mongo \
--volume mongo-4.4-data:/data/db \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MONGO_INITDB_ROOT_USERNAME=admin \
--env MONGO_INITDB_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mongo:4.4.25-focal \
--config /etc/mongo/mongod.conf

1.4、服务管理

  • 生成服务

我们借助命令podman generate systemd生成一个用户级服务

podman generate systemd --new mongo-4.4.25 > ~/.config/systemd/user/mongo-4.4.25.service
  • 刷新服务
systemctl --user daemon-reload
  • 查看服务
lwk@qwfys:~$ systemctl --user status mongo-4.4.25.service 
○ mongo-4.4.25.service - Podman container-609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62.service
     Loaded: loaded (/home/lwk/.config/systemd/user/mongo-4.4.25.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:podman-generate-systemd(1)
lwk@qwfys:~$
  • 开机运行
lwk@qwfys:~$ systemctl --user enable mongo-4.4.25.service 
Created symlink /home/lwk/.config/systemd/user/default.target.wants/mongo-4.4.25.service → /home/lwk/.config/systemd/user/mongo-4.4.25.service.
lwk@qwfys:~$
  • 启动服务
lwk@qwfys:~$ systemctl --user start mongo-4.4.25.service 
lwk@qwfys:~$ systemctl --user status mongo-4.4.25.service 
● mongo-4.4.25.service - Podman container-609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62.service
     Loaded: loaded (/home/lwk/.config/systemd/user/mongo-4.4.25.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-10-20 01:13:09 CST; 8s ago
       Docs: man:podman-generate-systemd(1)
    Process: 6170 ExecStartPre=/bin/rm -f /run/user/1000/mongo-4.4.25.service.ctr-id (code=exited, status=0/SUCCESS)
   Main PID: 6237 (conmon)
      Tasks: 25 (limit: 18850)
     Memory: 20.1M
        CPU: 319ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/mongo-4.4.25.service
             ├─6212 /usr/bin/slirp4netns --disable-host-loopback --mtu=65520 --enable-sandbox --enable-seccomp -c -e 3 -r 4 --netns-type=path /run/user/1000/netns/cni-401d59f2-40bb-f9bf-f85>
             ├─6214 containers-rootlessport
             ├─6225 containers-rootlessport-child
             └─6237 /usr/bin/conmon --api-version 1 -c bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2 -u bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2 ->

Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.551+08:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message">
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.573+08:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationM>
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.573+08:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":>
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.586+08:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.614+08:00"},"s":"I",  "c":"STORAGE",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deplo>
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.618+08:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic dat>
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.619+08:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","att>
Oct 20 01:13:12 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:12.621+08:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mong>
lwk@qwfys:~$
  • 查看日志
lwk@qwfys:~$ journalctl --user --unit mongo-4.4.25
Oct 20 01:13:08 qwfys systemd[1865]: Starting Podman container-609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62.service...
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:09.022804009 +0800 CST m=+0.094267064 container remove 609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62 (image=docker.io>
Oct 20 01:13:09 qwfys podman[6171]: 609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:08.961494548 +0800 CST m=+0.032957581 image pull  mongo:4.4.25-focal
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:09.085400286 +0800 CST m=+0.156863341 volume create 2f705d6769b4c0a4e685e87a028c8a83b3d5126bacd4cf9c3cd4074f3bb4e505
Oct 20 01:13:09 qwfys podman[6171]: 
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:09.098456917 +0800 CST m=+0.169919975 container create bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2 (image=docker.io>
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:09.237984454 +0800 CST m=+0.309447477 container init bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2 (image=docker.io/l>
Oct 20 01:13:09 qwfys systemd[1865]: Started Podman container-609056b175d2467e7749f014726b0831a53cb0e8bcc258085ce72b0d6b4f3a62.service.
Oct 20 01:13:09 qwfys podman[6171]: 2023-10-20 01:13:09.246006469 +0800 CST m=+0.317469493 container start bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2 (image=docker.io/>
Oct 20 01:13:09 qwfys podman[6171]: bd577b9f89912a2d843799e8ae042d3694365049d33ad907c80aeef79effa6e2
Oct 20 01:13:09 qwfys conmon[6237]: about to fork child process, waiting until server is ready for connections.
Oct 20 01:13:09 qwfys conmon[6237]: forked process: 23
Oct 20 01:13:09 qwfys conmon[6237]: 
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.526+08:00"},"s":"I",  "c":"CONTROL",  "id":20698,   "ctx":"main","msg":"***** SERVER RESTARTED *****"}
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.534+08:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enab>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.541+08:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP Fast>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":23,"p>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"ver>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{>
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.544+08:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly >
Oct 20 01:13:09 qwfys conmon[6237]: {"t":{"$date":"2023-10-20T01:13:09.544+08:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":>
lwk@qwfys:~$ 

1.5、容器管理

  • 日志查看
lwk@qwfys:~$ podman logs -f mongo-4.4.25 
about to fork child process, waiting until server is ready for connections.
forked process: 23

{"t":{"$date":"2023-10-20T01:13:09.526+08:00"},"s":"I",  "c":"CONTROL",  "id":20698,   "ctx":"main","msg":"***** SERVER RESTARTED *****"}
{"t":{"$date":"2023-10-20T01:13:09.534+08:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2023-10-20T01:13:09.541+08:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":23,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"bd577b9f8991"}}
{"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.25","gitVersion":"3e18c4c56048ddf22a6872edc111b542521ad1d5","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
{"t":{"$date":"2023-10-20T01:13:09.542+08:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"config":"/etc/mongo/mongod.conf","net":{"bindIp":"127.0.0.1","port":27017,"tls":{"mode":"disabled"}},"processManagement":{"fork":true,"pidFilePath":"/tmp/docker-entrypoint-temp-mongod.pid"},"systemLog":{"destination":"file","logAppend":true,"path":"/proc/1/fd/1"}}}}
{"t":{"$date":"2023-10-20T01:13:09.544+08:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
{"t":{"$date":"2023-10-20T01:13:09.544+08:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=7406M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
{"t":{"$date":"2023-10-20T01:13:10.055+08:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1697735590:55567][23:0x7f66e3aa0cc0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global recovery timestamp: (0, 0)"}}
{"t":{"$date":"2023-10-20T01:13:10.055+08:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1697735590:55677][23:0x7f66e3aa0cc0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global oldest timestamp: (0, 0)"}}
{"t":{"$date":"2023-10-20T01:13:10.081+08:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":537}}
{"t":{"$date":"2023-10-20T01:13:10.081+08:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2023-10-20T01:13:10.123+08:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
{"t":{"$date":"2023-10-20T01:13:10.136+08:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
{"t":{"$date":"2023-10-20T01:13:10.144+08:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"3474538b-cd88-452a-9a7b-d2666ceb9b9c"}},"options":{"uuid":{"$uuid":"3474538b-cd88-452a-9a7b-d2666ceb9b9c"}}}}
{"t":{"$date":"2023-10-20T01:13:10.179+08:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"admin.system.version","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2023-10-20T01:13:10.179+08:00"},"s":"I",  "c":"COMMAND",  "id":20459,   "ctx":"initandlisten","msg":"Setting featureCompatibilityVersion","attr":{"newVersion":"4.4"}}
{"t":{"$date":"2023-10-20T01:13:10.182+08:00"},"s":"I",  "c":"STORAGE",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
{"t":{"$date":"2023-10-20T01:13:10.185+08:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"fbb84523-f56b-48ad-9d5b-f6cc5d009214"}},"options":{"capped":true,"size":10485760}}}
{"t":{"$date":"2023-10-20T01:13:10.217+08:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"local.startup_log","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2023-10-20T01:13:10.218+08:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
{"t":{"$date":"2023-10-20T01:13:10.218+08:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
{"t":{"$date":"2023-10-20T01:13:10.221+08:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
{"t":{"$date":"2023-10-20T01:13:10.221+08:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
{"t":{"$date":"2023-10-20T01:13:10.221+08:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
^Clwk@qwfys:~$ 

2、客户端管理

这里,我们采用podman临时启动一个客户端来连接上面已经通过podman启动的服务器。

podman run -it --rm mongo:4.4.25-focal \
	mongo --host 192.168.101.129 \
   	--port 37017 \
	--username admin \
	--password Gah6kuP7ohfio4 \
	--authenticationDatabase admin \
	harbin

上面这个代码片断,我们采用账号admin连接到数据库harbin。

接下来是真实的样例,以供大家参考。

lwk@qwfys:~$ podman run -it --rm mongo:4.4.25-focal \
        mongo --host 192.168.101.129 \
   		--port 37017 \
        --username admin \
        --password Gah6kuP7ohfio4 \
        --authenticationDatabase admin \
        harbin
MongoDB shell version v4.4.25
connecting to: mongodb://192.168.101.129:37017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b08d4df1-1e8c-43f6-8a6a-4f8d0785b606") }
MongoDB server version: 4.4.25
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2023-10-23T11:43:08.188+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
---
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> exit
bye
lwk@qwfys:~$ 

或者采用下这种方式也是可以的。

podman run -it --rm mongo:4.4.25-focal \
  mongo \
    --username=admin \
    --password=Gah6kuP7ohfio4 \
    mongodb://192.168.101.129:37017/harbin?authSource=admin

对应的真实样例如下:

lwk@qwfys:~$ podman run -it --rm mongo:4.4.25-focal mongo --username=admin --password=Gah6kuP7ohfio4 mongodb://192.168.101.129:37017/harbin?authSource=admin
MongoDB shell version v4.4.25
connecting to: mongodb://192.168.101.129:37017/harbin?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b9a28fde-2240-4209-9d4a-539da25b3b44") }
MongoDB server version: 4.4.25
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2023-10-23T11:43:08.188+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
---
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> exit
bye
lwk@qwfys:~$

至些,我们已经基于podman完成了mongodb 4.4.25的安装。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
后台采用apache服务器下的cgi处理c语言做微信小程序后台逻辑的脚本映射。PC端的服务器和客户端都是基于c语言写的。采用mysql数据库进行用户数据和聊天记录的存储。.zip C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qwfys200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值