如何在 MySQL 中为用户设置默认架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12426320/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:51:03  来源:igfitidea点击:

How do I set the default schema for a user in MySQL

mysqldefaultdatabase-schema

提问by Robert Louis Murphy

Is there a way to set a default schema for each user in MySQL and if so how? Where user x would default to schema y and user z would default to schema a.

有没有办法为 MySQL 中的每个用户设置默认架构,如果可以,如何设置?其中用户 x 将默认为架构 y,用户 z 将默认为架构 a。

回答by Devart

There is no default database for user. There is default database for current session.

用户没有默认数据库。当前会话有默认数据库。

You can get it using DATABASE() function -

您可以使用 DATABASE() 函数获取它 -

SELECT DATABASE();

And you can set it using USE statement -

您可以使用 USE 语句设置它 -

USE database1;

You should set it manually - USE db_name, or in the connection string.

您应该手动设置 - USE db_name,或在连接字符串中设置。

回答by Gary

If your user has a local folder e.g. Linux, in your users home folder you could create a .my.cnf file and provide the credentials to access the server there. for example:-

如果您的用户有一个本地文件夹,例如 Linux,则在您的用户主文件夹中,您可以创建一个 .my.cnf 文件并提供访问该服务器的凭据。例如:-

[client]
host=localhost
user=yourusername
password=yourpassword or exclude to force entry
database=mygotodb

Mysql would then open this file for each user account read the credentials and open the selected database.

然后,Mysql 会为每个用户帐户打开此文件,读取凭据并打开选定的数据库。

Not sure on Windows, I upgraded from Windows because I needed the whole house not just the windows (aka Linux) a while back.

不确定在 Windows 上,我从 Windows 升级,因为我需要整个房子,而不仅仅是 Windows(又名 Linux)。