Memcached:Win/Linux环境安装及一键部署脚本

1. Win安装

Windows安装Memcached

2. Linux安装

2.1 安装依赖环境

sudo yum install -y libevent-devel

2.2 下载 Memcached

cd /tmp
wget https://memcached.org/files/memcached-1.6.12.tar.gz

2.3 解压 Memcached

tar xzf memcached-1.6.12.tar.gz

2.4 编译安装 Memcached

cd memcached-1.6.12
./configure && make && sudo make install

2.5 配置 Memcached

创建一个 Memcached 配置文件 /etc/memcached.conf,并添加以下内容:

# 监听的 IP 地址和端口号
-l 127.0.0.1
-p 11211

# 内存限制(单位是 MB)
-m 64

# 后台运行
-d

2.6 赋权

sudo chmod +x /usr/local/bin/memcached

2.7 启动 Memcached

memcached -d -m 64 -u root -l 127.0.0.1 -p 11211

2.8 验证 Memcached 是否启动成功

sudo netstat -lnpt | grep memcached

如果看到输出中有 127.0.0.1:11211,则表示 Memcached 启动成功。

2.9 添加到系统服务

(1)创建systemd服务文件

使用root权限创建/usr/lib/systemd/system/memcached.service文件,文件内容如下:

[Unit]
Description=memcached daemon

[Service]
Type=simple
ExecStart=/usr/local/bin/memcached -m 64 -u root -l 127.0.0.1 -p 11211
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
User=root

[Install]
WantedBy=multi-user.target

(2)启用服务,执行以下命令:

sudo systemctl daemon-reload
sudo systemctl enable memcached
sudo systemctl start memcached

(3)确认服务是否已经启动,执行以下命令:

sudo systemctl status memcached

如果服务状态显示为"active (running)",则表示服务已经正常运行。

2.10 sudo 命令执行失败问题

问题描述

[root@linkhot01 memcached-1.6.12]# sudo memcached -d -m 64 -u root -l 127.0.0.1 -p 11211
sudo: memcached:找不到命令

报错说明:这个错误提示显示系统找不到 memcached 命令,有可能是因为 memcached 没有正确安装或者没有正确配置系统环境变量。

分析结果:如果将sudomemcached命令连在一起执行,会导致系统无法找到memcached命令。这是因为在使用sudo时,系统会默认使用root用户环境变量中的PATH变量,而非当前用户的环境变量。

因此,在使用sudo命令时,需要使用绝对路径或者将memcached命令加入root用户的环境变量中,才能够正常执行memcached命令。

3. 一键部署脚本

#!/bin/bash

# 安装依赖库
sudo yum install libevent-devel -y

# 下载、编译和安装 memcached
cd /usr/local/src
sudo wget http://www.memcached.org/files/memcached-1.6.12.tar.gz
sudo tar -zxvf memcached-1.6.12.tar.gz
cd memcached-1.6.12/
sudo ./configure --prefix=/usr/local/memcached
sudo make && sudo make install

# 添加 memcached 环境变量
echo 'export PATH=$PATH:/usr/local/memcached/bin' | sudo tee -a /etc/profile
source /etc/profile

# 创建 memcached 用户
sudo useradd -s /sbin/nologin -d /dev/null memcached

# 生成服务文件
sudo sh -c "cat > /usr/lib/systemd/system/memcached.service << EOF
[Unit]
Description=Memcached
After=network.target

[Service]
Type=simple
User=memcached
Group=memcached
Environment='PATH=/usr/local/memcached/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'
ExecStart=/usr/local/memcached/bin/memcached -d -m 64 -u memcached -l 127.0.0.1 -p 11211
ExecStop=/usr/local/memcached/bin/memcached -d -m 64 -u memcached -l 127.0.0.1 -p 11211 -s /tmp/memcached.sock -S -X
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
"

# 启动 memcached 服务并设置开机自启
sudo systemctl start memcached
sudo systemctl enable memcached

