如何登录mysql并从linux终端查询数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6200215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to log in to mysql and query the database from linux terminal
提问by Param-Ganak
I am using debian linux. I have a linux machine on which mysql is install. I can log in to my linux machine using root user as well as other user. I can connect to mysql database on linux machine from windows machine using sqlyog. Now I want to execute queries on linux machine only using linux terminal
我正在使用 debian linux。我有一台安装了 mysql 的 linux 机器。我可以使用 root 用户和其他用户登录我的 linux 机器。我可以使用sqlyog从windows机器连接到linux机器上的mysql数据库。现在我想只使用 linux 终端在 linux 机器上执行查询
I tried some following things on terminal
我在终端上尝试了以下一些东西
I went to root directory then I went to /var/lib directory
我去了根目录,然后我去了/var/lib目录
I run following commands on terminal
我在终端上运行以下命令
mysqladmin -u root -p
mysqladmin -u root -ppassword
everytime I have get following error message
每次我收到以下错误消息时
ERROR 1045 (28000) Access denied for user 'root'@'localhost' (Using password NO)
错误 1045 (28000) 用户 'root'@'localhost' 访问被拒绝(使用密码 NO)
please guide me for following
请指导我跟随
- How do I get mysql prompt in linux terminal?
- How I stop the mysql server from linux terminal?
- How I start the mysql server from linux terminal?
- How do I get mysql prompt in linux terminal?
- How do I login to mysql server from linux terminal?
- How do I solve following error?
- 如何在 linux 终端中获得 mysql 提示?
- 如何从 linux 终端停止 mysql 服务器?
- 如何从 linux 终端启动 mysql 服务器?
- 如何在 linux 终端中获得 mysql 提示?
- 如何从 linux 终端登录到 mysql 服务器?
- 如何解决以下错误?
ERROR 1045 (28000) Access denied for user 'root'@'localhost' (Using password NO)
错误 1045 (28000) 用户 'root'@'localhost' 访问被拒绝(使用密码 NO)
Please give me solutions for above question. Thank You
请给我以上问题的解决方案。谢谢你
采纳答案by álvaro González
1.- How do I get mysql prompt in linux terminal?
1.- 如何在 linux 终端中获得 mysql 提示?
mysql -u root -p
At the Enter password:
prompt, well, enter root's password :)
在Enter password:
提示符下,输入 root 的密码 :)
You can find further reference by typing mysql --help
or at the online manual.
您可以通过键入mysql --help
或在在线手册中找到更多参考。
2. How I stop the mysql server from linux terminal?
2. 如何从linux终端停止mysql服务器?
It depends. Red Hat based distros have the service
command:
这取决于。基于 Red Hat 的发行版具有以下service
命令:
service mysqld stop
Other distros require to call the init script directly:
其他发行版需要直接调用 init 脚本:
/etc/init.d/mysqld stop
3. How I start the mysql server from linux terminal?
3. 如何从linux终端启动mysql服务器?
Same as #2, but with start
.
与#2 相同,但带有start
.
4. How do I get mysql prompt in linux terminal?
4、如何在linux终端获取mysql提示?
Same as #1.
与#1 相同。
5. How do I login to mysql server from linux terminal?
5、如何从linux终端登录mysql服务器?
Same as #1.
与#1 相同。
6. How do I solve following error?
6.我如何解决以下错误?
Same as #1.
与#1 相同。
回答by quarkonium
At the command prompt try:
在命令提示符下尝试:
mysql -u root -p
give the password when prompted.
出现提示时提供密码。
回答by stefgosselin
I assume you are looking to use mysql client, which is a good thing and much more efficient to use than any phpMyAdmin alternatives.
我假设您正在寻找使用 mysql 客户端,这是一件好事,并且比任何 phpMyAdmin 替代品都更有效。
The proper way to log in with the commandline client is by typing:
使用命令行客户端登录的正确方法是键入:
mysql -u username -p
Notice I did not type the password. Doing so would of made the password visible on screen, that is not good in multi-user environnment!
注意我没有输入密码。这样做会使密码在屏幕上可见,这在多用户环境中是不好的!
After typing this hit enter key, mysql will ask you for your password.
键入此回车键后,mysql 会要求您输入密码。
Once logged in, of course you will need:
登录后,您当然需要:
use databaseName;
to do anything.
做任何事。
Good-luck.
祝你好运。
回答by flindeberg
To your first question:
对于你的第一个问题:
mysql -u root -p
or
或者
mysqladmin -u root -p "your_command"
depending on what you want to do. The password will be asked of you once you hit enter! I'm guessing you really want to use mysqland not mysqladmin.
取决于你想做什么。按回车键后,系统会要求您输入密码!我猜你真的想使用mysql而不是mysqladmin。
For restarting or stopping the MySQL-server on linux, it depends on your installation, but in the common debian derivatives this will work for starting, stopping and restarting the service:
在 Linux 上重启或停止 MySQL 服务器,这取决于你的安装,但在常见的 debian 衍生产品中,这将用于启动、停止和重启服务:
sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql restart
sudo /etc/init.d/mysql status
In some newer distros this might work as well if MySQL is set up as a deamon/service.
在一些较新的发行版中,如果将 MySQL 设置为守护程序/服务,这可能也有效。
sudo service mysql start
sudo service mysql stop
sudo service mysql restart
sudo service mysql status
But the question is really impossible to answer without knowing your particular setup.
但是如果不知道您的特定设置,这个问题真的无法回答。
回答by baraboom
To stop or start mysql on most linux systems the following should work:
要在大多数 linux 系统上停止或启动 mysql,应执行以下操作:
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
The other answers look good for accessing the mysql client from the command line.
其他答案看起来很适合从命令行访问 mysql 客户端。
Good luck!
祝你好运!
回答by cezio
you should use
mysql
command. It's a command line client for mysql RDBMS, and comes with most mysql installations: http://dev.mysql.com/doc/refman/5.1/en/mysql.htmlTo stop or start mysql database (you rarely should need doing that 'by hand'), use proper init script with
stop
orstart
parameter, usually/etc/init.d/mysql stop
. This, however depends on your linux distribution. Some new distributions encourageservice mysql start
style.You're logging in by using
mysql
sql shell.The error comes probably because double '-p' parameter. You can provide
-ppassword
or just-p
and you'll be asked for password interactively. Also note, that some instalations might use mysql (not root) user as an administrative user. Check your sqlyog configuration to obtain working connection parameters.
你应该使用
mysql
命令。它是 mysql RDBMS 的命令行客户端,并随大多数 mysql 安装一起提供:http: //dev.mysql.com/doc/refman/5.1/en/mysql.html要停止或启动 mysql 数据库(您很少需要“手动”执行此操作),请使用带有
stop
或start
参数的正确初始化脚本,通常为/etc/init.d/mysql stop
. 但是,这取决于您的 Linux 发行版。一些新的发行版鼓励service mysql start
风格。您正在使用
mysql
sql shell登录。错误可能是因为双 '-p' 参数。您可以提供
-ppassword
或 just-p
,系统会以交互方式要求您输入密码。另请注意,某些安装可能使用 mysql(不是 root)用户作为管理用户。检查您的 sqlyog 配置以获取工作连接参数。
回答by user789877
I had the same exact issue on my ArchLinux VPS today.
我今天在我的 ArchLinux VPS 上遇到了同样的问题。
mysql -u root -p
just didn't work, whereas mysql -u root -pmypassword
did.
mysql -u root -p
只是没有用,而mysql -u root -pmypassword
确实有效。
It turned out I had a broken /dev/tty device file (most likely after a udev upgrade), so mysql couldn't use it for an interactive login.
原来我有一个损坏的 /dev/tty 设备文件(很可能是在 udev 升级之后),所以 mysql 无法使用它进行交互式登录。
I ended up removing /dev/tty and recreating it with mknod /dev/tty c 5 1
and chmod 666 /dev/tty
. That solved the mysql problem and some other issues too.
我最终删除的/ dev / tty的,并重新创建mknod /dev/tty c 5 1
和chmod 666 /dev/tty
。这也解决了 mysql 问题和其他一些问题。
回答by user2807050
if u got still no access to db, 1. in ur error message is set no password right? then first do mysqlpasswd 'username' after that enter and then give it a password type again as requested and then try to access again with mysql -p if you are root
如果您仍然无法访问数据库, 1. 在您的错误消息中没有设置密码,对吗?然后首先在输入后执行 mysqlpasswd 'username' ,然后根据要求再次给它一个密码类型,然后尝试使用 mysql -p 再次访问,如果你是 root
回答by maioman
if you're already logged in as root just
如果您已经以 root 身份登录
mysql -u root
prompting the password will otherwise return as error
提示输入密码,否则将作为错误返回
回答by Udi
Try "sudo mysql -u root -p" please.
请尝试“sudo mysql -u root -p”。