In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB module / usr/lib/python2.6/site-packages/salt/modules/mongodb.py
The MongoDB module will first check whether the PyMongo module is installed.
#-*-coding: utf-8-*-'Module to provide MongoDB functionality to Salt:configuration: This module uses PyMongo, and accepts configuration details as parameters as well as configuration settings:: mongodb.host:' localhost' mongodb.port: 27017 mongodb.user:''mongodb.password:' 'This data can also be passed into pillar. Options passed into opts will overwrite options passed into pillar.'''# Import python libsimport logging# Import salt libsfrom salt._compat import string_types# Import third party libstry: import pymongo HAS_MONGODB = Trueexcept ImportError: HAS_MONGODB = Falselog = logging.getLogger (_ _ name__)
Introduce logging module to deal with logs, and then introduce salt._compat.string_types
Handle the exception for checking pymongo. If the import of pymongo is successful, set the HAS_MONGODB variable to True, and the import failure to False.
Def _ _ virtual__ ():''Only load this module if pymongo is installed' if HAS_MONGODB: return 'mongodb' else: return False
If you define a _ _ virtual__ function, you can control whether the module is allowed to be visible. If False,SaltStack is returned, the module will be ignored, and if a string is returned, SaltStack will use that string.
Def _ connect (user=None, password=None, host=None, port=None, database='admin'):''Returns a tuple of (user, host, port) with config, pillar, or default values assigned to missing values. '' If not user: user = _ salt__ ['config.option'] (' mongodb.user') if not password: password = _ salt__ ['config.option'] (' mongodb.password') if not host: host = _ salt__ ['config.option'] (' mongodb.host') if not port: port = _ salt__ ['config.option'] (' mongodb.port ') try: conn = pymongo.connection.Connection (host=host Port=port) mdb = pymongo.database.Database (conn, database) if user and password: mdb.authenticate (user, password) except pymongo.errors.PyMongoError: log.error ('Error connecting to database {0}' .format (database)) return False return conn
Pymongo.connection.Connection
This method PyMongo has been deprecated and replaced with the new MongoClient.
Def db_list (user=None, password=None, host=None, port=None):''List all Mongodb databases CLI Example:. Code-block:: bash salt'* 'mongodb.db_list' 'conn = _ connect (user, password, host, port) if not conn: return' Failed to connect to mongo database' try: log.info ('Listing databases') return conn.database_names () except pymongo.errors.PyMongoError as err: log.error (err) return str (err)
List all MongoDB databases
$sudo salt'* 'mongodb.db_list' 10.10.41.17 28018localhost.localdomain:' mongodb.db_list' is not available.gintama-qa-server:-pay-clan-test-game1-admin-gam_server_1
SaltStack will first check to see if the PyMongo module is included, and if it does, it will not be executed.
Def db_exists (name, user=None, password=None, host=None, port=None):''Checks if a database exists in Mongodb CLI Example:. Code-block:: bash salt'* 'mongodb.db_exists' 'dbs = db_list (user, password, host, port) if isinstance (dbs, string_types): return False return name in dbs
Check whether a library exists
$sudo salt 'gintama-qa-server' mongodb.db_exists pay' '10.10.41.17 28018gintama-qa-server: True$ sudo salt' gintama-qa-server' mongodb.db_exists pay3 '10.10.41.17 28018gintama-qa-server: False
Def db_remove (name, user=None, password=None, host=None, port=None):''Remove a Mongodb database CLI Example:. Code-block:: bash salt'* 'mongodb.db_remove' 'conn = _ connect (user, password, host Port) if not conn: return 'Failed to connect to mongo database' try: log.info (' Removing database {0} '.format (name)) conn.drop_database (name) except pymongo.errors.PyMongoError as err: log.error (' Removing database {0} failed with error: {1} '.format (name) Str (err) return str (err) return True
Delete a library
Def user_list (user=None, password=None, host=None, port=None, database='admin'):''List users of a Mongodb database CLI Example:. Code-block:: bash salt'* 'mongodb.user_list' 'conn = _ connect (user, password, host, port) if not conn: return' Failed to connect to mongo database' try: log.info ('Listing users') mdb = pymongo.database.Database (conn) Database) output = [] for user in mdb.system.users.find (): output.append ([('user', user [' user'])), ('readOnly', user.get (' readOnly') ]) return output except pymongo.errors.PyMongoError as err: log.error ('Listing users failed with error: {0}' .format (str (err) return str (err))
List the account number
Def user_exists (name, user=None, password=None, host=None, port=None, database='admin'):''Checks if a user exists in Mongodb CLI Example:. Code-block:: bash salt'* 'mongodb.user_exists' 'users = user_list (user, password, host, port, database) for user in users: if name = = dict (user) .get (' user'): return True return False
Check whether the account exists
Def user_create (name, passwd, user=None, password=None, host=None, port=None, database='admin'):''Create a Mongodb user CLI Example:. Code-block:: bash salt'* 'mongodb.user_create' 'conn = _ connect (user, password, host, port) if not conn: return' Failed to connect to mongo database' try: log.info ('Creating user {0}' .format (name)) mdb = pymongo.database.Database (conn, database) mdb.add_user (name Passwd) except pymongo.errors.PyMongoError as err: log.error ('Creating database {0} failed with error: {1}' .format (name, str (err) return str (err) return True
Create an account
Def user_remove (name, user=None, password=None, host=None, port=None, database='admin'):''Remove a Mongodb user CLI Example:. Code-block:: bash salt'* 'mongodb.user_remove' 'conn = _ connect (user, password, host, port) if not conn: return' Failed to connect to mongo database' try: log.info ('Removing user {0}' .format (name)) mdb = pymongo.database.Database (conn) Database) mdb.remove_user (name) except pymongo.errors.PyMongoError as err: log.error ('Creating database {0} failed with error: {1}' .format (name, str (err) return str (err) return True
Test situation
$sudo salt 'gintama-qa-server' mongodb.user_create gintama gintama123' '10.10.41.17 28018 taiwan_game1gintama-qa-server: True$ sudo salt' gintama-qa-server' mongodb.user_exists gintama '10.10.41.17 28018 taiwan_game1gintama-qa-server: True$ sudo salt' gintama-qa-server' mongodb.user_list '10.10.41.17 28018 taiwan_game1gintama-qa-server: | _ | _-user-gintama | _-readOnly-None$ sudo salt 'gintama-qa-server' mongodb.user_remove gintama' 10.10.41.17 28018 taiwan_game1gintama-qa-server: True$ sudo salt 'gintama-qa-server' mongodb.user_list' 10.10.41.17 28018 taiwan_game1gintama-qa-server:$ sudo salt 'gintama-qa-server' Mongodb.user_exists gintama 10.10.41.17 28018 taiwan_game1gintama-qa-server: False
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.