Installed memcached via homebrew, how to start and stop server? - ruby

I have memcached installed via homebrew.
how do I start/stop the server?
Any command-line tools to interact with memcached?
does homebrew have a way of removing a package?

When you installed it, it put a file named homebrew.mxcl.memcached.plist in /usr/local/Cellar/memcached/$version/; you copy that file into ~/Library/LaunchAgents and then tell launchd to start it with launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist.
If you were watching the console carefully during the brew install command, it should have said something about doing these exact steps. If you run brew info it'll re-print this information, e.g. brew info memcached.
You should probably read https://docs.brew.sh/Manpage -- it has more useful documentation including the brew remove command which will remove the package if you decide you don't want it.

Brew used to have a command brew services (https://thoughtbot.com/blog/starting-and-stopping-background-services-with-homebrew), now deprecated. Instead, to get always-correct advice, run this command:
brew info memcached
Via telnet: telnet localhost 11211
See also What are some useful tips/tools for monitoring/tuning memcached health?
brew remove memcached

Additionally you can run "brew info", if you have forgotten about the instructions.
→ brew info memcached
memcached 1.4.7
http://memcached.org/
Depends on: libevent
/usr/local/Cellar/memcached/1.4.6 (8 files, 156K)
You can enable memcached to automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/memcached/1.4.7/com.danga.memcached.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/com.danga.memcached.plist
Or start it manually:
/usr/local/bin/memcached
Add "-d" to start it as a daemon.
http://github.com/mxcl/homebrew/commits/master/Library/Formula/memcached.rb

To restart:
If you have the memcached starting up with launchd and your plist file has
<key>KeepAlive</key>
<true/>
Then you can just kill the process and it will reboot automagically.
ps ux | grep memcached
pkill -f memcached
ps ux | grep memcached
To stop launchd from restarting automatically:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
To add memcached to launchd again:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
To uninstall something installed by homebrew:
brew uninstall memcached

And you can also create aliases
alias memcached-start="launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached-stop="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached-restart="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist;launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
and after that you cant easy start/stop/restart memcached
memcached-start
memcached-stop
memcached-restart

You can also use Lunchy to set the start/stop. Lunchy is a wrapper written over launchctl. I’ve written a detailed post about this.
$ gem install lunchy
$ mkdir ~/Library/LaunchAgents
$ cp /usr/local/Cellar/memcached/$version/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/
To start memcached
$ lunchy start memcached
To stop memcahed
$ lunchy stop memcached

You can enable Memcached to automatically load on login.
This first line creates a symlink (symbolic link) from where Homebrew installed it to the LaunchAgents folder.
ln -sfv /usr/local/Cellar/memcached/1.4.17/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/
Then to launch it now:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

Related

Alias for Start/Stop Postgres as root

I have installed Postgres on my M1 mac using a dmg file which I downloaded from their website. The homebrew installation was causing some issues, that's why I installed it this way.
Previously with homebrew I could start/stop postgres using brew services start/stop postgres. Currently I have to do this
sudo -u postgres pg_ctl -D /Library/PostgreSQL/14/data start/stop to start/stop the server. I tried to make things a bit easier by adding an alias to .zshrc file, but as I learned I can't just add a command to shell with sudo like that.
My question is there any way to add an alias to simplify the start/stop of postgres.
Use a function instead of an alias:
start_postgres() {sudo -u postgres pg_ctl -D /Library/PostgreSQL/14/data start}

Can't change document root for apache 2.4

I want to set up local web server using Apache and PHP on my Mac running Sierra. I followed this tutorial:
https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions
And I am stuck at changing the document root from httpd.config, for whatever reason this change does not take effect at all. If I type http://localhost it still says "It works", it still uses the original html file.
Another weird thing is that apparently I can't stop apache at all. I tried running
$ sudo apachectl stop
but I still can’t access http://localhost
if I run
$ brew services list
httpd24 is stopped. What gives? What am I doing wrong?
There is an instance of apache pre-installed with macOS. So you have now two versions of apache installed. If you check:
which apachectl
you will probably obtain:
/usr/sbin/apachectl
which is the official macOS version of apachectl. Stop it with:
sudo apachectl stop
Disable the auto-loading of the pre-installed apache with (as explained in the tutorial you mentioned):
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
To run the homebrew version do:
brew services run httpd24
to stop it:
brew services stop httpd24
to register it to launch at login:
brew services start httpd24

How to shut down rethinkdb running on Mac OSX

Note: We love RethinkDB but our current project does not use it so we need to free up resources on our local machine ...
We installed RethinkDB using homebrew:
brew update && brew install rethinkdb
And added the rethinkdb process to our plist as per the instructions in:
http://rethinkdb.com/docs/start-on-startup/
But now we need to shut it down to free-up the 8080 port for another service.
What command do we need to run? (thanks!)
Do this in the terminal
check to see if you started it using brew
$ brew services list
stop rethinkdb:
$ brew services stop rethinkdb
restart:
$ brew services start rethinkdb
Like this, I think, if you used launchctl:
sudo launchctl unload -w /Library/LaunchDaemons/com.rethinkdb.server.plist

How do I start MariaDB on boot on Mac OS X?

Just installed MariaDB (with homebrew). Everything looks like it's working, but I can't figure out how to have it automatically startup on boot on my Mac. I can't find any Mac-specific docs for this.
The installation output says:
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
I guess I don't know where the right place is.
From brew info mariadb
To have launchd start mariadb now and restart at login:
brew services start mariadb
Or, if you don't want/need a background service you can just run:
mysql.server start
Just run brew services start mariadb on terminal.
With help from Calvin's answer (deleted, I guess?), and this page, these are the steps I used to accomplish this:
cp /usr/local/Cellar/mariadb/5.5.30/homebrew.mxcl.mariadb.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
On next boot, MariaDB was up and running.
If you install MariaDB by Homebrew, you can use this to see how to start your mariadb at login.
brew info mariadb
To have launchd start mariadb at login:
ln -sfv /usr/local/opt/mariadb/*.plist ~/Library/LaunchAgents
Then to load mariadb now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
You need to use launchd. See $ man launchd.
Additionally, Nathan wrote a good article on launchd.

How can I start PostgreSQL server on Mac OS X?

Final update:
I had forgotten to run the initdb command.
By running this command
ps auxwww | grep postgres
I see that postgres is not running
> ps auxwww | grep postgres
remcat 1789 0.0 0.0 2434892 480 s000 R+ 11:28PM 0:00.00 grep postgres
This raises the question:
How do I start the PostgreSQL server?
Update:
> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
sh: /usr/local/var/postgres/server.log: No such file or directory
Update 2:
The touch was not successful, so I did this instead:
> mkdir /usr/local/var/postgres
> vi /usr/local/var/postgres/server.log
> ls /usr/local/var/postgres/
server.log
But when I try to start the Ruby on Rails server, I still see this:
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
Update 3:
> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running
Update 4:
I found that there wasn't any pg_hba.conf file (only file pg_hba.conf.sample), so I modified the sample and renamed it (to remover the .sample). Here are the contents:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
But I don't understand this:
> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running
Also:
sudo find / -name postgresql.conf
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
Update 5:
sudo pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
Update 6:
This seems odd:
> egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
egrep: /usr/local/var/postgres/postgresql.conf: No such file or directory
Though, I did do this:
>sudo find / -name "*postgresql.conf*"
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
/usr/share/postgresql/postgresql.conf.sample
So I did this:
egrep 'listen|port' /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
#listen_addresses = 'localhost' # what IP address(es) to listen on;
#port = 5432 # (change requires restart)
# supported by the operating system:
# %r = remote host and port
So I tried this:
> cp /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf
> cp /usr/share/postgresql/postgresql.conf.sample /usr/share/postgresql/postgresql.conf
I am still getting the same "Is the server running?" message.
The Homebrew package manager includes launchctl plists to start automatically. For more information, run brew info postgres.
Start manually
pg_ctl -D /usr/local/var/postgres start
Stop manually
pg_ctl -D /usr/local/var/postgres stop
Start automatically
"To have launchd start postgresql now and restart at login:"
brew services start postgresql
What is the result of pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start?
What is the result of pg_ctl -D /usr/local/var/postgres status?
Are there any error messages in the server.log?
Make sure tcp localhost connections are enabled in pg_hba.conf:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
Check the listen_addresses and port in postgresql.conf:
egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
#listen_addresses = 'localhost' # What IP address(es) to listen on;
#port = 5432 # (change requires restart)
Cleaning up
PostgreSQL was most likely installed via Homebrew, Fink, MacPorts or the EnterpriseDB installer.
Check the output of the following commands to determine which package manager it was installed with:
brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres
If you want to manually start and stop PostgreSQL (installed via Homebrew), the easiest way is:
brew services start postgresql
and
brew services stop postgresql
If you have a specific version, make sure to suffix the version. For example:
brew services start postgresql#10
I had almost the exact same issue, and you cited the initdb command as being the fix. This was also the solution for me, but I didn't see that anyone posted it here, so for those who are looking for it:
initdb /usr/local/var/postgres -E utf8
If your computer was abruptly restarted
You may want to start PG server but it was not.
First, you have to delete the file /usr/local/var/postgres/postmaster.pid Then you can restart the service using one of the many other mentioned methods depending on your install.
You can verify this by looking at the logs of Postgres to see what might be going on: tail -f /usr/local/var/postgres/server.log
For specific version:-
tail -f /usr/local/var/postgres#[VERSION_NUM]/server.log
Eg:
tail -f /usr/local/var/postgres#11/server.log
Another approach is using the lunchy gem (a wrapper for launchctl):
brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy
To start PostgreSQL:
lunchy start postgres
To stop PostgreSQL:
lunchy stop postgres
For further information, refer to: "How to Install PostgreSQL on a Mac With Homebrew and Lunchy"
Here my two cents: I made an alias for postgres pg_ctl and put it in file .bash_profile (my PostgreSQL version is 9.2.4, and the database path is /Library/PostgreSQL/9.2/data).
alias postgres.server="sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.2/data"
Launch a new terminal.
And then? You can start/stop your PostgreSQL server with this:
postgres.server start
postgres.server stop
The cleanest way by far to start/stop/restart PostgreSQL if you have installed it through brew is to simply unload and/or load the launchd configuration file that comes with the installation:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
The first line will stop PostgreSQL and the second line will start it. There isn't any need to specify any data directories, etc. since everything is in that file.
To start the PostgreSQL server:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
To end the PostgreSQL server:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
You can also create an alias via CLI to make it easier:
alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'
With these you can just type "pg-start" to start PostgreSQL and "pg-stop" to shut it down.
For test purposes, I think PostgreSQL App is the best option!
Run an app, and the server is up and running.
Close the app, and the server goes down.
http://postgresapp.com/
If you have installed using Homebrew, the below command should be enough.
brew services restart postgresql
This sometimes might not work. In that case, the below two commands should definitely work:
rm /usr/local/var/postgres/postmaster.pid
pg_ctl -D /usr/local/var/postgres start
# Remove old database files (if there was any)
$ rm -rf /usr/local/var/postgres
# Install the binary
$ brew install postgresql
# init it
$ initdb /usr/local/var/postgres
# Start the PostgreSQL server
$ postgres -D /usr/local/var/postgres
# Create your database
$ createdb mydb
# Access the database
$ psql mydb
psql (9.0.1)
Type "help" for help.
Sometimes it's just the version which you are missing, and you are scratching your head unnecessarily.
If you are using a specific version of PostgreSQL, for example, PostgreSQL 10, then simply do
brew services start postgresql#10
brew services stop postgresql#10
The normal brew services start postgresql won't work without a version if you have installed it for a specific version from Homebrew.
When you install PostgreSQL using Homebrew,
brew install postgres
at the end of the output, you will see this methods to start the server:
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
I think this is the best way.
You can add an alias into your .profile file for convenience.
I had the same problem and performed all updates from the first post. But after checking the log file,
/usr/local/var/postgres/server.log
I see the true cause:
FATAL: data directory "/usr/local/var/postgres" has group or world access
DETAIL: Permissions should be u=rwx (0700).
After changing permissions on this directory,
chmod 0700 /usr/local/var/postgres
the PostgreSQL server started.
Check the log file every time.
For a quick disposable test database, you can run the server in the foreground.
Initialize a new PostgreSQL database in a new directory:
mkdir db
initdb db -E utf8
createdb public
Start the server in the foreground (Ctrl + C to stop the server):
postgres -d db
In another shell session, connect to the server
psql -d public
If you didn't install it with Homebrew and directly from the Mac package, this worked for me for PostgreSQL 12 when using all the default locations, variables, etc.
$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/12/bin/pg_ctl -D /Library/PostgreSQL/12/data/ stop
Variation on this answer: https://stackoverflow.com/a/13103603/2394728
initdb `brew --prefix`/var/postgres/data -E utf8`` && pg_ctl -D /usr/local/var/postgres/data -l logfile start
PostgreSQL is integrated in Server.app available through the App Store in Mac OS X v10.8 (Mountain Lion). That means that it is already configured, and you only need to launch it, and then create users and databases.
Tip: Do not start with defining $PGDATA and so on. Take file locations as is.
You would have this file:
/Library/Server/PostgreSQL/Config/org.postgresql.postgres.plist
To start:
sudo serveradmin start postgres
Process started with arguments:
/Applications/Server.app/Contents/ServerRoot/usr/bin/postgres_real -D /Library/Server/PostgreSQL/Data -c listen_addresses=127.0.0.1,::1 -c log_connections=on -c log_directory=/Library/Logs/PostgreSQL -c log_filename=PostgreSQL.log -c log_line_prefix=%t -c log_lock_waits=on -c log_statement=ddl -c logging_collector=on -c unix_socket_directory=/private/var/pgsql_socket -c unix_socket_group=_postgres -c unix_socket_permissions=0770
You can sudo:
sudo -u _postgres psql template1
Or connect:
psql -h localhost -U _postgres postgres
You can find the data directory, version, running status and so forth with
sudo serveradmin fullstatus postgres
For development purposes, one of the simplest ways is to install Postgres.app from the official site. It can be started/stopped from Applications folder or using the following commands in terminal:
# Start
open -a Postgres
# Stop
killall Postgres
killall postgres
This worked for me (macOS v10.13 (High Sierra)):
sudo -u postgres /Library/PostgreSQL/9.6/bin/pg_ctl start -D /Library/PostgreSQL/9.6/data
Or first
cd /Library/PostgreSQL/9.6/bin/
If you installed PostgreSQL using the EnterpriseDB installer, then what Kenial suggested is the way to go:
sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data start
sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data stop
Homebrew is the way!!
To start the service:
brew services start postgresql
To list it:
brew services list | grep postgres
To stop the service:
brew services stop postgresql
If you didn't install the Postgres server with Homebrew or installed using .dmg file, try this:
$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/13/bin/pg_ctl -D /Library/PostgreSQL/13/data/ stop
For MacPorts, just use the load/unload command and the port name of the running server:
sudo port load postgresql96-server
- or -
sudo port unload postgresql96-server
so you don't have to remember where the /Library/LaunchDaemons/org.macports.postgresql96.plist file is located.
having installed Postgres with homebrew that is what I do to start postgres and keep it in foreground to see the logs:
/opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres
install postgresql using brew: brew install postgresql, you can specify the version using "#" sign: brew install postgresql#14
start postgresql: brew services start postgresql or specific version brew services start postgresql#14
stop postgresql: brew services stop postgresql
$ brew upgrade postgres
fixed it for me.
That, of course, will upgrade your PostgreSQL version and update/install any dependencies.
Warning: Do this knowing that your PostgreSQL version will likely change. For me, that wasn't a big deal.
None of the previous answers fixed the issue for me, despite getting the same error messages.
I was able to get my instance back up and running by deleting the existing postmaster.pid file which was locked and was not allowing connections.
This worked for me every time, inspired by Craig Ringer:
brew install proctools
sudo pkill -u postgres
proctools includes pkill. If you don't have Homebrew: https://brew.sh/
After doing brew services restart postgresql.
It works best to:
brew services stop postgresql
brew postgresql-upgrade-database
brew services start postgresql
Then type: psql
it now runs this was after the error:
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
The upgrade may be optional depending on the other dependencies your running.
Which means that rather than Restart using brew for in on mac os, Stop completely postgres and then start postgres and connect to your psql databaseName.
Hope this was useful.

Resources