[python] arch linux install mysql and use with python



1. 概述

Relational database like MariaDB is one of the required components to setup a web server as well as other less common uses such as when configuring a shared Kodi database. On Arch Linux MySQL has been replaced by a functionally identical community fork called MariaDB. The installation is also practically identical and as simple as can be expected.


2. 安装 MySQL / MariaDB

Install with pacman.

sudo pacman -S mariadb

Initialize data directories.

> sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql


Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo

See the MariaDB Knowledgebase at https://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at https://mariadb.org/jira

The latest information about MariaDB is available at https://mariadb.org/.
You can find additional information about the MySQL part at:
https://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

By default MySQL will run as the user running the command unless a user is explicitly

specified with --user option. Path to the installation directory can be specified with --basedir option while data directory is specified with --datadir.


3. 运行 MySQL / MariaDB

Start the service with systemd & Enable the service to start on boot.

sudo systemctl start mysqld     #启动
sudo systemctl enable mysqld    #开机启动

Then reboot!


4. 配置 MySQL / MariaDB

Secure the installation.

sudo mysql_secure_installation

Make sure you set root password, remove anonymous users and disallow root login remotely unless you know you will need remote access to MariaDB. There should also be no need to keep test database so remove test database and access to it. Finish the install process with reload privilege tables now.


5. 使用 MySQL / MariaDB

Invoke the command line tool.

The username is specified with -u option follower by the username which is root by default. The password is specified with the -p option followed by the password without a space in between or the password can be omitted in which case MariaDB will prompt for one.

  • List all existing databases.

    SHOW DATABASES;
    
  • List all database users.

    SELECT DISTINCT User FROM mysql.user;
    

Use command line or Install phpMyAdmin to administer MySQL / MariaDB databases.


通过下面命令检查之后,如果看到有mysql 的socket处于 listen 状态则表示安装成功:

sudo netstat -tap | grep mysql

登陆mysql数据库可以通过如下命令:

mysql -u root -p

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,上面命令输入之后会提示输入密码,此时输入密码就可以登录到mysql。

下面是一些命令行中操作的DEMO,可做今后参考:

mysqladmin -u root -p create blog
mysql  mysql -u root -p
show databases;
use blog;

CREATE TABLE IF NOT EXISTS `blog_table`(
   `blogId` BIGINT UNSIGNED,
   `url` VARCHAR(100) NOT NULL,
   `title` VARCHAR(1000) NOT NULL,
   `support` INT UNSIGNED,
   `pageView` INT UNSIGNED,
   PRIMARY KEY ( `blogId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `tag_table`(
   `tagId` INT UNSIGNED AUTO_INCREMENT,
   `tagName` VARCHAR(100) NOT NULL,
   PRIMARY KEY ( `tagId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `blog_tag_relation_table`(
   `relationId` INT UNSIGNED AUTO_INCREMENT,
   `blogId` BIGINT UNSIGNED,
   `tagId` INT UNSIGNED,
   PRIMARY KEY ( `relationId` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

show tables;
desc blog_table;
desc tag_table;
desc blog_tag_relation_table;

//change blogId int 2 bigint
alter table blog_table change blogId blogId BIGINT UNSIGNED;
//show data
select * from blog_table;
//delete data
delete from blog_table where blogId=201801021423;

INSERT INTO blog_table(blogId,url,title,support,pageView) 
VALUES(201801021423,'http://106.14.226.191:3000/blog/201607281658.html','[商业_法务] 1、公司一款新消费类电子产品如何快速全面的专利保护',0,0);

//too short
alter table blog_table change title title VARCHAR(1000) NOT NULL;


INSERT INTO tag_table(tagId,tagName) 
VALUES(0,'硬件_模拟电路');

select * from blog_table;
select * from tag_table;
select * from blog_tag_relation_table;

delete from blog_table where blogId>0;
delete from tag_table where tagId>=0;
delete from blog_tag_relation_table where relationId >= 0;

select a.title , a.url, b.tagName from blog_table a, tag_table b, blog_tag_relation_table c WHERE a.blogId = c.blogId AND a.blogId = 201602021408 AND b.tagId = c.tagId;

select a.title , a.url, b.tagName from blog_table a, tag_table b, blog_tag_relation_table c WHERE a.blogId = c.blogId AND b.tagId = c.tagId ORDER BY b.tagId;

为了python操作mysql需要执行下面命令:

pip install MySQL-python (arch linux 2.7 版本 python 会报错,建议用下面的命令)
sudo pacman -S mysql-python

链接



: ** 在 ubuntu 上比较好操作,在 arch linux 上有些难操作,这里记录下~ **

posted @ 2021-09-30 19:27  beautifulzzzz  阅读(197)  评论(0编辑  收藏  举报