1 关闭防火墙和selinux
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭selinux,vim /etc/selinux/config
SELINUX=disabled
2 编译安装python3
Linux系统一般都会有自带的Python,但是是2.7版本的,python2和python3是不兼容的,要安装python3不需要将python2卸载。自带的 python 一般安装在 /usr/bin/ 目录下,我们将python3安装在/usr/python3下
(1) 解压安装包
xz -d Python-3.6.5.tar.xz
tar -xvf Python-3.6.5.tar
(2) 编译安装
- 编译之前需要先安装依赖包(必须)
yum -y install zlib-devel
yum install openssl-devel -y
在解压后的python目录中执行编译安装
./configure --prefix=/usr/python3
make
make install
- 安装成功后,安装目录在/usr/python3
(3) 创建软链接
ln -s /usr/python3/bin/python3 /usr/bin/python3
(4) 配置pip
在python3的安装目录下/usr/python3/bin执行
cp pip3 /usr/bin/
cp pip3.6 /usr/bin/
3 安装MariaDB
(1) 安装
yum -y install mariadb mariadb-server
(2) 启动
systemctl start mariadb
(3) 设置开机启动(选)
systemctl enable mariadb
(4) 配置
mysql_secure_installation
以下步骤请注意:
Enter current password for root (enter for none): ----- 初次运行直接回车
Set root password? [Y/n]----- 是否设置root用户密码,输入y并回车或直接回车
New password: ----- 设置root用户的密码
Re-enter new password: ----- 再输入一次你设置的密码
Remove anonymous users? [Y/n] ----- 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] ----- 是否禁止root远程登录,回车
Remove test database and access to it? [Y/n] ----- 是否删除test数据库,回车
Reload privilege tables now? [Y/n] ----- 是否重新加载权限表,回车
(5) 登录
mysql -u root -p
(6) 配置字符集
- 编辑文件/etc/my.cnf,在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
- 编辑文件/etc/my.cnf.d/client.cnf,在[client]中添加
default-character-set=utf8
- 编辑文件/etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加
default-character-set=utf8
- 重启数据库
systemctl restart mariadb
- 进入数据库查看字符集
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
(7) 创建用户
创建用户并授权
MariaDB [(none)]> create user phq@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user from mysql.user;
+------+
| user |
+------+
| root |
| root |
| phq |
| root |
+------+
MariaDB [(none)]> grant all privileges on *.* to phq@localhost;
(8) 允许远程登录
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
4 python脚本查询MariaDB
(1) 安装connector包
windows环境在cmd执行
pip install pymysql
linux环境执行:
pip3 install pymysql
(2) 创建测试表
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255),
PRIMARY KEY (`id`)
)
(3) python查询MariaDB
#!/usr/bin/python3
import pymysql
#打开数据库连接
db = pymysql.connect(host = "192.168.59.131", user = "phq", password = "123456", db = "test",port = 3306)
cursor = db.cursor()
sql = "select * from test"
try:
#执行sql,获取所有查询结果
cursor.execute(sql)
results = cursor.fetchall()
#遍历查询结果
for row in results:
id = row[0]
name = row[1]
print(id,name)
except Exception as e:
raise e
finally:
db.close()