測試用,正式機不要裝
收集到很多監控數據,想要做分析,試著用python測試看看
python新手用Anaconda最方便
安裝Anaconda,其中一個步驟選yes會建立路徑,否則要自己手動建
wget https://repo.continuum.io/archive/Anaconda2-5.0.1-Linux-x86_64.sh
yum install bzip2
bash Anaconda2-5.0.1-Linux-x86_64.sh
手動建,改成自己anaconda2的路徑
vi ~/.bashrc
export PATH="/root/anaconda2/bin:$PATH"
重整source,輸入python,顯示Python 2.7.14 |Anaconda即成功了
source ~/.bashrc
設定ipython,首先設定密碼
ipython
from notebook.auth import passwd
passwd()
output一組密碼記下來
Out[2]: 'sha1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
設定config
jupyter notebook --generate-config --allow-root
依照上個命令產生的config路徑(Writing default config to: /root/.jupyter/jupyter_notebook_config.py)
vi /root/.jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_root = True
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.password = u'sha1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
啟動,預設 http://0.0.0.0:8888
jupyter notebook
1-12有草草帶過撈Zabbix SQL資料,下面詳細帶過步驟
參考教學,要用mysql-connector-python套件,先來安裝
https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html
一般沒裝Anaconda用pip安裝
yum install python-pip
sudo pip install mysql-connector-python
查相關套件
# pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (8.0.5) - MySQL driver written in Python
有裝Anaconda用conda安裝
conda install -c mysql-connector-python
回到jupyte notebook網頁,右上new一個python2
import測試套件是否安裝成功
import mysql.connector
依照教學,連上zabbix sql
https://docs.microsoft.com/zh-tw/azure/mysql/connect-python
import mysql.connector
from mysql.connector import errorcode
# Obtain connection string information from the portal
config = {
'host':'127.0.0.1',
'user':'zabbix',
'password':'zabbix',
'database':'zabbix'
}
# Construct connection string
try:
conn = mysql.connector.connect(**config)
print("Connection established")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with the user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = conn.cursor()