centos 7 에 테스트 mariadb 를 설치 하고 SQLAlchemy 를 처음 사용 합 니 다.

4700 단어 mysqlDBcentosmaria
mariadb 설치:yum-y install mariadb 테스트:시작 서비스 systemctl start mariadb.service systemctl enable mariadb.service 기본 암 호 는 빈 my sql-u root-p 입 니 다.
SQLAlchemy 참고 문서:http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html ORM(Object Relational Map)의 기본 개념.데이터베이스 에 있 는 표 의 한 줄 에 대응 하 는 Object 입 니 다.SQLAlchemy 는 Object 와 DB 표 줄 간 의 동기 화 작업 을 도와 줍 니 다.공식 문서 작업:1 버 전 검사 2 링크 import sqlalchemy from sqlalchemy import createengine engine=create_engine(“mysql://root:@localhost/test”,encoding=”utf-8”,echo=True)
링크 가 연결 문자열 의 오 류 를 의심 하지 않 는 것 을 발 견 했 습 니 다.사실은 없습니다.바로 my sql 시작 입 니 다.python 라 이브 러 리 에 MySQLdb 라 이브 러 리 가 없 기 때 문 입 니 다.윈도 우즈 에서 mysql 을 연결 할 때.exe 의 MySQL db 라 이브 러 리 를 설치 해 야 한 다 는 생각 이 들 었 습 니 다.그 렇 군요.linux 에 설치 되 어 있 지 않 아서 연결 이 되 지 않 았 습 니 다.python mysql 드라이버(API)홈 페이지 연결https://pypi.python.org/pypi/MySQL-python/1.2.5 python 이 my sql 을 조작 할 수 있 도록 하려 면 MySQL-python 드라이브 가 필요 합 니 다.python 이 my sql 을 조작 하 는 데 없어 서 는 안 될 모듈 입 니 다.
1 python 이 mysql 을 조작 할 수 있 도록 하려 면 MySQL-python 드라이브 가 필요 합 니 다.이것 은 python 이 mysql 을 조작 하 는 데 없어 서 는 안 될 모듈 입 니 다.
yum install gcc python-devel mysql-devel -y

2 다운로드 및 압축 풀기
wget "https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c"
unzip MySQL-python-1.2.5.zip

3 압축 해제 후 파일 디 렉 터 리 에 들 어가 설치
cd MySQL-python-1.2.5
python setup.py install

방화벽 닫 는 거 기억 해.
systemctl start firewalld.service#  firewall
systemctl stop firewalld.service#  firewall
systemctl disable firewalld.service#  firewall    

다른 Host 에서 mariadb 연결
SQL Error(1130):Host'192.168.XX.XX'is not allowed to connect to this MySQL server 는 연 결 된 사용자 계 정 에 원 격 연결 권한 이 없고 이 컴퓨터(localhost)에 만 로그 인 할 수 있 음 을 설명 합 니 다.my sql 데이터베이스 에 있 는 user 표 의 host 항목 을 변경 하여 localhost 를%로 변경 하려 면 먼저 아래 단계 로 Mysql 서버 에 로그 인 하여 my sql 서버 에 로그 인 하려 면 dos 아래 my sql 의 bin 디 렉 터 리 로 전환 하여 다음 과 같은 작업 을 해 야 합 니 다.my sql>use my sql;mysql>update user set host = ‘%’ where user =’root’; MariaDB [mysql]> update user set host = ‘%’ where host = ‘localhost’; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql>flush privileges; mysql> select host, user from user; mysql>quit OK。원 격 연결 성공!
표 만 들 기
#!usr/bing/python
# coding=utf-8
#   sqlalchemy  mariadb    
__author__ = 'ning'
from sqlalchemy import create_engine
from sqlalchemy import String, Integer, Column
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine("mysql://root:[email protected]/test", encoding="utf-8", echo=True)

Base = declarative_base()


class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    password = Column(String(50))

    def __repr__(self):
        return "<User(id=%s,name=%s,fullname=%s,password=%s)>" %\
               (self.id, self.name, self.fullname, self.password)

Base.metadata.create_all(engine)

좋은 웹페이지 즐겨찾기