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

来源:http://www.cnblogs.com/kungfupanda/archive/2016/08/05/5741194.html
-Advertisement-
Play Games

http://stackoverflow.com/questions/12426320/how-do-i-set-the-default-schema-for-a-user-in-mysql http://stackoverflow.com/questions/12426320/how-do-i-s ...


 

http://stackoverflow.com/questions/12426320/how-do-i-set-the-default-schema-for-a-user-in-mysql

 

 
up vote16down votefavorite 2

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.

   
shareimprove this question asked Sep 14 '12 at 14:13 Robert Louis Murphy 7551928
 
1  
Are user x and z MySQL users, or users of some other system (e.g. your OS)? Do you only need to specify a default database when using one type of client (e.g. mysql CLI, or PHP Data Objects), or do you need it to be for all clients? Do you need/want to override any default schema specified by the client on connecting? Do you want to disable changing of the default schema after one has been selected? – eggyalSep 14 '12 at 14:40 
    
MySQL does not support schemas. Do you mean "database" instead? – a_horse_with_no_name Sep 14 '12 at 15:22
1  
Oracle MySQL refers to databases as schemas in their tools. So where Microsoft has schemas and databases, MySQL just has schemas, but we call them databases. – Robert Louis Murphy Sep 14 '12 at 15:40
add a comment

2 Answers

activeoldestvotes
up vote27down voteaccepted

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

You can get it using DATABASE() function -

SELECT DATABASE();

And you can set it using USE statement -

USE database1;

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

 

========================

http://stackoverflow.com/questions/1820971/set-current-database-in-mysql-script

 

 

6down votefavorite 1

I have a script file for MySQL that needs to be run on about 30 databases. Here's a snippet:

ALTER TABLE <whatever_database>.`tblusers` ADD COLUMN `availability` ...

ALTER TABLE <whatever_database>.`tblusers` ADD COLUMN `reliability` INTEGER ...

There are many more lines in this script and I'd like to automate it in a loop of current databases to be updated. I've got the list and the loop down using cursors, but here's where I'm running into trouble:

When I'm on a particular database in the cursor, that database name is in a variable. I can't just say ALTER TABLE curschema.tblusers because the script complains that there is no database named curschema (the name of the variable containing the database name that I'd like to run operations on). I've been able to work around this by creating and executing a statement using parameters:

SET @curschema = curschema;
SET @query = NULL;
SET @email = emailAddress;
SET @pass = pass;
SET @statement = CONCAT('SELECT userid INTO @query FROM ', @curschema, '.tbluser 
PREPARE stmt FROM @statement;
EXECUTE stmt;

The problem is, setting up executable strings (like above) will become an extremely tedious task with the dozens of lines of code that I have to run. I was hoping that there was a way that I could set the current database for operations and then just reset that database each loop pass so my generic statements might be run:

(start of my loop)

SET DATABASE database0 -- (through to database29)

-- run statements with database0 .... 29 being implied with the above command

ALTER TABLE `tblusers` ADD COLUMN `availability` TINYINT(1) UNSIGNED ...

ALTER TABLE `tblusers` ADD COLUMN `reliability` INTEGER UNSIGNED NOT ...

(end of my loop)

Does such a command exist to set the current database on which operations may be performed? If no such command exists, is there an easier way to accomplish my task? I figure that at this juncture it's just easier to do a find/replace on all the database names 30 times than it is to rewrite my entire script file to be executable/parameter-ized strings.

Thanks!

 
shareimprove this question edited Feb 14 '13 at 12:55 casperOne 57.4k10125202 asked Nov 30 '09 at 16:49 afilbert 45115
  add a comment

1 Answer

activeoldestvotes
up vote16down voteaccepted

Did you try USE ?

USE db_name

The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued.

Example:

USE db1;
SELECT COUNT(*) FROM mytable;   # selects from db1.mytable
USE db2;
SELECT COUNT(*) FROM mytable;   # selects from db2.mytable

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一資料庫伺服器執行shutdown immediate時,遇到了下麵ORA錯誤,如下所示: $ sqlplus / as sysdba SQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 5 10:56:24 2016 Copyright (c)... ...
  • 問題:使用某大腿寫的遠程工具管理Mysql資料庫時發現所有數據能正常顯示,但是無法進行刪除、修改等操作。 思路:可以遠程讀取到資料庫里的信息,說明當前主機可以遠程連接資料庫。卻無法進行刪除、修改這些操作,說明某些許可權並未賦予當前遠程用戶。 解決方法: 查看當前用戶許可權 顯示當前用戶下的許可權為:sel ...
  • 跟蹤標記:1117 功能: 預設,同一個文件組下的多個文件,如果某個文件沒有可用空間,且設置了自動增長,則該文件自動增長,其他文件大小保持不變; 開啟後,同一文件組下的多個文件,如果某個文件沒有可用空間,且設置了自動增長,文件組下所有文件同時增長,每個文件漲幅取決於自身filegrowth設置; 用 ...
  • 持久化概念 持久化是將程式數據在持久狀態和瞬時狀態間轉換的機制。通俗的講,就是瞬時數據(比如記憶體中的數據,是不能永久保存的)持久化為持久數據(比如持久化至資料庫中,能夠長久保存)。 Redis持久化 官方說明:http://www.redis.io/topics/persistence 持久化方式 ...
  • 從MySQL5.5開始,MySQL以插件的形式支持半同步複製。如何理解半同步呢?首先我們來看看非同步,全同步的概念 非同步複製(Asynchronous replication) MySQL預設的複製即是非同步的,主庫在執行完客戶端提交的事務後會立即將結果返給給客戶端,並不關心從庫是否已經接收並處理,這樣 ...
  • 博主不想寫字並向你仍來了一堆代碼 1-6 SQL——結構化查詢語言,Structured Query Language; 基本按列查詢: 高級一點的過濾查詢,WHERE,ORDER,IN,NOT,AND,OR,BETWEEN,NULL: 通配符,%,_,[](實測MySQL我這裡不支持。。。) 不要 ...
  • 背景: 測試環境1台centOS機器,最近一段頻繁報“ ”, 記錄解決問題的思路,最終找到問題根源:黑客入侵,總結經驗。 思路: 查看max_allowed_packet : show global VARIABLES like '%max_allowed_packet%'; (註意:mysql 系 ...
  • 安裝Redis包 在\Python27\Scripts目錄下執行 Python操作Redis 結果 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...