MySQL安装入门

Posted by Kaka Blog on January 16, 2019

安装

ubuntu版本:18.04 mysql版本:5.7.24

1、下载

sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.11-1_all.deb

2、更新源

sudo dpkg -i mysql-apt-config_0.8.11-1_all.deb
sudo apt-get update

3、正式安装

sudo apt-get install mysql-server

4、查看密码

sudo cat /etc/mysql/debian.cnf

5、设置root密码

mysql -udebian-sys-maint -p

修改密码:

mysql> use mysql;
mysql> update user set authentication_string=password('bq09Pv0F89mbONyMgEYpKAAH') where user='root' and Host ='localhost';
mysql> update user set plugin="mysql_native_password";
mysql> flush privileges;
mysql> quit;

6、重启服务

sudo service mysql restart

之后就可以用root账号登录。

配置MySQL

修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf,修改字符适配utf8,添加内容:

character_set_server=utf8
init_connect='SET NAMES utf8'

注释掉:bind-address = 127.0.0.1,保证其他服务器可以访问MySQL

配置远程登录:

mysql> grant all on *.* to root@'%' identified by 'bq09Pv0F89mbONyMgEYpKAAH' with grant option;
mysql> flush privileges;
mysql> quit;

配置主从

服务器A(192.168.1.1)配置文件:

log-bin = mysql-bin
server-id = 1 
expire-logs-days = 100 
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
auto-increment-increment = 2 
auto-increment-offset = 1

服务器A(192.168.1.2)配置文件:

log-bin = mysql-bin
server-id = 2 
expire-logs-days = 100 
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
auto-increment-increment = 2 
auto-increment-offset = 2

重启两台MySQL 在服务器A(192.168.1.1)上授权用户:

GRANT REPLICATION SLAVE ON *.* TO 'bak'@'192.168.1.2 ' IDENTIFIED BY PASSWORD '123456';

在服务器B(192.168.1.2)上授权用户

GRANT REPLICATION SLAVE ON *.* TO 'bak'@'192.168.1.1 ' IDENTIFIED BY PASSWORD '123456';

在A服务器(192.168.1.1)上执行:

change master to master_host='192.168.1.2',master_user='bak',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;

在B服务器(192.168.1.2)上执行:

change master to master_host='192.168.1.1 ',master_user='bak',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;

最后执行:start slave;

导入导出

导出数据库:

mysqldump -uroot -p database_name > database_name.sql

导入数据库:

mysqladmin -uroot -p create database_name
mysql -uroot -p database_name < database_name.sql

导出表:

mysqldump -uroot -p database_name table_name > database_name_table_name.sql

导入表:

先连接到数据库

mysql> source /home/bigdata/database_name_table_name.sql

Q&A

1、ubuntu安装mysql报错:dpkg: error processing package mysql-server (–configure)

解决方法:检测3306端口是否被占用,是的话修改端口重新启动,如果需要卸载,可以执行以下命令:

sudo rm /var/lib/mysql/ -R
sudo rm /etc/mysql/ -R
sudo apt-get autoremove 'mysql*' --purge
sudo apt-get install mysql-server

2、修改datadir路径启动失败:

Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

解决方法:

# 建立新的bai路径
sudo mkdir -p /data/mysql
# 复制数据
sudo cp -R /var/lib/mysql/* /data/mysql
# 修改文件夹权限
sudo chown -R mysql:mysql /data/mysql
# 修改配置文件
sudo vim /etc/mysql/my.cnf
# b查找并用#注释以下
datadir = /var/lib/mysql
# 新增
datadir = /data/mysql

3、mysql 链接时报错:1251-Client does not support authentication protocol requested by server

解决方法:

mysql> use mysql;
mysql> select user,host,plugin from user where user='root';
mysql> update user set plugin="mysql_native_password" where user='root';
# 使用mysql_native_password对新密码进行编码。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

参考资料