linux安装MySQL并用Python连接数据库以及远程访问数据库

1,安装MySQL

sudo apt-get install mysql-server
#提示输入root的密码


sudo apt install mysql-client


sudo apt install libmysqlclient-dev


输入如下命令进行检验是否安装mysql成功。
sudo netstat -tap | grep mysql

2,进入MySQL查看数据

mysql -uroot -p123456  --root 是用户,123456 是密码, u 和p 是固定的格式

/* 显示 数据库 :数据库就是一堆表的集合*/

show databases
        +--------------------+
        | Database           |
        +--------------------+
        | information_schema |
        | mysql              |
        | performance_schema |
        | sys                |
        +--------------------+
        4 rows in set (0.00 sec)
-- 进入mysql数据库
use mysql;
--------------------
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    ----------------------


show tables  显示这个数据库有哪些表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |


-- 查看表的结构

mysql> desc user;  --每个字段严格指定 类型 大小

+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(32)                          | NO   | PRI |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |



-- 查看数据内容
mysql> select * from user;
mysql> select * from user\G;

 -- 退出
mysql> quit
------
Bye
-----


-- 赋予远程连接的权限(好像不好用),本机使用好用


mysql> grant all on *.* to 'root'@'%' identified by "123456"
flush privileges;
exit;
service mysql restart

-- Python 远程连接数据库


-- Python 远程连接数据库
---------------------
      import pymysql

      # 创建连接
      conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='mysql')
        # 下面也可以
      conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='mysql')#mysql是数据库

      # 创建游标
      cursor = conn.cursor()

      effect_row = cursor.execute("select * from user")
      print(cursor.fetchone())
      conn.commit()

      # 关闭游标
      cursor.close()
      # 关闭连接
      conn.close()
---------------------------


查看进程


zjw@zjw-System-Product-Name:~$ ps -ef
zjw@zjw-System-Product-Name:~$ ps -ef | grep mysql

 

 

 

 

 

 

你可能感兴趣的:(linux安装MySQL并用Python连接数据库以及远程访问数据库)