How to use MySQL in terminal? - mysql

I am new to mySQL,on my Ubuntu 16.04.
I am learning from J.Murach's book.So the problem is when I have to create large table
CREATE TABLE invoices
(
invoice_id INT PRIMERY_KEY AUTO INCREMENT,
vendor_id INT NOT NULL,
invoice_number VARCHAR(50) NOT NULL,
...
How to type this in terminal?I do not know anything about spaces and tabs?Should I use some text editor?

Run the following commands to install MySql server on Ubuntu
sudo apt update
sudo apt install mysql-server mysql-client
Once the setup completes you should be able to use MySql by just typing the command in the Terminal
mysql -u root -p
and then the password on being prompted. You can directly enter the sql statements in the terminal then.
To exit, type
exit

If you're using XAMPP Control Panel, locate your MySQL folder in your home drive(in Windows it's C:\xampp\mysql\bin).Once you get to the bin folder, run the MySQL.exe file (again in windows: MySQL.exe -uroot -p).Replace root with the name of your administrator account. DONT change anything else. Then u should open in to the MySQL command screen. Note that in later versions of xampp, its called MariaDB and I have no idea why :-).
Happy Coding,
Anand

Related

mysql command line client for linux

I just installed mysql 5.6 in a new CentOS 7 linux installation. I want to use the mysql command line client that I have been used to using in windows, but I cannot seem to find it. Is there a mysql command line client for linux? If so, how do I make sure that I have it? And how do I open it up in the GUI?
You can open MySql command line utility using following command
mysql -u user_name -p
It will ask for password for user_name. If password is not set call as follows
mysql -u user_name
First make sure the service is running :
sudo systemctl start mysqld
Then make sure to run the security script that will remove some dangerous defaults and lock down access to our database system a little bit.
sudo mysql_secure_installation
Now you can use the command line tool
mysql --user=user_name --password=your_password db_name

how to completely delete schema data after uninstall mysql worbench on ubuntu

i deleted my workbench application completely with synaptic package and installed a fresh workbench. when i opened it, all old DB's (with tables) that have been preserved showed again. so if the workbench save my old data on my ubuntu where is it so i can delete it?
just for give you more details i already delete cache and even the /.mysql/workbench/ folder and it's not helped.
MySQL Workbench is just a program to interface with your MySQL instance, so uninstalling it won't delete any of your data or structures. If you want to completely wipe everything in the instance, probably the easiest way is to uninstall, then reinstall mysql-server, like so:
sudo apt-get purge mysql-server && sudo apt-get install mysql-server
If you have packages that depend on it, you can delete all databases through the command-line interface instead. Make sure you have mysql-client installed, then open a terminal and type
mysql -u root -p
You will be prompted for the root password, and then you will be presented with the interactive prompt. Type
show databases;
to get the list of databases in the instance, then type
drop database `<database name>`;
for each database in the list (besides information_schema). Press Ctrl-D, and you're done!

convert_tz returns null

