The most detailed # # # # zero-based white Pycharm call also through a remote server mysql database


This article describes the call Ali cloud server in python MySQL database

1. The server configuration

1.1 Installation MySQL database

1. Download the MySQL installation package

rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

2. Install MySQL

yum install -y mysql-server

3. Set the MySQL boot

systemctl enable mysqld.service

4. Check has been installed at startup

systemctl list-unit-files | grep mysqld

If the following instructions to automatically start the installation has been completed

mysqld.service enabled

5. Set the open service

systemctl start mysqld.service

6. Review the default password MySql

grep 'temporary password' /var/log/mysqld.log  

Which, root @ localhost: behind the content of the default password for MySQL
Here Insert Picture Description
7. landed MySql, enter a user name and password

mysql -u root -p       //密码也就是第六步里面查看到的默认密码

8. Note the modification of the current user password given below

mysql>SET PASSWORD = PASSWORD('新密码');  //但是这样会报错的,具体错误看下面
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements  //报错

The reason: mysql For safety, have their own policy requirements, if we want to set for our common root or 123456 Such a password, you need to modify the policy requirements, using the following command:

mysql>set global validate_password_policy=LOW;

Execution of the code to change the password again, there are suggested to change the password represent success!

mysql>SET PASSWORD = PASSWORD('新密码');
Query OK,0 row addected, 1 warning(0.00 sec)

9. Turn remote login, authorized MySQL root user remote login (non-root user Linux system)

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MySQL的root用户密码' WITH GRANT OPTION; 

10 so that the command to take effect immediately

mysql>flush privileges;

This step must be done, or can not succeed! Phrase represents the number of reloads permission from the grant table mysql database, MySQL because the rights are placed in the cache, so the need to reload after the finish to change.

Ali cloud MySQL 1.2 open port authority

  1. Log Ali cloud website, in order to: Control Panel -> cloud server ECS-> Network and Security -> Security Group
  2. In the security group ID / name Click the security group ID corresponding to the next
  3. In the jump page, click to add the security group rules
  4. In the following pages, the protocol type selected MySQL (3306) , authorization object fill 0.0.0.0/0 , then click OK
    Here Insert Picture Description

2. Access a remote server in Pycharm in MySQL database

In pycharm the new file and writes the following code .py

import pymysql

# 打开数据库连接
db = pymysql.connect("服务器IP", "root", "root密码", "mysql", port=3306, charset='utf8')
# 端口号3306,utf-8编码,否则中文有可能会出现乱码。
sql = '''SELECT * from user'''
try:
    cursor = db.cursor()
    cursor.execute(sql)
    text=cursor.fetchall()
    print(text[0])
except Exception as e:
    db.rollback()  # 如果出错就回滚并且抛出错误收集错误信息。
    print("Error!:{0}".format(e))
finally:
    db.close()
# 关闭数据库连接

If the following code appears to indicate that the remote access server MySQL database success!

('%', 'root', 'Y', 'Y', 'Y'.......(省略)
Published 59 original articles · won praise 2 · Views 4633

Guess you like

Origin blog.csdn.net/lch551218/article/details/103996193