Can't write to .bash_profile file even when I am admin - mysql

I am trying to fix the problem with MySQL Server. When I type in terminal MySQL I get command not found.
I know I have to edit .bash_profile file and add to it this line:
export PATH=${PATH}:/usr/local/mysql/bin
The problem is I can't write to it. I tried these commands:
brackets ~/.bask_profile
sudo brackets ~/.bash_profile
touch ~/.bash_profile; open ~/.bash_profile
sudo touch ~/.bash_profile; open ~/.bash_profile
All the time I get a message that I am not allowed to write to this file because it is protected.
My question is, how can I write to this file and start MySQL Server?
Thank you for your help.
Cheers
EDIT:
Output in terminal of command:
ls -l ~/.bash_profile
is
-rw-r--r-- 1 root staff 447 13 paź 17:33 /Users/macos/.bash_profile
EDIT2:
Could you write to me about how to find the path to the .bash_profile file? I think changing the privileges of this file will be the solution to the problem.

The solution was using vi/vim editor:
vi ~/.bash_profile
Then in vi/vim I pasted this line:
export PATH=${PATH}:/usr/local/mysql/bin
And saved it with this commands:
:wq!

Related

Mysqldump isn't working: "command not found"

I have to export a database from the command line. I tried using this command:
mysqldump -u root -p db_name > backup.sql
But it returns this error:
-bash: mysqldump: command not found
After this, I also tried with
sudo mysqldump
but the error is the same.
I'm at the beginning and I'm not very good at it at the moment. If I have to work on directory, please be clear because I'm not confident with the terminal.
If you have the latest mysql installation in El Capitan, the mysqldump executable should be in the /usr/local/mysql/bin directory.
In order to use it, you can either run /usr/local/mysql/bin/mysqldump directly, create a symlink, or add the whole bin directory to your path, so you can use any of the executable files without typing the full path.
As suggested below, you can easily make a symlink in your /usr/bin directory, which should already be in your path, by running this command: ln -s /usr/bin/mysqldump /usr/local/mysql/bin/mysqldump
That command should create a link called mysqldump in your /usr/bin directory, which will redirect to the full path of the mysqldump program.
If you would rather add the entire mysql library of tools, all at once, you can follow this guide: https://coolestguidesontheplanet.com/add-shell-path-osx/ and learn how to add new directories to your path.
If you not installed MySql.
Ubuntu
sudo apt update
sudo apt-get install mysql-client
Add a semi-colon to the end of your command, it could make all the difference. I was getting the same error and that fixed it for me.
I'd also suggest declaring everything explicitly in the command you're running. The following worked for me:
1) Find the direct path to your mysqldump file. Check usr/local/mysql/bin/mysqldump if installed using MySQL Server DMG, or if you're using homebrew check in usr/local/Cellar/mysql... (even just do a spotlight search for it).
2) Create a folder to dump the backup to. I made mine ~/dumps.
3) Tie it all together, ensuring you have a semi-colon at the end!
/usr/local/mysql/bin/mysqldump -u root -p db_name > ~/dumps/db_name.sql ;

Copy my.cnf file to Mysql Directory with Command Line

I am trying to copy a new my.cnf to my mysql5 directory in the command line. I have used:
cp my.cnf /opt/local/etc/mysql5
However when I execute the command, I get Permission Denied.
I am the directory where my.cnf resides.
Thanks for your help.
You are trying to copy the file to root user's directory. You'll need to use sudo.
Try
$ sudo cp my.cnf /opt/local/etc/mysql5

Using MySQL in the command line in OS X - command not found?

