linux安装mysql数据库

安装MySQL数据库

yum -y install mysql-server

启动MySQL数据库

service mysqld start
systemctl restart mysqld.service
systemctl start mysqld.service
systemctl stop mysqld.service

在这里插入图片描述

登录MySQL数据库

mysql -u root -p
注:初始密码为空

查看表

show tables

修改密码

# 这种形式修改密码后不能登陆(原因未知)
update user set authentication_string=password("123456") where user="root"; 
# 这种方式修改密码可以登陆
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';

开启防火墙

#查看已放行的端口
firewall-cmd --list-all
firewall-cmd --zone=public --add-port=3306/tcp --permanent

重启防火墙

firewall-cmd --reload
修改host
update user set host='%' where user='root';
python 连接mysql
# 导入pymysql
import pymysql   

config = {
    'host': 'localhost',  # 本地地址
    'port': 3306,         # 端口
    'user': 'root',       # 数据库用户名
    'password': 'root',   # 数据库密码是 
    'db': 'test_pymysql'  # 数据库的名字
}

conn = pymysql.connect(**config) # 连接数据库

下载解压mysql的压缩包,文件夹改名mysql

tar -zxvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz #解压安装包
mv mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz mysql #修改文件夹名称
创建用户和用户组

groupadd mysql #创建组
useradd -g mysql mysql #创建用户
创建data文件存放数据,并授权全部文件

mkdir /mysql解压文件夹/data #创建data文件夹,用于存放数据库表
chown -r -g mysql:mysql /mysql解压文件夹 #修改权限
初始化数据库,并记录生成的随机密码

bin/mysqld --initialize --user=mysql --basedir=/opt/mysql8.0 --datadir=/opt/mysql8.0/data #初始化MySQL,会生成默认密码,用于第一次登陆
创建my.cnf配置文件文件配置MySQL

因为mysql启动会自动寻找/etc/my.cnf配置文件,所以把配置文件放到/etc目录下,命名为my.cnf

[mysqld]

#Rove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
# These are commonly set, remove the # and set as required.
basedir = /opt/mysql
datadir = /opt/mysql/data
socket = /tmp/mysql.sock
log-error = /opt/mysql/data/error.log
pid-file = /opt/mysql/data/mysql.pid
tmpdir = /tmp
port = 3306
#lower_case_table_names = 1
# server_id = .....
# socket = .....
#lower_case_table_names = 1
max_allowed_packet=32M
default-authentication-plugin = mysql_native_password
#lower_case_file_system = on
#lower_case_table_names = 1
log_bin_trust_function_creators = ON
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
设置开机自启动

# cd /mysql解压目录/support-files/
# cp mysql.server /etc/init.d/mysql 
# chmod +x /etc/init.d/mysql
注册服务

chkconfig --add mysql
etc/ld.so.conf要配置路径

/mysql安装目录全路径/lib
配置环境变量

export PATH=$PATH:/mysql解压全路径/bin:/mysql解压全路径/lib
启动MySQL,登陆,输入之前随机生成的密码

service mysql start #启动mysql
mysql -uroot -p #登陆

然后输入之前初始化自动生成的密码登陆
修改root密码

alter user 'root'@'localhost' identified by '你的新的root账户密码';
开启MySQL远程访问
修改root用户可访问的ip:host选项由loaclhost变更为%,意为允许所有ip登陆访问

use mysql; #切换到mysql库
update user set host ='%' where user='root'; #修改host为所有IP
修改系统防火墙允许3306端口通过

MySQL8.0版本还需要修改密码的加密方式:

先切换到mysql库

use mysql
一.修改加密方式

ALTER USER'root'@'%' IDENTIFIED BY '你的密码' PASSWORD EXPIRE NEVER;
二.修改密码

ALTER USER'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码';
三.授权root用户可以远程访问

GRANT ALL ON *.* TO 'root'@'%';
四.刷新

FLUSH PRIVILEGES;
然后重启服务
远程客户端打开sqlyog访问测试

你可能感兴趣的:(linux,mysql,数据库)