V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ggvm
V2EX  ›  分享发现

yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务

  •  
  •   ggvm · 2022-10-08 18:57:37 +08:00 · 1533 次点击
    这是一个创建于 558 天前的主题,其中的信息可能已经有所发展或是发生改变。

    yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务。

    免费申请到的资源如下:

    申请链接: https://cloud.yugabyte.com/

    步骤:

    1 输入企业邮箱地址和密码注册

    2 选择免费套餐:

    可以看到免费可以得到的资源 2CPU/4G RAM/10G storage 。500 张表,12.5 百万行数据

    3 选择区域

    可以选择 AWS 或者 GCP ,根据经验,AWS 日本到中国线路比较优秀,有 AWS 日本当然可以优先选择。

    4 选择好区域后,创建中

    <figure class="wp-block-image size-full"></figure>

    5 创建成功

    大约等待 6-10 分钟,数据库就创建好了。创建成功后,系统页面引导我们使用数据库。

    <figure class="wp-block-image size-full"></figure>

    6 添加允许访问的 IP 和 IP 段。

    如果希望所有 ip 都能访问,填写 0.0.0.0/0

    <figure class="wp-block-image size-full"></figure>

    7 获取链接方式

    连接需要下载一个 CA 文件,例如为 root.crt

    <figure class="wp-block-image size-full"></figure>

    8 测试使用

    首先需要安装 psycopg2

    pip3 install psycopg2-binary

    或者参考你的 python 文档

    import psycopg2
    import psycopg2.extras
    
    config = {
        'host': 'ap-northeast-1.xxxxxxx.aws.ybdb.io',
        'port': '5433',
        'dbName': 'yugabyte',
        'dbUser': 'admin',
        'dbPassword': 'Ckw9F-xxxxxxx',
        'sslMode': 'verify-full',
        'sslRootCert': './root.crt'
    }
    
    
    def main(conf):
        print(">>>> Connecting to YugabyteDB!")
    
        try:
            if conf['sslMode'] != '':
                yb = psycopg2.connect(host=conf['host'], port=conf['port'], database=conf['dbName'],
                                      user=conf['dbUser'], password=conf['dbPassword'],
                                      sslmode=conf['sslMode'], sslrootcert=conf['sslRootCert'],
                                      connect_timeout=10)
            else:
                yb = psycopg2.connect(host=conf['host'], port=conf['port'], database=conf['dbName'],
                                      user=conf['dbUser'], password=conf['dbPassword'],
                                      connect_timeout=10)
        except Exception as e:
            print("Exception while connecting to YugabyteDB")
            print(e)
            exit(1)
    
        print(">>>> Successfully connected to YugabyteDB!")
    
        #create_database(yb)
        #select_accounts(yb)
        #transfer_money_between_accounts(yb, 800)
        select_accounts(yb)
        yb.close()
    
    
    def create_database(yb):
        try:
            with yb.cursor() as yb_cursor:
                yb_cursor.execute('DROP TABLE IF EXISTS DemoAccount')
    
                create_table_stmt = """
                    CREATE TABLE DemoAccount (
                        id int PRIMARY KEY,
                        name varchar,
                        age int,
                        country varchar,
                        balance int
                    )"""
                yb_cursor.execute(create_table_stmt)
    
                insert_stmt = """
                    INSERT INTO DemoAccount VALUES
                            (1, 'Jessica', 28, 'USA', 10000),
                            (2, 'John', 28, 'Canada', 9000)"""
                yb_cursor.execute(insert_stmt)
            yb.commit()
        except Exception as e:
            print("Exception while creating tables")
            print(e)
            exit(1)
    
        print(">>>> Successfully created table DemoAccount.")
    
    
    def select_accounts(yb):
        print(">>>> Selecting accounts:")
    
        with yb.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as yb_cursor:
            yb_cursor.execute("SELECT name, age, country, balance FROM DemoAccount")
    
            results = yb_cursor.fetchall()
            for row in results:
                print("name = {name}, age = {age}, country = {country}, balance = {balance}".format(**row))
    
    
    def transfer_money_between_accounts(yb, amount):
        try:
            with yb.cursor() as yb_cursor:
                yb_cursor.execute("UPDATE DemoAccount SET balance = balance - %s WHERE name = 'Jessica'", [amount])
                yb_cursor.execute("UPDATE DemoAccount SET balance = balance + %s WHERE name = 'John'", [amount])
            yb.commit()
        except (Exception, psycopg2.DatabaseError) as e:
            print("Exception while transferring money")
            print(e)
            if e.pgcode == 40001:
                print("The operation is aborted due to a concurrent transaction that is modifying the same set of rows." +
                      "Consider adding retry logic or using the pessimistic locking.")
            exit(1)
    
        print(">>>> Transferred {} between accounts.".format(amount))
    
    
    if __name__ == "__main__":
        main(config)
    

    我使用的是绿云 VPS提供的大阪的服务器,测试起来相当流畅。

    看到访问的速度还很不错。

    总结:

    yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务。

    虽然存储的总行数不能超过 1.25 千万,但对于一般的使用已经足够。

    yugabyteDB 提供了 AWS 和 GCP 的众多数据中心,满足了很多 VPS 群友缺少存储服务器的需求。

    更多免费数据库和 vps 资源有兴趣可了解 https://zhuji188.com/780.html

    5 条回复    2022-10-09 12:32:34 +08:00
    mayli
        1
    mayli  
       2022-10-09 00:27:24 +08:00 via Android
    会跑路吗?
    ggvm
        2
    ggvm  
    OP
       2022-10-09 08:49:15 +08:00
    @mayli 暂时看不到这个迹象
    luojianxhlxt
        3
    luojianxhlxt  
       2022-10-09 09:32:19 +08:00
    企业邮箱地址,这个怎么判断呀
    ggvm
        4
    ggvm  
    OP
       2022-10-09 09:55:03 +08:00
    @luojianxhlxt 自己搞个域名就是
    Wincer
        5
    Wincer  
       2022-10-09 12:32:34 +08:00
    aws 的 region 后续可以免费更换成其它的吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5290 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:26 · PVG 09:26 · LAX 18:26 · JFK 21:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.