I'm trying to get MySQL up and running on my Mac OS X 10.9.5.
I've installed the latest version 5.6.21 of MySQL Community Server. I've gone to system preferences and started the mysql server, then launched terminal and typed this:
/usr/local/mysql/bin/mysql --version
which should return the version. But when I type any of the mysql commands I get command not found.
I've also tried:
sudo mysql_secure_installation
mysql -u root --password=password`
I do have web hosting with MySQL etc installed, but I want to be able to get to grips with it in the command line first.
So there are few places where terminal looks for commands. This places are stored in your $PATH variable. Think of it as a global variable where terminal iterates over to look up for any command. This are usually binaries look how /bin folder is usually referenced.
/bin folder has lots of executable files inside it. Turns out this are command. This different folder locations are stored inside one Global variable i.e. $PATH separated by :
Now usually programs upon installation takes care of updating PATH & telling your terminal that hey i can be all commands inside my bin folder.
Turns out MySql doesn't do it upon install so we manually have to do it.
We do it by following command,
export PATH=$PATH:/usr/local/mysql/bin
If you break it down, export is self explanatory. Think of it as an assignment. So export a variable PATH with value old $PATH concat with new bin i.e. /usr/local/mysql/bin
This way after executing it all the commands inside /usr/local/mysql/bin are available to us.
There is a small catch here. Think of one terminal window as one instance of program and maybe something like $PATH is class variable ( maybe ). Note this is pure assumption. So upon close we lose the new assignment. And if we reopen terminal we won't have access to our command again because last when we exported, it was stored in primary memory which is volatile.
Now we need to have our mysql binaries exported every-time we use terminal. So we have to persist concat in our path.
You might be aware that our terminal using something called dotfiles to load configuration on terminal initialisation. I like to think of it's as sets of thing passed to constructer every-time a new instance of terminal is created ( Again an assumption but close to what it might be doing ). So yes by now you get the point what we are going todo.
.bash_profile is one of the primary known dotfile.
So in following command,
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile
What we are doing is saving result of echo i.e. output string to ~/.bash_profile
So now as we noted above every-time we open terminal or instance of terminal our dotfiles are loaded. So .bash_profile is loaded respectively and export that we appended above is run & thus a our global $PATH gets updated and we get all the commands inside /usr/local/mysql/bin.
P.s.
if you are not running first command export directly but just running second in order to persist it? Than for current running instance of terminal you have to,
source ~/.bash_profile
This tells our terminal to reload that particular file.
That means /usr/local/mysql/bin/mysql is not in the PATH variable..
Either execute /usr/local/mysql/bin/mysql to get your mysql shell,
or type this in your terminal:
PATH=$PATH:/usr/local/mysql/bin
to add that to your PATH variable so you can just run mysql without specifying the path
for me the following commands worked:
$ brew install mysql
$ brew services start mysql
You can just modified the .bash_profile by adding the MySQL $PATH as the following:
export PATH=$PATH:/usr/local/mysql/bin.
I did the following:
1- Open Terminal then $ nano .bash_profile or $ vim .bash_profile
2- Add the following PATH code to the .bash_profile
# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/mysql/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
3- Save the file.
4- Refresh Terminal using $ source ~/.bash_profile
5- To verify, type in Terminal $ mysql --version
6- It should print the output something like this:
$ mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64)
The Terminal is now configured to read the MySQL commands from $PATH which is placed in the .bash_profile .
modify your bash profile as follows
<>$vim ~/.bash_profile
export PATH=/usr/local/mysql/bin:$PATH
Once its saved you can type in mysql to bring mysql prompt in your terminal.
You have to create a symlink to your mysql installation if it is not the most recent version of mysql.
$ brew link --force mysql#5.6
see this post by Alex Todd

MySql PATH doesn't work

I'm trying to create a PATH for mysql, I create the file :
vim ~/.bash_profile
And add this to the file :
export PATH="/usr/local/mysql/bin:$PATH"
But when I do "mysql -v", it doesn't work..
when I do echo $PATH :
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/X11/bin:/usr/local/mysql/bin
My etc/path :
/usr/bin
/bin
/usr/sbin
/sbin
I did not found any answer...
Thank you!
.bash_profile is not a configuration file. It's a shell script. You have to execute it to make it have any effect.
Either logout and login again, or issue:
$ source .bash_profile

How can I access the mysql command line tool when using XAMPP in OS X?

I've got a vanilla install of XAMPP in OS X. How can I access the mysql command line tool? I've tried typing "mysql" at the command line, and it spits back "-bash: mysql: command not found".
XAMPP is installed in Mac OS X in the following directory:
/Applications/XAMPP/
You can look what's inside that directory and run mysql command line tool providing the full path to it:
$ /Applications/XAMPP/xamppfiles/bin/mysql
If you need, you can modify your PATH environment variable to include XAMPP binaries and you won't need to specify the whole path all the time.
Open your .profile file in Mac. This can be done by entering the terminal and typing
pico ~/.profile
Add the following line to your ./profile file. Replace the path where you installed Xampp, however by default this is the route and should work:
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/xampp/xamppfiles/bin:$PATH
Open a new terminal window (Recommendation is to quit all terminal windows and then reopen) and type:
mysql
That is all, isn't easy!!
Before using the mysql command, make sure that you start up the server first by running
$ mysql.server start
Then you will be able to use the commands mysqladmin and mysql.
To shut it down, run
$ mysql.server stop
and to restart just use
$ mysql.server restart
Very intuitive.
Open terminal and Follow this bellow step to add mysql to your mac environmental variable
step 1:
sudo nano ~/.bash_profile
step 2:
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/xampp/xamppfiles/bin:$PATH
save it by control+x and then y and hit return. That's it!! now close the terminal and reopen
mysql --version
this will tell you which MySQL version you are using with xampp
Since I cannot comment on the accepted answer by Pablo Santa Cruz - Here's some additional info. If you're going to modify your PATH environment variable to include XAMPP binaries, make sure you add
/Applications/XAMPP/xamppfiles/bin
and not
/Applications/XAMPP/xamppfiles/bin/mysql
to the /etc/paths file. To do this run the command
sudo nano /etc/paths
then add the path to the file. Save using Ctrl+O and exit using Ctrl+X. Quit terminal and open again.