这个脚本会下载、编译和安装 memcached,添加 memcached 环境变量,创建 memcached 用户,生成服务文件,启动 memcached 服务并设置开机自启。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个CentOS一键部署OpenStack的Shell脚本,使用该脚本可以方便快速地搭建OpenStack环境: ```bash #!/bin/bash #更新系统 yum update -y #安装epel源 yum install epel-release -y #安装openstack源 yum install centos-release-openstack-ocata -y #安装openstack客户端和python-openstackclient yum install python-openstackclient -y #安装MariaDB数据库 yum install mariadb mariadb-server python2-PyMySQL -y #配置MariaDB数据库 systemctl enable mariadb.service systemctl start mariadb.service mysql_secure_installation #安装rabbitmq消息队列 yum install rabbitmq-server -y #配置rabbitmq消息队列 systemctl enable rabbitmq-server.service systemctl start rabbitmq-server.service rabbitmqctl add_user openstack RABBIT_PASS rabbitmqctl set_permissions openstack ".*" ".*" ".*" #安装memcached缓存服务器 yum install memcached python-memcached -y #配置memcached缓存服务器 systemctl enable memcached.service systemctl start memcached.service #安装httpd服务器 yum install httpd mod_wsgi -y #配置httpd服务器 systemctl enable httpd.service systemctl start httpd.service #安装OpenStack Keystone服务 yum install openstack-keystone httpd mod_wsgi -y #配置OpenStack Keystone服务 systemctl enable httpd.service systemctl start httpd.service su -s /bin/sh -c "keystone-manage db_sync" keystone keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone keystone-manage credential_setup --keystone-user keystone --keystone-group keystone keystone-manage bootstrap --bootstrap-password ADMIN_PASS \ --bootstrap-admin-url http://controller:35357/v3/ \ --bootstrap-internal-url http://controller:5000/v3/ \ --bootstrap-public-url http://controller:5000/v3/ \ --bootstrap-region-id RegionOne #重启httpd服务 systemctl restart httpd.service #安装OpenStack Glance服务 yum install openstack-glance -y #配置OpenStack Glance服务 su -s /bin/sh -c "glance-manage db_sync" glance #安装OpenStack Nova服务 yum install openstack-nova-api openstack-nova-conductor \ openstack-nova-console openstack-nova-novncproxy \ openstack-nova-scheduler -y #编辑/etc/nova/nova.conf文件 cat > /etc/nova/nova.conf << EOF [DEFAULT] transport_url = rabbit://openstack:RABBIT_PASS@controller auth_strategy = keystone my_ip = MANAGEMENT_INTERFACE_IP_ADDRESS use_neutron = True firewall_driver = nova.virt.firewall.NoopFirewallDriver [api] auth_strategy = keystone [keystone_authtoken] auth_uri = http://controller:5000 auth_url = http://controller:35357 memcached_servers = controller:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = nova password = NOVA_PASS [vnc] enabled = True vncserver_listen = MANAGEMENT_INTERFACE_IP_ADDRESS vncserver_proxyclient_address = MANAGEMENT_INTERFACE_IP_ADDRESS [glance] api_servers = http://controller:9292 [oslo_concurrency] lock_path = /var/lib/nova/tmp EOF #配置OpenStack Nova服务 su -s /bin/sh -c "nova-manage api_db sync" nova su -s /bin/sh -c "nova-manage db sync" nova #重启OpenStack Nova服务 systemctl enable openstack-nova-api.service \ openstack-nova-consoleauth.service openstack-nova-scheduler.service \ openstack-nova-conductor.service openstack-nova-novncproxy.service systemctl start openstack-nova-api.service \ openstack-nova-consoleauth.service openstack-nova-scheduler.service \ openstack-nova-conductor.service openstack-nova-novncproxy.service #安装OpenStack Neutron服务 yum install openstack-neutron-linuxbridge ebtables ipset -y #编辑/etc/neutron/neutron.conf文件 cat > /etc/neutron/neutron.conf << EOF [DEFAULT] core_plugin = ml2 service_plugins = router allow_overlapping_ips = True transport_url = rabbit://openstack:RABBIT_PASS@controller auth_strategy = keystone notify_nova_on_port_status_changes = True notify_nova_on_port_data_changes = True [agent] root_helper = sudo /usr/bin/neutron-rootwrap /etc/neutron/rootwrap.conf [keystone_authtoken] auth_uri = http://controller:5000 auth_url = http://controller:35357 memcached_servers = controller:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = neutron password = NEUTRON_PASS [database] connection = mysql+pymysql://neutron:NEUTRON_DBPASS@controller/neutron [oslo_concurrency] lock_path = /var/lib/neutron/tmp EOF #编辑/etc/neutron/plugins/ml2/linuxbridge_agent.ini文件 cat > /etc/neutron/plugins/ml2/linuxbridge_agent.ini << EOF [linux_bridge] physical_interface_mappings = provider:PHYSICAL_INTERFACE_NAME [vxlan] enable_vxlan = False [securitygroup] enable_security_group = True firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver EOF #配置OpenStack Neutron服务 su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neutron.conf \ --config-file /etc/neutron/plugins/ml2/ml2_conf.ini upgrade head" neutron #重启OpenStack Neutron服务 systemctl enable neutron-server.service \ neutron-linuxbridge-agent.service neutron-dhcp-agent.service \ neutron-metadata-agent.service systemctl start neutron-server.service \ neutron-linuxbridge-agent.service neutron-dhcp-agent.service \ neutron-metadata-agent.service #安装OpenStack Horizon服务 yum install openstack-dashboard -y #重启httpd服务 systemctl restart httpd.service ``` 在运行该脚本之前,需要将以下变量修改为您自己的值: - RABBIT_PASS:RabbitMQ的密码 - NOVA_PASS:Nova的密码 - NEUTRON_PASS:Neutron的密码 - NEUTRON_DBPASS:Neutron数据库的密码 - MANAGEMENT_INTERFACE_IP_ADDRESS:管理接口的IP地址 - PHYSICAL_INTERFACE_NAME:物理接口的名称 运行该脚本后,您将得到一个完整的OpenStack环境。但是,在使用OpenStack之前,您还需要进行一些其他的配置和操作,请参考OpenStack官方文档进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值