I know this sounds stupid, but when I use
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Asia/Jakarta') AS time
it outputs NULL. I'm using MySQL Workbench in Ubuntu 12.04 64 bit, and it works in my other laptop/os (also using MySQL Workbench).
This will happen if you haven't loaded the time zone table into mysql.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
mysql is the name of the built-in database that holds MySQL-specific configuration data.
I found this thread after spending some time trying to figure out why after running the command in the accepted answer (which is the same on MySQL's dev site) the command was unable to convert between timezones such as
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','MET') AS time
It turns out that on OS X there are two files that cause problems: /usr/share/zoneinfo/Factory and /usr/share/zoneinfo/+VERSION.
The fix... temporarily moving these files to a different location such as /usr/share/zoneinfo/.bak/ allows for the command
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
to fully populate all of the expected timezone information.
This may or may not be a bug in my installed version of MySQL:
$ mysql --version
mysql Ver 14.14 Distrib 5.6.11, for osx10.6 (x86_64) using EditLine wrapper
I am also operating in STRICT_MODE.
In any case, I hope this saves a few headaches for anyone searching for the fix.
Apart from Windows environment, You can set Time Zone by
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
In Windows environment,
1. download Time zone description tables from http://dev.mysql.com/downloads/timezones.html
2. Stop MySQL server
3. Put then inside Mysql installation package (ie. C:\Program Files\MySQL\data\mysql)`
4. Start MySQL server
..Your work is finished..
If still you are getting NULL for CONVERT_TZ
Download these database tables and insert it into mysql database http://www.4shared.com/folder/Toba2qu-/Mysql_timezone.html
Now you problem will be solved.. :)
MAMP PRO
Open Terminal
cd /usr/share/zoneinfo/
sudo mv +VERSION ~/Desktop
cd /applications/MAMP/Library/bin
sudo ./mysql_tzinfo_to_sql /usr/share/zoneinfo | ./mysql -p -u root mysql
sudo mv ~/Desktop/+VERSION /usr/share/zoneinfo/
1) In Windows, there isn't any data folder now in C:\Program Files\MySQL\ as in other answers.
2) In that case, look for C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql. Generally this folder hidden and you will not see C:\ProgramData\ some times.
3) Change the Settings in View tab to see Hidden files and Folders as explained here https://irch.info/index.php?pg=kb.page&id=133
4) Stop the MySQL service by searching for "services" in Windows Start button.
5) Then unzip the timezone_2017c_posix.zip and then copy the files in it (copy the files directly, don't copy the whole folder itself), and paste in
C:\ProgramData\MySQL\MySQLServer5.x\Data\mysql\
6) For MySQL 5.7, timezone_2017c_posix.zip will just give a .sql file after unzipping and it may not solve the issue. So go ahead and download the zip file for 5.6 even if you are running MySQL 5.7 and copy those files to C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql\
7) Restart the MySQL server. To check if the CONVERT_TZ () is working, run this sql query.
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Asia/Jakarta');
and check for non-null output.
These are the steps to make it work if you're in windows and using MySQL 5.7.
Right click on My Computer/Computer/This PC or whatever the name in your OS and choose Properties.
Choose "Advanced system settings" from the left panel.
Choose "Environmental Variables", enter the complete path name of your MySQL bin directory (generally it will be in, C:\Program Files\MySQL\MySQL Server 5.7\bin).
Open cmd prompt, enter into mysql using mysql -u root -p password.
Enter use mysql to select the MySQL DB.
Download the file "timezone_YYYYc_posix_sql.zip" (In the place of YYYY, substitute the maximum year available in that page like 2017 or 2018) from https://dev.mysql.com/downloads/timezones.html.
Extract it and open the file in text editor.
Copy the contents and execute in the cmd prompt.
On successful completion, you should be able to use CONVERT_TZ and other timezone functions.
If you are using MySql on Windows you have to load the timezone data into the mysql schema. Here is a good HOWTO: http://www.geeksengine.com/article/populate-time-zone-data-for-mysql.html
If you don't do this, the function CONVERT_TZ won't recognize your input timezone (i.e. your examples: 'UTC','Asia/Jakarta'), and will simply return NULL.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
if you get the error data too long for column 'abbreviation' at row 1
then see: https://bugs.mysql.com/bug.php?id=68861
the fix would be to run the following
this will add a line to disable the mysql mode and allow mysql to insert truncated data
this was because of a mysql bug where mysql would add a null character at the end (according to the above link)
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
(if the above gives error "data too long for column 'abbreviation' at row 1")
mysql_tzinfo_to_sql /usr/share/zoneinfo > /tmp/zut.sql
echo "SET SESSION SQL_MODE = '';" > /tmp/mysql_tzinfo_to.sql
cat /tmp/zut.sql >> /tmp/mysql_tzinfo_to.sql
mysql --defaults-file=/etc/mysql/my.cnf --user=verifiedscratch -p mysql < /tmp/mysql_tzinfo_to.sql
On Mac OS Catalina when using XAMPP,
Go to /Applications/XAMPP/xamppfiles/bin folder in Terminal then run following.
./mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | ./mysql -u root mysql
This worked for me.

Installing MySQL on a mac

i have downloaded and installed mySQL my double clicking on its icon. It was installed successfully.
When i goto startup and preference i see the icon of mysql added and when i click on it i see a screen where it says 'MySQL server instance is running'.
But when i open terminal and cd to /usr/local/mysql and then when i type sudo ./bin/mysqld_safe i was prompted for a password. and i have not added a password when i installed mySQL, so i tried leaving it blank, and then i tried various passwords to login but all attempts failed.
So now i need to know how to login to mySQL via the terminal ?
mysql version - 5.5.24-osx10.6x86_64
my Mac OS - 10.7.3
What I found installing mysql on MacOs, there are a few differences. One is that it installs it without a password. The other thing is that it by default allows for anonymous logins.
Use this to set the password:
mysqladmin -u root -h localhost password yourpassword
You can remove anonymous logins this way:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> DROP USER ''#'localhost';
mysql> DROP USER ''#'host_name';
The other thing is that I found that the install does not modify the path variable. What I did to run mysql from the command line was to add /usr/local/mysql/bin to path by adding it to /etc/paths or /etc/paths.d . This may be what you need in order to run mysql. Like someone said in the comments, mysqld_safe is one way to start the mysql server, and it seems that is already set to run.
Here are specific instructions to add something to /etc/paths.d
$ cd /etc/paths.d
$ cat > mysql
/usr/local/bin/mysql
(and then type Ctrl-D
that should put a file there)
you may have to sudo if you do not have permissions.
The sudo command, by default, lets anyone in the admin group run a command as root by giving his own password. That's why it asked for your password when you typed "sudo ./bin/mysqld_safe". It has nothing whatsoever to do with mysql.
If you don't have a password, you cannot use sudo in the default configuration. Either give yourself a password, or edit the sudoers file. (I would strongly suggest the former over the latter, especially if you have no idea what sudo does.)
For more information, type "man sudo" (and then "man sudoers") from your Terminal.
Meanwhile, the reason "it says -bash: mysql: command not found when i type mysql in the terminal" is because you've clearly installed it into /usr/local/mysql/bin/mysql, and that isn't on your path. If it were on your path, you could have just done "sudo mysqld_safe" above, instead of "sudo ./bin/mysqld_safe". Since it's not, you have to do "./bin/mysqld_safe".
For more information, consult a good primer on the Unix shell.
Finally, if you've got the mysql daemon running, and are trying to start the client, it's "mysql" that you want to run, not "mysqld_safe".

MySQL retains password of earlier installation even after proper 'yum remove' & 'yum install'

I'm using MySQL-Server over CentOS. I 'yum remove' the MySQL-server and then again 'yum install' it.
When I tried setting up a new password for 'root' using 'mysqladmin'... it raised an error.
Some following up showed it still had earlier-installation's root credential working for it.
~~~~~
Trying some more stuff, I manually set 'old_password=0' in '/etc/my.cnf' and then tried re-installing. It still had the earlier password working for it.
Sounds like the mysql tables are not being removed/replaced when uninstalling/installing. I am not sure where they are located on CentOs (/var/lib/mysql?), but if you remove these manually, it should work.
login to console as root using
su -
root password: _
then move to the directory /var/lib/
using the command
cd /var/lib
now provide this command to delete the existing mysql database :
rm -rf mysql
This command will remove the existing database of mysql which also contains user table where the password for all mysql users are stored including the root.
After removing the database directory and all the associated files with it rum this command to reinstall mysql
yum -y install mysql-server
after installation you will have fresh mysql installed with default settings
and root will have no password.
You can go ahead from here.
This worked for me.